package com.quansu.heifengwuliu.widget

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.hwangjr.rxbus.RxBus
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.QMUIViewPager
import com.qmuiteam.qmui.widget.tab.QMUITab
import com.qmuiteam.qmui.widget.tab.QMUITabIndicator
import com.qmuiteam.qmui.widget.tab.QMUITabSegment
import com.quansu.heifengwuliu.R
import com.quansu.heifengwuliu.adapter.BaseFragmentPagerAdapter
import com.quansu.heifengwuliu.config.ConfigRx
import com.quansu.heifengwuliu.databinding.WidgetShipmentheadBinding
import com.quansu.heifengwuliu.fragment.OwnerImgFragment
import com.quansu.heifengwuliu.model.DataInfoBean
import com.ysnows.base.utils.B
import com.ysnows.base.view.BView
import com.ysnows.base.view.BaseView

/**
 * 发货 --头部
 *Created by shihuiyun
 *on 2020/9/15
 */
class ShipmentHeadView(context: Context, attrs: AttributeSet) :BaseLinearLayout(context,attrs) {

    private var binding: WidgetShipmentheadBinding? = null
    private var view: BView? = null
    var cats = ArrayList<String>()

    //车型
    var type_id=""

    init {
        init(context)

    }

    private fun init(context: Context) {
        if (context is BaseView) {
            view = context as BView
        }
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.widget_shipmenthead, this, true)


    }

    fun getCats(): MutableList<String> {

        return cats.toMutableList()
    }

    fun  setHeadData(info: DataInfoBean, fm: FragmentManager){

        if (null != info.car_type) {
            for (item in info.car_type) {
                cats.add(item.type_name)
            }
        }
        //默认第一个
        type_id=info.car_type[0].type_id

        //设置viewpager
        val fragments = ArrayList<Fragment>()
        var index = 0
        for (cat in cats) {
            var ownerImgFragment = OwnerImgFragment()
            ownerImgFragment.arguments = B().putString("img", info.car_type[index].type_img).ok()
            fragments.add(ownerImgFragment)
            index++
        }
        val adapter = BaseFragmentPagerAdapter(fm, fragments)//childFragmentManager
        binding!!.qmViewPager.adapter = adapter

        //设置tab
        binding!!.tabs.setIndicator(QMUITabIndicator(QMUIDisplayHelper.dp2px(context, 2), false, true))
        val space: Int = QMUIDisplayHelper.dp2px(context, 20)
        binding!!.tabs.setPadding(space, 0, space, 0)
        binding!!.tabs.mode = QMUITabSegment.MODE_SCROLLABLE
//        val space: Int = QMUIDisplayHelper.dp2px(context, 16)
//        binding.tabs.mode = QMUITabSegment.MODE_SCROLLABLE //MODE_SCROLLABLE 自适应宽度+滚动   MODE_FIXED  均分
//        binding.tabs.setItemSpaceInScrollMode(space)
//        binding.tabs.setupWithViewPager(binding.qmViewPager, false)
//        binding.tabs.setPadding(space, 0, space, 0)
//

        val builder = binding!!.tabs.tabBuilder()
        builder
                .skinChangeWithTintColor(false)
                .setSelectColor(Color.parseColor("#2E4E71"))
                .setNormalColor(Color.parseColor("#666666"))
                .setTypeface(Typeface.DEFAULT_BOLD, Typeface.DEFAULT)
                .setDynamicChangeIconColor(false)

        val tabs = ArrayList<QMUITab>()
        for (cat in cats) {
            val item = builder
                    .setText(cat)
                    .build(context)

            tabs.add(item)
        }
        tabs.forEach {
            binding!!.tabs.addTab(it)
        }
        binding!!.tabs.setupWithViewPager(binding!!.qmViewPager, false)


        binding!!.tabs.addOnTabSelectedListener(object : QMUITabSegment.OnTabSelectedListener {
            //mTabSegment选项被选中的监听
            /**
             * 当某个 Tab 被选中时会触发
             *
             * @param index 被选中的 Tab 下标
             */
            override fun onTabSelected(index: Int) {
                type_id=info.car_type[index].type_id

                RxBus.get().post(ConfigRx.CHOSE_CAR, info.car_type[index].weight)

            }

            /**
             * 当某个 Tab 被取消选中时会触发
             *
             * @param index 被取消选中的 Tab 下标
             */
            override fun onTabUnselected(index: Int) {}

            /**
             * 当某个 Tab 处于被选中状态下再次被点击时会触发
             *
             * @param index 被再次点击的 Tab 下标
             */
            override fun onTabReselected(index: Int) {
                type_id=info.car_type[index].type_id

                RxBus.get().post(ConfigRx.CHOSE_CAR, info.car_type[index].weight)

            }

            /**
             * 当某个 Tab 被双击时会触发
             *
             * @param index 被双击的 Tab 下标
             */
            override fun onDoubleTap(index: Int) {}
        })

    }


    fun getTvCity(): TextView? {
        return binding!!.tvCity
    }

    fun getTvChoseTime(): TextView? {
        return binding!!.tvChoseTime
    }

    fun getLlChoseAddress(): LinearLayout? {
        return binding!!.llChoseAddress
    }

    fun getLlChoseMap(): LinearLayout? {
        return binding!!.llChoseMap
    }

    fun getEtName(): EditText? {
        return binding!!.etName
    }

    fun getEtPhone(): EditText? {
        return binding!!.etPhone
    }

    fun getEtDetailedAddress(): EditText? {
        return binding!!.etDetailedAddress
    }

    fun getTabs(): QMUITabSegment?{
        return binding!!.tabs
    }

    fun getImgNext(): ImageView?{
        return binding!!.imgNext
    }
    fun getImgUp(): ImageView?{
        return binding!!.imgUp
    }
    fun getImgDown(): ImageView?{
        return binding!!.imgDown
    }
    fun getQmViewPager(): QMUIViewPager?{
        return binding!!.qmViewPager
    }


}
