package cn.muchinfo.rma.view.autoWidget

import android.app.Activity
import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.Fragment
import cn.muchinfo.rma.R
import kotlinx.android.synthetic.main.head_view.view.*
import org.jetbrains.anko.view

/**
 * 头部view封装
 */
class HeadView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
): ConstraintLayout(context, attrs, defStyleAttr) {

    private var leftListener: OnClickListener? = null
    private var rightListener: OnClickListener? = null

    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.HeadView)
        initViews(typedArray)
        initListeners()
        typedArray.recycle()
    }

    private fun initListeners() {
        id_left?.setOnClickListener {
            view {
                if (context != null) {
                    if (context is Activity) {
                        (context as Activity).finish()
                    } else if (context is Fragment) {
                        (context as Fragment).activity?.finish()
                    }
                }
                leftListener?.onClick(null)
            }
        }

        id_right?.setOnClickListener { view {
                rightListener?.onClick(null)
            }
        }
    }

    private fun initViews(typedArray: TypedArray) {
        View.inflate(context, R.layout.head_view, this)
        // 读取title
        val title = typedArray.getString(R.styleable.HeadView_hv_title);
        if ("" == title || title == null) {
            id_title?.text = resources.getString(R.string.str_title)
        } else {
            id_title?.text = title
        }
        // 是否显示左侧按钮
        val showLeft = typedArray.getBoolean(R.styleable.HeadView_hv_showLeft, true)
        id_left?.visibility = if (showLeft) View.VISIBLE else View.GONE
        // 设置左侧图片
        val left = typedArray.getInt(R.styleable.HeadView_hv_left, 0)
        if (left != 0) {
            id_left?.setImageDrawable(resources.getDrawable(left, context.theme))
        }
        // 是否显示右侧按钮
        val showRight = typedArray.getBoolean(R.styleable.HeadView_hv_showRight, false)
        id_right?.visibility = if (showRight) View.VISIBLE else View.GONE
        // 设置右侧图片
        val right = typedArray.getInt(R.styleable.HeadView_hv_right, 0)
        if (right != 0) {
            id_right?.setImageDrawable(resources.getDrawable(right, context.theme))
        }
    }

//region 外部访问方法

    public fun setLeftListener(leftListener: OnClickListener?) {
        this.leftListener = leftListener
    }

    public fun setRightListener(rightListener: OnClickListener?) {
        this.rightListener = rightListener
    }

    /**
     * 设置title值
     */
    fun setTitle(titleId: Int) {
        setTitle(resources.getString(titleId))
    }

    fun setTitle(titleString: String) {
        id_title?.text = titleString
    }

    /**
     * 获取title的值
     */
    fun getTitle() = id_title?.text

    /**
     * 设置左侧图片
     */
    fun setLeftView(leftId: Int) {
        id_left?.setImageDrawable(resources.getDrawable(leftId, context.theme))
    }

    /**
     * 设置右侧图片
     */
    fun setRightView(rightId: Int) {
        id_right?.setImageDrawable(resources.getDrawable(rightId, context.theme))
    }

    /**
     * 可以设置有个View进来。
     */
    fun setTitleView(view: View) {
        id_title?.visibility = View.GONE
        id_title_view?.visibility = View.VISIBLE
        id_title_view?.addView(view)
    }

//endregion

}