package com.cr.pages

import android.graphics.BitmapFactory
import android.os.Build
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 androidx.annotation.RequiresApi
import com.cr.cruav.R
import com.cr.view.CrImageEditor
import kotlinx.android.synthetic.main.frag_image_editor.*
import java.io.File
import java.io.FileInputStream

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/6/20 11:20
 * 描述：图片编辑页面
 */
class FragmentImageEditor : CrNavigationFragment(), View.OnClickListener {
    // 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

    /**
     * 初始化
     * @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())
        }

        override fun onStartTrackingTouch(p0: SeekBar?) {
        }

        override fun onStopTrackingTouch(p0: SeekBar?) {
        }

    }

    /**
     * 视图点击事件
     * @param view View
     */
    override fun onClick(view: View?) {
        when (view?.id) {
            R.id.btn_line_color1 -> {
                imageEditor?.crSetLineColor(self?.context!!.getColor(R.color.line_1))
            }
            R.id.btn_line_color2 -> {
                imageEditor?.crSetLineColor(self?.context!!.getColor(R.color.line_2))
            }
            R.id.btn_line_color3 -> {
                imageEditor?.crSetLineColor(self?.context!!.getColor(R.color.line_3))
            }
            R.id.btn_line_color4 -> {
                imageEditor?.crSetLineColor(self?.context!!.getColor(R.color.line_4))
            }
            R.id.btn_line_color5 -> {
                imageEditor?.crSetLineColor(self?.context!!.getColor(R.color.line_5))
            }
            R.id.btn_back -> {

            }
            R.id.btn_reset -> {

            }
            R.id.btn_save -> {

            }
            R.id.btn_recover -> {

            }
        }
    }

    /**
     * 显示图片
     */
    private fun showImage() {
        imagePath?.let {
            var file = File(it)
            if (file.exists()) {
                var fis = FileInputStream(it)
                var bitmap = BitmapFactory.decodeStream(fis)
                imageEditor?.setImageBitmap(bitmap!!)
            }
        }
    }

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