package com.yidianling.home.ui.widget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.ydl.ydl_image.module.GlideApp
import com.ydl.ydlcommon.utils.ScreenUtil
import com.yidianling.common.tools.RxImageTool
import com.yidianling.home.R
import com.yidianling.home.event.IHomeBaseEvent
import com.yidianling.home.model.bean.HomeHeaderBean
import com.yidianling.home.ui.view.HomePagerBannerRealView
import kotlinx.android.synthetic.xlzx.home_pager_banner_category_view.view.*

/**
 * @author <a href="https://www.jianshu.com/u/c1e5310dd724">xujian</a>
 * @描述: 首页顶部模块-分类模块
 * @Copyright Copyright (c) 2019
 * @Company 壹点灵
 * @date 2019/02/16
 */
class HomeModuleCategoryView : FrameLayout {

    /**
     * 数据缓存
     */
    var mDataList: ArrayList<HomeHeaderBean.AskCategoryDataBean>? = null
    /**
     * 分类view宽度
     */
    var categoryWidth: Int = 0
    /**
     * 图片宽高
     */
    var imageViewWidth: Int = 0
    /**
     * 文本宽度
     */
    var textViewWidth: Int = 0
    /**
     * 文本高度
     */
    var textViewHeight: Int = 0
    /**
     * 间隔
     */
    var margin: Int = 0

    var dp8: Int = 0


    private var realTestView: HomePagerBannerRealView? = null
    /**
     * 倍数
     */
    private var multiple: Int = 0
    /**
     * 测评首页事件处理类
     */
    private var homeEvent: IHomeBaseEvent? = null

    constructor(context: Context?) : super(context) {
        HomeModuleCategoryView(context, null)
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        initView();
    }

    fun setEvent(homeEvent: IHomeBaseEvent) {
        this.homeEvent = homeEvent
    }

    /**
     * 界面初始化
     */
    private fun initView() {
        View.inflate(context, R.layout.home_pager_banner_category_view, this)
        val params = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.WRAP_CONTENT
        )
//        params.topMargin = ScreenUtil.screenHeight * (356f / 750f).toInt()
        layoutParams = params
        initArgs()
    }

    /**
     * 初始化变量
     */
    private fun initArgs() {
        categoryWidth = ScreenUtil.screenWidth - RxImageTool.dp2px(30f)
        textViewWidth = categoryWidth / 4
        imageViewWidth = RxImageTool.dip2px(38f)
        margin = (categoryWidth - 4 * imageViewWidth) / 8
        dp8 = RxImageTool.dip2px(8f)
        //高度=文字12dp+16dp间距
        textViewHeight = RxImageTool.dip2px(28f)
    }

    fun initData(askCategoryDataBean: List<HomeHeaderBean.AskCategoryDataBean>?) {
        if (null == askCategoryDataBean || askCategoryDataBean.isEmpty()) {
            visibility = View.GONE
            return
        }
        visibility = View.VISIBLE
        if (null == mDataList) {
            mDataList = ArrayList()
        }
        if (getChange(askCategoryDataBean, mDataList)) {
            mDataList!!.clear()
            mDataList!!.addAll(askCategoryDataBean)
        }
        refreshView()
    }

    /**
     * 判断数据是否变化
     */
    private fun getChange(
        bean: List<HomeHeaderBean.AskCategoryDataBean>?,
        cacheBean: List<HomeHeaderBean.AskCategoryDataBean>?
    ): Boolean {
        if (bean?.size != cacheBean?.size) {
            return true
        }
        for (index in 0 until bean?.size!!) {
            if (!bean[index].url.equals(cacheBean!![index].url)
                || !bean[index].coverUrl.equals(cacheBean!![index].coverUrl)
                || !bean[index].cateTitle.equals(cacheBean!![index].cateTitle)
                || bean[index].cateId != cacheBean!![index].cateId
            ) {
                return true
            }
        }
        return false
    }

    /**
     * 刷新View
     */
    private fun refreshView() {
        homeModulePagerBannerFlRoot.removeAllViews()
        var i = 0
        for (item in mDataList!!) {
            if (i > 7) {
                break
            }
            val imageView = createIcon(i)
            val textView = createText(i)

            homeModulePagerBannerFlRoot.addView(imageView)
            homeModulePagerBannerFlRoot.addView(textView)

            imageView.setOnClickListener {
                homeEvent!!.categoryClick(item)
            }
            textView.setOnClickListener {
                homeEvent!!.categoryClick(item)
            }
            bindIconData(imageView, item)
            bindTextData(textView, item)
            i++
        }
    }

    /**
     * 创建图标
     */
    private fun createIcon(i: Int): ImageView {
        val imageParams = FrameLayout.LayoutParams(imageViewWidth, imageViewWidth)
        val imageView = ImageView(context)
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP

        if (i > 3) {
            multiple = 2
            imageParams.topMargin =
                imageViewWidth + dp8 + textViewHeight + dp8 + RxImageTool.dip2px(1f)
            imageParams.leftMargin = margin * (i - 3) + (i - 4) * (imageViewWidth + margin)
        } else {
            multiple = 1
            imageParams.leftMargin = margin * (i + 1) + i * (imageViewWidth + margin)
        }
        imageView.layoutParams = imageParams

        return imageView
    }

    /**
     * 加载图片
     */
    private fun bindIconData(
        imageView: ImageView,
        categoryBean: HomeHeaderBean.AskCategoryDataBean
    ) {


        GlideApp.with(context).load(categoryBean.coverUrl)
            .centerCrop().into(imageView)
    }

    /**
     * 创建文本
     */
    private fun createText(index: Int): TextView {
        val textParams = FrameLayout.LayoutParams(textViewWidth, textViewHeight)
        val textView = TextView(context)
        textView.setTextColor(Color.parseColor("#444444"))

        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f)
        textView.gravity = Gravity.CENTER_HORIZONTAL
        if (index > 3) {
            textParams.topMargin = 2 * (imageViewWidth + dp8) + textViewHeight + dp8
            textParams.leftMargin = textViewWidth * (index - 4)
        } else {
            textParams.topMargin = imageViewWidth + dp8
            textParams.leftMargin = textViewWidth * (index)
        }
        textView.layoutParams = textParams

        return textView
    }

    /**
     * 设置文本
     */
    private fun bindTextData(textView: TextView, categoryBean: HomeHeaderBean.AskCategoryDataBean) {
        textView.text = categoryBean.cateTitle
    }


    fun onDestory() {
        if (null == realTestView) {
            return
        }
        realTestView!!.onDestory()
    }
}