package com.xhly.manageapp.customview

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.xhkjedu.manageapp.R


class CombinationCardView : FrameLayout {
    private var cv: LinearLayout
    private var root: View
    private var iv: ImageView
    private var tv:TextView
    private var x = 0f
    private var y = 0f
    private var listener: OnClickListener?=null
    constructor(mContext: Context) : this(mContext, null)

    constructor(mContext: Context, mAttributeSet: AttributeSet?) : this(mContext, mAttributeSet, 0)
    constructor(
        mContext: Context,
        mAttributeSet: AttributeSet?,
        defStyleAttr: Int
    ) : super(mContext, mAttributeSet, defStyleAttr) {
        root= LayoutInflater.from(mContext).inflate(R.layout.btncardview, this)
        cv =root.findViewById(R.id.llayout_cv)
        iv =root.findViewById(R.id.iv)
        tv=root.findViewById(R.id.tv)
    }


    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent?): Boolean {

        when (event!!.action) {
            MotionEvent.ACTION_DOWN -> {
                 x = event!!.x
                 y = event!!.y
                cv.animate().scaleX(0.93f).scaleY(0.93f).setDuration(500).start()
                changeState(0)
            }

            MotionEvent.ACTION_UP -> {
                cv.postDelayed({
                    cv.animate().scaleX(1f).scaleY(1f).setDuration(500).start()
                    changeState(1)
                },120)
                listener?.let {
                    it.onClick(root)
                }
            }
            MotionEvent.ACTION_CANCEL->{
                cv.animate().scaleX(1f).scaleY(1f).setDuration(500).start()
                changeState(1)
            }
        }
        return true
    }

    override fun setOnClickListener(l: OnClickListener?) {
        this.listener=l
        super.setOnClickListener(l)
    }


    private fun changeState(states:Int) {
        if (states == 0) {
            //iv.visibility = View.INVISIBLE
        } else {
            //iv.visibility = View.INVISIBLE
        }
    }
    fun setText(s:String){
        tv.text = s
    }

    fun setImageDrawable(drawable:Drawable){
        iv.setImageDrawable(drawable)
    }
}