package com.pan_american.android.ui.customer_resource.market_sales_revenue

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.pan_american.android.OASystem
import com.pan_american.android.R
import com.pan_american.android.base.BaseActivity
import com.pan_american.android.data.model.common.adapter.ViewPager2Adapter
import com.pan_american.android.data.model.customer_resource.market_sales_revenue.network.MarketSalesRevenueBaseRequest
import com.pan_american.android.data.model.customer_resource.market_sales_revenue.network.MarketSalesRevenueBaseResponse
import com.pan_american.android.data.network.APIService
import com.pan_american.android.data.network.ServiceCreator
import com.pan_american.android.databinding.ActivityMarketSalesRevenueBinding
import com.pan_american.android.databinding.LayoutTitleBinding
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MarketSalesRevenueActivity : BaseActivity<ActivityMarketSalesRevenueBinding>() {

    private lateinit var titleBinding: LayoutTitleBinding

    private val apiService = ServiceCreator.create<APIService>()

    override fun getViewBinding() = ActivityMarketSalesRevenueBinding.inflate(layoutInflater)

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

        initTitle()

        getBaseDataSource()
    }

    override fun initTitle() {
        titleBinding = LayoutTitleBinding.bind(binding.root).apply {
            titleText.text = resources.getString(R.string.market_sales_revenue)

            backButton.setOnClickListener {
                back()
            }
        }
    }

    override fun initViews() {

        val fragmentList: MutableList<Fragment> = ArrayList()
        fragmentList.add(SalesRevenueTotalFragment())
        fragmentList.add(SalesRevenueRankFragment())
        fragmentList.add(SalesRevenueListFragment())
        binding.salesRevenueContainer.adapter = ViewPager2Adapter(this, fragmentList)
        binding.salesRevenueContainer.isUserInputEnabled = false
        binding.salesRevenueContainer.currentItem = 0
        binding.salesRevenueContainer.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                when(position) {
                    0 -> {
                        binding.total.isChecked = true
                    }

                    1 -> {
                        binding.rank.isChecked = true
                    }

                    2 -> {
                        binding.groupList.isChecked = true
                    }
                }
            }
        })
        //当导航点击切换时，viewpager2也跟着切换界面
        binding.marketSalesRevenueSwitch.setOnCheckedChangeListener { radioGroup, i ->
            if (radioGroup.id == binding.marketSalesRevenueSwitch.id) {
                when(i) {
                    binding.total.id -> {
                        binding.salesRevenueContainer.currentItem = 0
                    }

                    binding.rank.id -> {
                        binding.salesRevenueContainer.currentItem = 1
                    }

                    binding.groupList.id -> {
                        binding.salesRevenueContainer.currentItem = 2
                    }
                }
            }
        }
    }

    private fun getBaseDataSource() {
        apiService.getMarketSalesRevenueBaseData(MarketSalesRevenueBaseRequest()).enqueue(object : Callback<MarketSalesRevenueBaseResponse> {
            override fun onResponse(
                marketSalesRevenueBaseRequest: Call<MarketSalesRevenueBaseResponse>,
                response: Response<MarketSalesRevenueBaseResponse>
            ) {
                val baseDataResponse = response.body()

                if (baseDataResponse != null) {
                    if (baseDataResponse.code == 200) {

                        OASystem.marketSalesRevenueCompanyList.clear()
                        OASystem.marketSalesRevenueUserList.clear()
                        OASystem.marketSalesRevenueDateList.clear()

                        OASystem.marketSalesRevenueCompanyList = baseDataResponse.data.companyData
                        OASystem.marketSalesRevenueUserList = baseDataResponse.data.userData
                        OASystem.marketSalesRevenueDateList = baseDataResponse.data.dtData

                        initViews()

                    } else {
                        showMessage(baseDataResponse.msg)
                    }
                }
            }

            override fun onFailure(p0: Call<MarketSalesRevenueBaseResponse>, p1: Throwable) {
                showErrorInfo(R.string.base_resource_data_get_failed)
            }
        })
    }
}