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.common.DataManager
import com.cr.cruav.CrApplication
import com.cr.cruav.R
import com.cr.event.BarAction
import com.cr.event.EventFragmentBarAction
import com.cr.map.CaseModel
import com.cr.models.CompletionModel
import com.cr.models.ICompletion

/**
 * 操作系统：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

    // todo: 2023/8/18 上传案件页面
    private var fragmentUploadCaseAction:FragmentUploadAction?=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.requireActivity())
        // 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()
        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()
            fragmentImageEditor?.crSetChangeListener(imageEditorListener)
            adapter?.addFragment(fragmentImageEditor!!)
            // todo: 2023/6/20 设置显示的照片
            fragmentImageEditor?.crSetImage(imagePath)
            // todo: 2023/6/20 显示页面
            showPage(fragmentImageEditor!!)
        }

        // todo: 2023/8/18 案件上传
        override fun onUploadCase(joinCase: CaseModel) {
            nvBar?.crSetTitle(R.string.upload_case_title_main)
            nvBar?.crSetVisible(backIsVisible = true, dismissIsVisible = false)
            // todo: 2023/8/18 初始化页面
            fragmentUploadCaseAction = FragmentUploadAction()
            adapter?.addFragment(fragmentUploadCaseAction!!)
            // todo: 2023/8/18 显示页面
            showPage(fragmentUploadCaseAction!!)
            // todo: 2023/8/25 设置关联案件
            fragmentUploadCaseAction?.crSetJoinCase(joinCase)
        }
    }

    /**
     * 照片编辑监听
     */
    private val imageEditorListener = object:FragmentImageEditor.IChangeListener{
        // todo: 2023/9/28 保存
        override fun onSave(oldName:String,newName:String) {
            // todo: 2023/9/28 保存到编辑表
            if(DataManager.appendEditImage(oldName,newName)){
                DataManager.replaceImageName(joinCase!!.name!!,oldName,newName,object:ICompletion<String>{
                    override fun onCompletion(completion: CompletionModel<String>) {
                        if(completion.isSuccess){
                            // todo: 2023/9/28 保存成功
                            showInformation("照片编辑保存成功！")
                            fragmentUploadCaseMain?.crSetJoinCase(joinCase!!)
                        }else{
                            showError("错误" + completion.message)
                        }
                    }
                })
            }else{
                showError("编辑数据保存失败！")
            }
        }

        // todo: 2023/9/28 恢复
        override fun onRecover(oldName:String,newName:String) {
            DataManager.replaceImageName(joinCase!!.name!!,newName,oldName,object:ICompletion<String>{
                override fun onCompletion(completion: CompletionModel<String>) {
                    if(completion.isSuccess){
                        // todo: 2023/9/28 保存成功
                        showInformation("照片恢复成功！")
                        fragmentUploadCaseMain?.crSetJoinCase(joinCase!!)
                    }else{
                        showError(completion.message)
                    }
                }
            })
        }
    }

    /**
     * 设置关联的案件
     * @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()

    }
}