package com.cr.widget

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.opengl.Visibility
import android.text.Editable
import android.text.InputType
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.View
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import com.cr.cruav.R
import com.cr.data.CrUtil.Companion.checkBlank
import com.cr.data.FontManager

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/3/29 11:38
 * 描述：文本框编辑组件
 */
class CrEditTextWidget @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : CrLinearLayoutWidget(context, attrs, defStyleAttr), View.OnClickListener,TextWatcher {
    // define: 2023/3/29 编辑文本框
    private var txtContent: TextView? = null
    // define: 2023/3/29 字体图标
    private var leftICO: TextView? = null
    // define: 2023/3/29 图片图标
    private var imageView: ImageView? = null
    // define: 2023/3/29 删除按钮
    private var btnDelete: ImageButton? = null

    /**
     * 初始化
     */
    init {
        initView(R.layout.wg_edit_text)
        joinControls()
        initProperty(attrs!!)
    }

    /**
     * 链接控件
     */
    override fun joinControls(){
        txtContent = findViewById(R.id.view_edit_txt)
        txtContent?.addTextChangedListener(this)
        leftICO = findViewById(R.id.view_edit_let_lbl)
        imageView = findViewById(R.id.view_edit_left_img)
        btnDelete = findViewById(R.id.view_edit_btn_del)
        // todo: 2023/4/3 默认删除按钮隐藏
        btnDelete?.visibility = GONE
        btnDelete?.setOnClickListener(this)
    }

    /**
     * 初始化属性
     * @param attrs AttributeSet 属性管理器
     */
    override fun initProperty(attrs: AttributeSet) {
        var attributes: TypedArray = context!!.obtainStyledAttributes(attrs,R.styleable.ViewEditTextProperty);
        if(attributes != null){
            // todo: 2023/3/29 设置提示文字
            var hintText: String? = attributes.getString(R.styleable.ViewEditTextProperty_crHint)
            hintText?.let { txtContent?.hint = it }
            // todo: 2023/3/29 设置图片
            var drawble:Drawable? = attributes.getDrawable(R.styleable.ViewCommonProperty_crLeftImg)
            if(drawble != null){
                imageView?.visibility = VISIBLE
                imageView?.setImageDrawable(drawble)
            }else{
                imageView?.visibility = GONE
            }
            // todo: 2023/3/29 设置图标
            var res:String? = attributes.getString(R.styleable.ViewCommonProperty_crLeftResource);
            if(res == null){
                leftICO?.visibility = GONE;
            }else{
                FontManager.setTextView(context!!,leftICO!!,res);
            }
            // todo: 2023/3/29 设置是否为密码框
            var isPassword = attributes.getBoolean(R.styleable.ViewCommonProperty_crIsPassword,false)
            if(isPassword) txtContent?.inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT
            // todo: 2023/3/29 设置图标颜色
            var color:Int? = attributes.getColor(R.styleable.ViewCommonProperty_crIcoColor, Color.BLACK)
            color!!.let {
                leftICO?.setTextColor(color)
            }
            // todo: 2023/3/29 设置提示信息颜色
            var hintColor:Int?=attributes.getColor(R.styleable.ViewCommonProperty_crHintColor,Color.argb(255,188,188,188))
            hintColor!!.let {
                txtContent?.setHintTextColor(hintColor)
            }
            // todo: 2023/3/29 设置文本颜色
            var textColor:Int?=attributes.getColor(R.styleable.ViewCommonProperty_crTextColor,Color.argb(255,71,129,233))
            textColor!!.let {
                txtContent?.setTextColor(textColor)
            }
            // todo: 2023/3/29 应用属性
            attributes.recycle()
        }
    }

    /**
     * 设置显示内容
     * @param text String
     */
    fun setContent(text:String){
        txtContent?.text = text
    }

    /**
     * 获取显示内容
     * @return String
     */
    fun getContent():String{
        return txtContent?.text.toString()
    }

    /**
     * 设置是否为密码框
     * @param isPassword Boolean
     */
    fun setIsPassword(isPassword:Boolean){
        if(isPassword){
            txtContent?.inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT
        }else{
            txtContent?.inputType = InputType.TYPE_CLASS_TEXT
        }
    }

    /**
     * 设置文字颜色
     * @param color Int 颜色
     */
    fun setContentColor(color:Int){
        txtContent?.setTextColor(color)
    }

    /**
     * 设置提示文字颜色
     * @param color Int 颜色
     */
    fun setContentHintColor(color:Int){
        txtContent?.setHintTextColor(color)
    }

    /**
     * 设置字体图标
     * @param icoName String 图标名称
     */
    fun setFontIcon(icoName:String){
        leftICO?.visibility = VISIBLE
        imageView?.visibility = GONE
        FontManager.setTextView(context!!,leftICO!!,icoName);
    }

    /**
     * 检查输入内容是否为空
     * @param message String 错误消息
     * @return String?
     */
    fun checkBlank(message:String):String?{
        return txtContent?.checkBlank(message)
    }

    /**
     * 设置图片图标
     * @param drawable Drawable 图标资源
     */
    fun setImageIcon(drawable: Drawable){
        leftICO?.visibility = GONE
        imageView?.visibility = VISIBLE
        imageView?.setImageDrawable(drawable)
    }


    /**
     * 视图点击事件
     * @param view View
     */
    override fun onClick(view: View?) {
        when(view?.id){
            R.id.view_edit_btn_del->
                txtContent?.text = ""
        }
    }

    /**
     * 文本框变化事件 变化前
     * @param p0 CharSequence
     * @param p1 Int
     * @param p2 Int
     * @param p3 Int
     */
    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
    }

    /**
     * 文本框变化事件 变化中
     * @param p0 CharSequence
     * @param p1 Int
     * @param p2 Int
     * @param p3 Int
     */
    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        if(txtContent?.text!!.isNotEmpty()){
            btnDelete?.visibility = VISIBLE
        }else{
            btnDelete?.visibility = GONE
        }
    }

    /**
     * 文本框变化事件 变化后
     * @param p0 Editable
     */
    override fun afterTextChanged(p0: Editable?) {

    }
}