package com.cr.pages

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewpager2.widget.ViewPager2
import com.cr.adapter.CrPageAdapter
import com.cr.cruav.CrApplication
import com.cr.cruav.R
import com.cr.data.CrUtil
import com.cr.event.BarAction
import com.cr.event.EventFragmentBarAction
import com.cr.map.CaseModel

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/6/15 11:18
 * 描述：案件上传页面框架
 */
class FragmentUploadCase : CrNavigationFragment() {
    private var joinCase: CaseModel? = null // define: 2023/6/15 关联的案件模型
    private var adapter: CrPageAdapter? = null // define: 2023/6/15 页面适配器
    private var viewPager: ViewPager2? = null // define: 2023/6/15 页面容器

    // todo: 2023/6/15 主页面
    private var fragmentUploadCaseMain: FragmentUploadCaseMain? = null

    // todo: 2023/6/20 编辑照片页面
    private var fragmentImageEditor: FragmentImageEditor? = null

    /**
     * 初始化
     * @param inflater LayoutInflater
     * @param container ViewGroup?
     * @param savedInstanceState Bundle?
     * @return View?
     */
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // todo: 2023/6/15 设置动画方向
        setAnimationDirection(AnimationDirection.RIGHT)
        self = this
        mainView = inflater.inflate(R.layout.frag_case_upload, null)
        // todo: 2023/6/15 挂接控件
        joinControls()
        // todo: 2023/6/15 初始化月面
        initPage()
        // todo: 2023/6/15 设置导航栏
        setBar(R.id.nv)
        // todo: 2023/6/15 初始化导航栏
        initBar()
        return mainView
    }

    /**
     * 覆写控件挂接
     */
    override fun initControls() {
        // todo: 2023/6/15 挂接页面
        viewPager = mainView?.findViewById(R.id.frag_pages)
        viewPager?.isUserInputEnabled = false  // todo: 2023/6/15 禁止滑动
    }

    /**
     * 初始化页面
     */
    override fun initPage() {
        // todo: 2023/6/15 初始化页面适配器
        adapter = CrPageAdapter(this.activity!!)
        // todo: 2023/6/15 初始化主页面
        fragmentUploadCaseMain = FragmentUploadCaseMain()
        fragmentUploadCaseMain?.crSetListener(caseMainListener)
        adapter?.addFragment(fragmentUploadCaseMain!!)
        // todo: 2023/6/15 设置监听
        viewPager?.adapter = adapter
    }

    /**
     * 显示页面
     * @param fragment CrNavigationFragment
     */
    private fun showPage(fragment: CrNavigationFragment) {
        var fragments = adapter?.getFragments()
        CrUtil.print("页面数量---" + fragments?.size)
        for (i in 0 until fragments!!.size) {
            if (fragments?.get(i) == fragment) {
                fragment.nvBar?.visibility = View.GONE
                viewPager?.currentItem = i
                break
            }
        }
    }

    /**
     * 初始化导航栏
     */
    private fun initBar() {
        nvBar?.crSetTitle(R.string.upload_case_title_main)
        nvBar?.crSetVisible(backIsVisible = false, dismissIsVisible = true)
        showPage(fragmentUploadCaseMain!!)
    }

    /**
     * 上传主页监听
     */
    private val caseMainListener = object : FragmentUploadCaseMain.IOperationListener {
        // todo: 2023/6/20 编辑照片操作回调
        override fun onEditImage(imagePath: String) {
            nvBar?.crSetTitle(R.string.upload_case_title_image_editor)
            nvBar?.crSetVisible(backIsVisible = true, dismissIsVisible = false)
            // todo: 2023/6/20 初始化照片编辑页面
            fragmentImageEditor = FragmentImageEditor()
            adapter?.addFragment(fragmentImageEditor!!)
            // todo: 2023/6/20 设置显示的照片
            fragmentImageEditor?.crSetImage(imagePath)
            // todo: 2023/6/20 显示页面
            showPage(fragmentImageEditor!!)
        }
    }

    /**
     * 设置关联的案件
     * @param joinCase CaseModel 关联案件
     */
    fun crSetJoinCase(joinCase: CaseModel) {
        this.joinCase = joinCase
        fragmentUploadCaseMain!!.crSetJoinCase(this.joinCase!!)
    }

    /**
     * 重写关闭事件
     */
    override fun onDismiss() {
        CrApplication.getEventBus().post(EventFragmentBarAction(self!!, BarAction.ACTION_DISMISS))
    }

    /**
     * 重写回退事件
     */
    override fun onGotoBack() {
        // todo: 2023/6/20 删除刚刚加载的页面
        adapter?.removeLastFragment()
        // todo: 2023/6/20 初始化任务栏
        initBar()

    }
}