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/4/17 09:12
 * 描述：涂鸦操作页面
 */
class FragmentDoodle : CrNavigationFragment(), CrButton.OnClickListener, OnCheckedChangeListener {
    /**
     * 操作监听接口
     */
    interface OnOperationListener {
        // todo: 2023/4/17 开启绘制
        fun onStartDraw()

        // todo: 2023/4/17 停止绘制
        fun onStopDraw()

        // todo: 2023/4/17 开启选择
        fun onSelect()

        // todo: 2023/4/17 停止选择
        fun onStopSelect()

        // todo: 2023/4/17 清除
        fun onClear()

        // todo: 2023/4/17 保存
        fun onSave()

        // todo: 2023/4/17 删除全部
        fun onRemove()

        // todo: 2023/4/17 移除
        fun onDelete()
    }

    private var btnClear: CrButton? = null // define: 2023/4/17 清除临时绘制的涂鸦
    private var btnSave: CrButton? = null  // define: 2023/4/17 保存临时绘制的涂鸦
    private var btnRemove: CrButton? = null // define: 2023/4/17 全部删除永久保存的涂鸦
    private var btnDelete: CrButton? = null // define: 2023/4/17 删除选择的涂鸦
    private var switchDraw: Switch? = null  // define: 2023/4/17 开启绘制
    private var switchSelect: Switch? = null // define: 2023/4/17 开启选择

    // define: 2023/4/17 操作监听
    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_doodle, 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/4/17 挂载控件
        switchDraw = mainView?.findViewById(R.id.doodle_on_draw)
        switchSelect = mainView?.findViewById(R.id.doodle_on_select)
        btnClear = mainView?.findViewById(R.id.doodle_clear)
        btnSave = mainView?.findViewById(R.id.doodle_save)
        btnRemove = mainView?.findViewById(R.id.doodle_remove)
        btnDelete = mainView?.findViewById(R.id.doodle_delete)
        // todo: 2023/4/17 设置监听
        btnClear?.setListener(this)
        btnSave?.setListener(this)
        btnRemove?.setListener(this)
        btnDelete?.setListener(this)
        switchDraw?.setOnCheckedChangeListener(this)
        switchSelect?.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.doodle_clear -> {
                if (listener != null) listener?.onClear()
            }
            R.id.doodle_save -> {
                if (listener != null) listener?.onSave()
            }
            R.id.doodle_remove -> {
                if (listener != null) listener?.onRemove()
            }
            R.id.doodle_delete -> {
                if (listener != null) listener?.onDelete()
            }
        }
    }

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

            }
            R.id.doodle_on_select -> {
                if (isChecked) {
                    if (listener != null) {
                        listener?.onSelect()
                        listener?.onStopDraw()
                    }
                    switchDraw?.isChecked = false
                } else{
                    if (listener != null) listener?.onStopSelect()
                }
            }
        }
    }

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

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

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

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

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

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