package com.cr.pages

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.SeekBar
import android.widget.TextView
import com.cr.common.CrColorManager
import com.cr.common.CrFileManager
import com.cr.common.DataManager
import com.cr.cruav.R
import com.cr.data.CrUtil
import com.cr.view.CrImageEditor

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/6/20 11:20
 * 描述：图片编辑页面
 */
class FragmentImageEditor : CrNavigationFragment(), View.OnClickListener {
    /**
     * 对外接口
     */
    interface IChangeListener{
        // todo: 2023/9/28 保存照片
        fun onSave(oldName:String,newName:String)

        // todo: 2023/9/28 恢复
        fun onRecover(oldName:String,newName:String)
    }
    // define: 2023/6/20 图片编辑器
    private var imageEditor: CrImageEditor? = null

    // define: 2023/6/26 绘制颜色控制按钮
    private var btnLineColor1: LinearLayout? = null
    private var btnLineColor2: LinearLayout? = null
    private var btnLineColor3: LinearLayout? = null
    private var btnLineColor4: LinearLayout? = null
    private var btnLineColor5: LinearLayout? = null

    // define: 2023/6/26 操作按钮
    private var btnBack: LinearLayout? = null
    private var btnReset: LinearLayout? = null
    private var btnSave: LinearLayout? = null
    private var btnRecover: LinearLayout? = null

    // define: 2023/6/26 控制线宽度
    private var seekLineWidth: SeekBar? = null
    private var lblLineWidth: TextView? = null

    // define: 2023/6/20 图片路径
    private var imagePath: String? = null

    // todo: 2023/9/28 监听
    private var listener:IChangeListener?= null // define: 2023/9/28 监听

    /**
     * 初始化
     * @param inflater LayoutInflater
     * @param container ViewGroup?
     * @param savedInstanceState Bundle?
     * @return View?
     */
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mainView = inflater.inflate(R.layout.frag_image_editor, null)
        self = this
        // todo: 2023/6/20 挂载控件
        joinControls()
        // todo: 2023/6/20 显示图片
        showImage()
        return mainView
    }

    /**
     * 重写挂载控件
     */
    override fun joinControls() {
        // todo: 2023/6/20 挂载图片显示控件
        imageEditor = mainView?.findViewById(R.id.image_editor)
        // todo: 2023/6/26 挂载颜色控制按钮
        btnLineColor1 = mainView?.findViewById(R.id.btn_line_color1)
        btnLineColor1?.setOnClickListener(this)
        btnLineColor2 = mainView?.findViewById(R.id.btn_line_color2)
        btnLineColor2?.setOnClickListener(this)
        btnLineColor3 = mainView?.findViewById(R.id.btn_line_color3)
        btnLineColor3?.setOnClickListener(this)
        btnLineColor4 = mainView?.findViewById(R.id.btn_line_color4)
        btnLineColor4?.setOnClickListener(this)
        btnLineColor5 = mainView?.findViewById(R.id.btn_line_color5)
        btnLineColor5?.setOnClickListener(this)
        // todo: 2023/6/26 挂载操作按钮
        btnBack = mainView?.findViewById(R.id.btn_back)
        btnBack?.setOnClickListener(this)
        btnReset = mainView?.findViewById(R.id.btn_reset)
        btnReset?.setOnClickListener(this)
        btnSave = mainView?.findViewById(R.id.btn_save)
        btnSave?.setOnClickListener(this)
        btnRecover = mainView?.findViewById(R.id.btn_recover)
        btnRecover?.setOnClickListener(this)
        // todo: 2023/6/26 初始化线宽度
        seekLineWidth = mainView?.findViewById(R.id.seek_line_width)
        lblLineWidth = mainView?.findViewById(R.id.lbl_line_width)
        seekLineWidth?.let {
            it.max = 10
            it.progress = 3
            lblLineWidth?.setText(String.format("线条宽度 ${it.progress}"))
        }
        seekLineWidth?.setOnSeekBarChangeListener(seekLineWidthListener)
    }

    /**
     * 线条宽度修改监听
     */
    private val seekLineWidthListener = object :SeekBar.OnSeekBarChangeListener{
        // todo: 2023/6/26 变化监听
        override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
            lblLineWidth?.text = String.format("线条宽度 $p1")
            imageEditor?.crSetLineWidth(p1.toFloat())
        }

        // todo: 2023/9/28 开始变化
        override fun onStartTrackingTouch(p0: SeekBar?) {
        }

        // todo: 2023/9/28 停止变化
        override fun onStopTrackingTouch(p0: SeekBar?) {
        }

    }

    /**
     * 视图点击事件
     * @param view View
     */
    override fun onClick(view: View?) {
        when (view?.id) {
            R.id.btn_line_color1 -> {
                imageEditor?.crSetLineColor(CrColorManager.getColor(R.color.line_1))
            }
            R.id.btn_line_color2 -> {
                imageEditor?.crSetLineColor(CrColorManager.getColor(R.color.line_2))
            }
            R.id.btn_line_color3 -> {
                imageEditor?.crSetLineColor(CrColorManager.getColor(R.color.line_3))
            }
            R.id.btn_line_color4 -> {
                imageEditor?.crSetLineColor(CrColorManager.getColor(R.color.line_4))
            }
            R.id.btn_line_color5 -> {
                imageEditor?.crSetLineColor(CrColorManager.getColor(R.color.line_5))
            }
            R.id.btn_back -> {
                // todo: 2023/9/28 回退一步
                imageEditor?.crSetUndo()
            }
            R.id.btn_reset -> {
                // todo: 2023/9/28 重绘
                imageEditor?.crReset()
            }
            R.id.btn_save -> {
                // todo: 2023/9/28 保存文件
                var fileName = imageEditor?.crSaveImage()
                if(fileName != null){
                    // todo: 2023/9/28 反馈
                    val oldName = CrFileManager.getFileName(this.imagePath!!)
                    this.listener?.onSave(oldName!!,fileName)
                }else{
                    showError("保存失败")
                }
            }
            R.id.btn_recover -> {
                // todo: 2023/9/28 找到原来的照片
                val imgName = CrFileManager.getFileName(this.imagePath!!)
                val oldName = DataManager.queryEditImage(imgName!!)
                if(oldName == null){
                    showError("文件已不存在，无法恢复！")
                }else{
                    this.imagePath = String.format("%s%s",CrUtil.IMAGE_PATH,oldName)
                    // todo: 2023/9/28 反馈
                    this.listener?.onRecover(oldName!!,imgName)
                }
                crSetImage(this.imagePath!!)
            }
        }
    }

    /**
     * 显示图片
     */
    private fun showImage() {
        imagePath?.let {
            imageEditor?.crSetBitmap(it)
        }
    }

    /**
     * 设置显示的图片
     * @param imagePath String 图片路径
     */
    fun crSetImage(imagePath: String) {
        this.imagePath = imagePath
        // todo: 2023/6/20 显示图片
        showImage()
    }

    /**
     * 设置监听
     * @param listener IChangeListener 监听
     */
    fun crSetChangeListener(listener:IChangeListener){
        this.listener = listener
    }
}