package com.quansu.heifengwuliu.fragment

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.annotation.Tag
import com.quansu.heifengwuliu.R
import com.quansu.heifengwuliu.activity.AddressActivity
import com.quansu.heifengwuliu.adapter.FragmentAdapter
import com.quansu.heifengwuliu.base.MBFragment
import com.quansu.heifengwuliu.config.ConfigRx
import com.quansu.heifengwuliu.config.MIntentAction
import com.quansu.heifengwuliu.databinding.FragmentOwnersingleBinding
import com.quansu.heifengwuliu.utils.AList
import com.quansu.heifengwuliu.utils.TAB
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import com.ysnows.base.base.BRepository
import com.ysnows.base.base.BViewModel
import com.ysnows.base.ccretrofit.CCRetrofit
import com.ysnows.base.model.PositionBean
import com.ysnows.base.route.IGaode
import com.ysnows.base.utils.B
import com.ysnows.base.utils.UiSwitch
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView

/**
 * 一装一卸
 *Created by shihuiyun
 *on 2020/9/11
 */
class OwnerSingleFragment : MBFragment<BViewModel<BRepository>, FragmentOwnersingleBinding>(){
    var cats= emptyArray<String>()
    private var outtabs = 0
    private var selectViewpage = 0
    private var type=1


    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        setIndicator()
        binding.viewPager.offscreenPageLimit = 0
        initViewPager()
        initData()

    }

    private fun initData() {

        //卸货信息
         binding.viewEnter.tvCity.setHint("请选择卸货地址")
         binding.viewEnter.llChoseMap.setOnClickListener {
            //地图选点
             type=2
             choseMap()
        }


        binding.viewOut.llChoseMap.setOnClickListener {
            //地图选点
            type = 1
            choseMap()
        }
        binding.viewOut.llChoseAddress.setOnClickListener {
            //地址薄 1
            UiSwitch.bundleRes(this, AddressActivity::class.java,
                    B.with().putString("type", "1").ok(), MIntentAction.REQUEST_CODE_ONE)
        }
        binding.viewEnter.llChoseAddress.setOnClickListener {
            //地址薄 2
            UiSwitch.bundleRes(this, AddressActivity::class.java,
                    B.with().putString("type", "2").ok(), MIntentAction.REQUEST_CODE_ONE)
        }



    }


    private fun choseMap(){

        AndPermission.with(repository().context)
                .runtime()
                .permission(Permission.ACCESS_COARSE_LOCATION,
                        Permission.ACCESS_FINE_LOCATION,
                        Permission.WRITE_EXTERNAL_STORAGE,
                        Permission.READ_EXTERNAL_STORAGE,
                        Permission.READ_PHONE_STATE
                )
                .onGranted { data ->
                    CCRetrofit.instance()
                            .with(context)
                            .create(IGaode::class.java)
                            .selectLocation()
                            .subscribe()

                }
                .start()

    }

    private fun initViewPager() {
        val tabs: AList<TAB> = AList()
        for (cat in cats) {

            var ownerImgFragment=OwnerImgFragment();

            tabs.add(TAB(cat, ownerImgFragment,  B.with().putString("img","12344").ok()))

        }
        outtabs = tabs.ok().size- 1
        val fragmentAdapter = FragmentAdapter(childFragmentManager, tabs.ok())
        binding.viewPager.adapter = fragmentAdapter

    }

    private fun setIndicator() {
        cats=arrayOf("1","2","3","4","5","6","11","23","18","30","33","100","101","1003")

        val commonNavigator = CommonNavigator(context)
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return if (cats == null) 0 else cats.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val colorTransitionPagerTitleView = ColorTransitionPagerTitleView(context)
                colorTransitionPagerTitleView.normalColor = context.resources.getColor(R.color.tv_subtitle)
                colorTransitionPagerTitleView.selectedColor = context.resources.getColor(R.color.tv_title)
                colorTransitionPagerTitleView.text = cats.get(index)
                colorTransitionPagerTitleView.setOnClickListener { binding.viewPager.setCurrentItem(index) }
                return colorTransitionPagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator {
                val indicator = LinePagerIndicator(context)
                indicator.mode = LinePagerIndicator.MODE_EXACTLY
                indicator.lineHeight = UIUtil.dip2px(context, 3.0).toFloat()
                indicator.lineWidth = UIUtil.dip2px(context, 40.0).toFloat()
                indicator.roundRadius = UIUtil.dip2px(context, 2.0).toFloat()
                indicator.startInterpolator = AccelerateInterpolator()
                indicator.endInterpolator = DecelerateInterpolator(2.0f)
                indicator.setColors(context.resources.getColor(R.color.bg_button_red))
                return indicator
            }
        }
        binding.slIndicator.navigator = commonNavigator

        ViewPagerHelper.bind( binding.slIndicator, binding.viewPager)

        binding.imgNext.setOnClickListener{
            selectViewpage =  binding.viewPager.getCurrentItem()
            if (outtabs != 0) {
                if (selectViewpage < outtabs) {
                    binding.viewPager.setCurrentItem(selectViewpage + 1)
                }
            }
        }
        //向右
        binding.imgDown.setOnClickListener {
            selectViewpage =  binding.viewPager.getCurrentItem()
            if (outtabs != 0) {
                if (selectViewpage < outtabs) {
                    binding.viewPager.setCurrentItem(selectViewpage + 1)
                }
            }
        }

        //向左
        binding.imgUp.setOnClickListener {
            selectViewpage =  binding.viewPager.getCurrentItem()
            if (outtabs != 0) {
                if (selectViewpage < outtabs) {
                    binding.viewPager.setCurrentItem(selectViewpage -1)
                }
            }
        }

    }

    override fun binding(inflater: LayoutInflater, container: ViewGroup?): FragmentOwnersingleBinding {
       return FragmentOwnersingleBinding.inflate(layoutInflater)
    }

    override fun title(): String? {
        return null
    }

    override val isRxbus: Boolean
        get() = true


    @Subscribe(tags = [Tag(ConfigRx.ADD_POSITION)])
    fun getPosition(bean: PositionBean) {

        var provincename=bean.provincename
        var latitude=bean.latitude
        var longitude=bean.longitude
        if(type==2) binding.viewEnter.etDetailedAddress.setText(bean.position)  else binding.viewOut.etDetailedAddress.setText(bean.position)



    }
}
