package com.cr.pages

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CompoundButton
import android.widget.CompoundButton.OnCheckedChangeListener
import android.widget.Switch
import com.cr.cruav.CrApplication
import com.cr.cruav.R
import com.cr.event.BarAction
import com.cr.event.EventFragmentBarAction
import com.cr.widget.CrButton
import com.squareup.otto.Subscribe

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/6/9 17:12
 * 描述：案件图斑编辑页面
 */
class FragmentCaseTools : CrNavigationFragment(), CrButton.OnClickListener,
    OnCheckedChangeListener {
    /**
     * 操作监听接口
     */
    interface OnOperationListener {
        // todo: 2023/6/9 开启绘制
        fun onStartDraw()

        // todo: 2023/6/9 停止绘制
        fun onStopDraw()

        // todo: 2023/6/9 保存
        fun onSave()

        // todo: 2023/6/9 删除
        fun onRemove()

        // todo: 2023/6/9 回退一步
        fun onUndo()

        // todo: 2023/6/9 重绘
        fun onReset()

        // todo: 2023/6/9 移动案件点
        fun onMoveCasePoint()

        // todo: 2023/6/13 删除违建点
        fun onRemoveCasePoint()
    }

    private var btnSave: CrButton? = null  // define: 2023/6/9 保存
    private var btnDelete: CrButton? = null  // define: 2023/6/9 删除
    private var btnUndo: CrButton? = null  // define: 2023/6/9 回退一步
    private var btnReset: CrButton? = null  // define: 2023/6/9 重新绘制
    private var btnMovePoint: CrButton? = null // define: 2023/6/9 移动案件点
    private var switchDraw: Switch? = null // define: 2023/6/9 开启绘制
    private var btnRemoveCasePoint:CrButton?=null  // define: 2023/6/13 删除违建点

    // todo: 2023/6/9 操作监听
    private var listener: OnOperationListener? = null

    /**
     * 创建视图
     * @param inflater LayoutInflater
     * @param container ViewGroup?
     * @param savedInstanceState Bundle?
     * @return View?
     */
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // todo: 2023/4/17 订阅监听
        CrApplication.getEventBus().register(this)
        self = this
        mainView = inflater.inflate(R.layout.frag_case_tools, null)
        // todo: 2023/4/17 设置导航栏
        setBar(R.id.nv)
        // todo: 2023/4/17 设置展现方式
        setAnimationDirection(AnimationDirection.LEFT)
        // todo: 2023/4/17 关联控件
        joinControls()
        return mainView
    }

    /**
     * 重写挂载控件
     */
    override fun joinControls() {
        // todo: 2023/6/9 挂在控件
        switchDraw = mainView?.findViewById(R.id.case_on_draw)
        btnSave = mainView?.findViewById(R.id.case_save)
        btnDelete = mainView?.findViewById(R.id.cases_delete)
        btnUndo = mainView?.findViewById(R.id.case_undo)
        btnReset = mainView?.findViewById(R.id.case_reset)
        btnMovePoint = mainView?.findViewById(R.id.case_move_point)
        btnRemoveCasePoint = mainView?.findViewById(R.id.case_remove_point)
        // todo: 2023/6/9 设置监听
        btnSave?.setListener(this)
        btnDelete?.setListener(this)
        btnUndo?.setListener(this)
        btnReset?.setListener(this)
        btnMovePoint?.setListener(this)
        btnRemoveCasePoint?.setListener(this)
        switchDraw?.setOnCheckedChangeListener(this)
    }

    /**
     * 关闭页面
     */
    override fun dismiss() {
        CrApplication.getEventBus().post(EventFragmentBarAction(self!!, BarAction.ACTION_DISMISS))
    }

    /**
     * 视图点击事件
     * @param view View
     */
    override fun onClick(view: View) {
        when (view?.id) {
            R.id.case_save -> {
                if (listener != null) listener?.onSave()
                switchDraw?.isChecked = false
            }
            R.id.cases_delete -> {
                if (listener != null) listener?.onRemove()
            }
            R.id.case_undo -> {
                if (listener != null) listener?.onUndo()
            }
            R.id.case_reset -> {
                if (listener != null) listener?.onReset()
            }
            R.id.case_move_point->{
                if(listener != null) listener?.onMoveCasePoint()
                switchDraw?.isChecked = false
                onDismiss()
            }
            R.id.case_remove_point->{
                if(listener != null) listener?.onRemoveCasePoint()
                switchDraw?.isChecked = false
                onDismiss()
            }
        }
    }

    /**
     * 开关事件
     * @param view CompoundButton
     * @param isChecked Boolean
     */
    override fun onCheckedChanged(view: CompoundButton?, isChecked: Boolean) {
        when (view?.id) {
            R.id.case_on_draw -> {
                if (isChecked) {
                    if (listener != null) {
                        listener?.onStartDraw()
                    }
                } else {
                    if (listener != null) listener?.onStopDraw()
                }

            }
        }
    }

    /**
     * 重写页面初始化
     */
    override fun initPage() {
        switchDraw?.isChecked = false
    }

    /**
     * 设置操作监听
     * @param listener OnOperationListener 操作监听
     */
    fun setListener(listener: OnOperationListener) {
        this.listener = listener
    }

    /**
     * 订阅事件执行
     * @param event EventAction
     */
    @Subscribe
    fun onAction(event: EventCaseToolsAction) {
        when (event.action) {
            EventCaseToolsAction.CLOSE_DRAW -> {
                switchDraw?.isChecked = false
            }
        }
    }

    // todo: 2023/4/17 生命周期
    /**
     * 销毁
     */
    override fun onDestroy() {
        // todo: 2023/4/17 解除订阅监听
        CrApplication.getEventBus().unregister(this)
        super.onDestroy()
    }

    // todo: 2023/4/17 订阅事件类
    class EventCaseToolsAction @JvmOverloads constructor(
        action: Int
    ) {
        // define: 2023/4/17 定义动作常量
        companion object {
            const val CLOSE_DRAW: Int = 0
        }

        // define: 2023/4/17 定义动作
        var action: Int = -1

        /**
         * 初始化
         */
        init {
            this.action = action
        }
    }
}