package com.quansu.heifengwuliu.activity

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.quansu.heifengwuliu.adapter.FragAdapter
import com.quansu.heifengwuliu.base.MBActivity
import com.quansu.heifengwuliu.databinding.ActivityAddressBinding
import com.quansu.heifengwuliu.fragment.AddressFragment
import com.ysnows.base.base.BRepository
import com.ysnows.base.base.BViewModel
import com.ysnows.base.utils.B

/**
 *Created by shihuiyun
 *on 2020/9/11
 */
class AddressActivity : MBActivity<BViewModel<BRepository>, ActivityAddressBinding>() {
    private var adapter: FragAdapter? = null
    var list = emptyArray<Fragment>()
    var type = "1"//1发货 2卸货
    var isShow = "1"//0不显示 1显示

    override fun __before() {
        super.__before()
        var bundle = intent.extras
        if (null != bundle) {
            type = bundle.getString("type","1")
            isShow = bundle.getString("isShow","1")
        }

    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initViewPager()
    }

    private fun initViewPager() {

        val billOneFragment = AddressFragment()
        billOneFragment.arguments = B.with().putString("type", "1")
                .putString("isShow", isShow)
                .ok()
        val billTwoFragment = AddressFragment()
        billTwoFragment.arguments = B.with().putString("type", "2")
                .putString("isShow", isShow)
                .ok()

        list = arrayOf(
                billOneFragment,
                billTwoFragment)

        adapter = FragAdapter(supportFragmentManager, list.toMutableList())
        binding.viewPager.offscreenPageLimit = 2
        binding.viewPager.adapter = adapter
        binding.viewPager.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
            override fun onPageSelected(position: Int) {
                choseType(position)
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })

        binding.llOne.setOnClickListener(View.OnClickListener { view: View? -> binding.viewPager.setCurrentItem(0) })
        binding.llTwo.setOnClickListener(View.OnClickListener { view: View? -> binding.viewPager.setCurrentItem(1) })


        if (type == "2") {
            binding.viewPager.currentItem = 1
        }


    }

    private fun choseType(position: Int) {
        when (position) {
            0 -> {
                binding.tvOne.setTextColor(Color.parseColor("#4B3200"))
                binding.tvTwo.setTextColor(Color.parseColor("#5D5E5F"))
                binding.lineOne.setVisibility(View.VISIBLE)
                binding.lineTwo.setVisibility(View.INVISIBLE)

            }
            1 -> {
                binding.tvTwo.setTextColor(Color.parseColor("#4B3200"))
                binding.tvOne.setTextColor(Color.parseColor("#5D5E5F"))
                binding.lineTwo.setVisibility(View.VISIBLE)
                binding.lineOne.setVisibility(View.INVISIBLE)

            }
            else -> {
            }
        }
    }

    override fun listeners() {
        super.listeners()

        binding.imgBack.setOnClickListener {
            finish()
        }


    }

    override fun binding(): ActivityAddressBinding {
        return ActivityAddressBinding.inflate(layoutInflater)

    }

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