package com.cr.pages

import android.graphics.Color
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.ImageView
import android.widget.Switch
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter
import com.contrarywind.listener.OnItemSelectedListener
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.EventMarkChange
import com.cr.view.CrViewWheel
import com.cr.widget.CrButtonWidget
import com.squareup.otto.Subscribe
import dji.v5.utils.common.ContextUtil

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/4/17 09:12
 * 描述：标志操作页面
 */
class FragmentMark : CrNavigationFragment(), CrButtonWidget.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: CrButtonWidget? = null // define: 2023/4/17 清除临时绘制的涂鸦
    private var btnSave: CrButtonWidget? = null  // define: 2023/4/17 保存临时绘制的涂鸦
    private var btnRemove: CrButtonWidget? = null // define: 2023/4/17 全部删除永久保存的涂鸦
    private var btnDelete: CrButtonWidget? = null // define: 2023/4/17 删除选择的涂鸦
    private var switchDraw: Switch? = null  // define: 2023/4/17 开启绘制
    private var switchSelect: Switch? = null // define: 2023/4/17 开启选择
    private var imageView:ImageView?= null  // define: 2023/4/18 图片
    private var wheelAngle:CrViewWheel?=null // define: 2023/4/18 角度选择器
    private var wheelType:CrViewWheel?=null // define: 2023/4/18 类型选择器

    private var mAngleItems:MutableList<Int> = mutableListOf()  // define: 2023/4/18 角度集合
    private var mTypeItems:MutableList<EventMarkChange> = mutableListOf()  // define: 2023/4/18 类型集合

    private var markChange:EventMarkChange?= EventMarkChange()  // define: 2023/4/18 选中符号

    // define: 2023/4/17 操作监听
    private var listener: OnOperationListener? = null

    // define: 2023/4/21 图片尺寸
    private var imgWidth:Int = 0
    private var imgHeight:Int = 0

    /**
     * 创建视图
     * @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_mark, 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.mark_on_draw)
        switchSelect = mainView?.findViewById(R.id.mark_on_select)
        btnClear = mainView?.findViewById(R.id.mark_clear)
        btnSave = mainView?.findViewById(R.id.mark_save)
        btnRemove = mainView?.findViewById(R.id.mark_remove)
        btnDelete = mainView?.findViewById(R.id.mark_delete)
        imageView = mainView?.findViewById(R.id.mark_ico)
        wheelAngle = mainView?.findViewById(R.id.mark_angle)
        wheelType = mainView?.findViewById(R.id.mark_type)
        // todo: 2023/4/17 设置监听
        btnClear?.setListener(this)
        btnSave?.setListener(this)
        btnRemove?.setListener(this)
        btnDelete?.setListener(this)
        switchDraw?.setOnCheckedChangeListener(this)
        switchSelect?.setOnCheckedChangeListener(this)
        // todo: 2023/4/18 选择器初始化
        wheelAngle?.setTextColorCenter(Color.YELLOW) // TODO: 4/17/21 设置选中项颜色
        wheelAngle?.setItemsVisibleCount(3)
        wheelAngle?.setTextSize(CrUtil.getDimens(R.dimen.sp_6))
        wheelAngle?.setTypeface(CrUtil.getFont(ContextUtil.getContext()))
        wheelAngle?.setCyclic(false) // TODO: 6/9/21 禁止循环
        wheelAngle?.setOnItemSelectedListener(angleListener)

        wheelType?.setTextColorCenter(Color.YELLOW) // TODO: 4/17/21 设置选中项颜色
        wheelType?.setItemsVisibleCount(3)
        wheelType?.setTextSize(CrUtil.getDimens(R.dimen.sp_6))
        wheelType?.setTypeface(CrUtil.getFont(ContextUtil.getContext()))
        wheelType?.setCyclic(false) // TODO: 6/9/21 禁止循环
        wheelType?.setOnItemSelectedListener(typeListener)

        // todo: 2023/4/21 给ImageView挂载事件
        imageView?.viewTreeObserver?.addOnGlobalLayoutListener {
            imgWidth = imageView!!.width
            imgHeight = imageView!!.height
            setMark()
        }

        // todo: 2023/4/18 页面初始化
        initPage()
    }

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

    /**
     * 视图点击事件
     * @param view View
     */
    override fun onClick(view: View) {
        when (view?.id) {
            R.id.mark_clear -> {
                if (listener != null) listener?.onClear()
            }
            R.id.mark_save -> {
                if (listener != null) listener?.onSave()
            }
            R.id.mark_remove -> {
                if (listener != null) listener?.onRemove()
            }
            R.id.mark_delete -> {
                if (listener != null) listener?.onDelete()
            }
        }
    }

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

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

    /**
     * 重写页面初始化
     */
    override fun initPage() {
        switchDraw?.isChecked = false
        switchSelect?.isChecked = false
        // todo: 2023/4/18 符号初始化
        markChange?.markName = "红色箭头"
        markChange?.markDrawable = R.drawable.ico_draw_red
        markChange?.markAngle = 50
        // todo: 2023/4/18 角度初始化
        for(angle in 1 until 36){
            mAngleItems.add(angle*10)
        }
        wheelAngle?.adapter = ArrayWheelAdapter(mAngleItems)
        // todo: 2023/4/18 初始化角度选择
        wheelAngle?.setSelectItem(markChange?.markAngle)
        // todo: 2023/4/18 符号样式初始化
        mTypeItems.add(EventMarkChange("红色箭头",R.drawable.ico_draw_red))
        mTypeItems.add(EventMarkChange("绿色箭头",R.drawable.ico_draw_green))
        mTypeItems.add(EventMarkChange("蓝色箭头",R.drawable.ico_draw_blue))
        mTypeItems.add(EventMarkChange("通用标志",R.drawable.ico_ty))
        var typeItems:MutableList<String> = mutableListOf()
        for(item in mTypeItems){
            typeItems.add(item.markName)
        }
        wheelType?.adapter = ArrayWheelAdapter(typeItems)
        wheelType?.setSelectItem(markChange?.markName)
    }

    /**
     * 角度选择监听器
     */
    private val angleListener = OnItemSelectedListener { index ->
        var angle = mAngleItems[index]
        markChange?.markAngle = angle
        setMark()
    }

    /**
     * 类型选择监听器
     */
    private val typeListener = OnItemSelectedListener { index ->
        var item = mTypeItems[index]
        markChange?.markName = item.markName
        markChange?.markDrawable = item.markDrawable
        setMark()
    }

    /**
     * 设置符号
     */
    private fun setMark(){
        // todo: 2023/4/18 设置符号
        imageView?.setImageDrawable(context!!.getDrawable(markChange!!.markDrawable))
        // todo: 2023/4/18 设置角度
        setAngle(markChange!!.markAngle)
    }

    /**
     * 设置角度
     * @param angle Int
     */
    private fun setAngle(angle:Int){
        imageView?.pivotX = (imgWidth/2).toFloat()
        imageView?.pivotY = (imgHeight/2).toFloat()
        imageView?.rotation = angle.toFloat()
    }

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

    /**
     * 获取当前符号
     * @return EventMarkChange?
     */
    fun getMark():EventMarkChange?{
        return markChange
    }

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

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

    /**
     * 显示隐藏监听
     * @param hidden Boolean
     */
    override fun onHiddenChanged(hidden: Boolean) {
       if(!hidden){
           //setMark()
       }
    }

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

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