package com.khmer9.yuki.screen.detail

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.khmer9.yuki.R
import com.khmer9.yuki.app.App
import com.khmer9.yuki.app.ShareActivity
import com.khmer9.yuki.base.BaseFragment
import com.khmer9.yuki.extension.btnClick
import com.khmer9.yuki.remote.User
import com.khmer9.yuki.util.PrefHelper
import kotlinx.android.synthetic.*
import kotlinx.android.synthetic.main.betting_detail.*

class BettingDetailFragment : BaseFragment(R.layout.betting_detail) {
    private lateinit var adapter: BetsDetailAdapter
    private lateinit var user: User
    private lateinit var sharePref: PrefHelper
    private lateinit var currency: String
    private lateinit var betsDetailViewModel: BetsDetailViewModel
    private val ARG_PARAM1 = "INVOICE_ID"
    private val ARG_PARAM2 = "no"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        sharePref = App.injectPrefHelper()
        (requireActivity() as ShareActivity).apply {
            user = loginData.user!!
        }

        betsDetailViewModel = BetsDetailViewModel(App.injectApiService(), App.injectPrefHelper())
        when (user.currencyType) {
            1 -> {//dollar
                currency = "$"
            }
            2 -> {//riel
                currency = "៛"
            }
            3 -> {//baht
                currency = "฿"
            }
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        btnClose.setOnClickListener {
            it.btnClick().subscribe {
                (requireActivity() as? ShareActivity)?.apply {
                    onBackPressed()
                }
            }
        }

        requireArguments().getString(ARG_PARAM1)?.let {
            betsDetailViewModel.getBetsDetailData(it)
            //mtxtInvoiceNo.text = " #$it"
        }

        requireArguments().getString(ARG_PARAM2)?.let {
            mtxtInvoiceNo.text = " #$it"
        }


        adapter = BetsDetailAdapter()
        val layoutBlueManager = LinearLayoutManager(requireActivity() as? ShareActivity)
        rv_current_bet.layoutManager = layoutBlueManager
        rv_current_bet.itemAnimator = DefaultItemAnimator()
        rv_current_bet.adapter = adapter
        label_amount.text = resources.getString(R.string.amount) + " ($currency)"
        betsDetailViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
            getCurrentBets(it)
        })
    }

    private fun getCurrentBets(it: BetsDetailViewState) {
        if (it.betDetailData?.bettingDetailList != null) {
            try {
                when (user.currencyType) {
                    1 -> {//dollar
                        tv_turn_over.text = "${it.betDetailData?.totalCast} $"
                        tv_total_win_lose.text = "${currencyFormat(it.betDetailData?.totalAmountWin!!)} $"
                    }
                    2 -> {//riel
                        tv_turn_over.text = "${it.betDetailData?.totalCast} ៛"
                        tv_total_win_lose.text = "${currencyFormat(it.betDetailData?.totalAmountWin!!)} ៛"
                    }
                    3 -> {//baht
                        tv_turn_over.text = "${it.betDetailData?.totalCast} ฿"
                        tv_total_win_lose.text = "${currencyFormat(it.betDetailData?.totalAmountWin!!)} ฿"
                    }
                }
            } catch (e: Exception) {
            }

            try {
                if (it.betDetailData?.totalAmountWin!! > 0) {
                    tv_turn_over.setTextColor(Color.parseColor("#303F9F"))
                    tv_total_win_lose.setTextColor(Color.parseColor("#303F9F"))
                } else {
                    tv_turn_over.setTextColor(Color.parseColor("#C62828"))
                    tv_total_win_lose.setTextColor(Color.parseColor("#C62828"))
                }
            } catch (e: Exception) {
                tv_turn_over.setTextColor(Color.parseColor("#303F9F"))
                tv_total_win_lose.setTextColor(Color.parseColor("#303F9F"))
            }

            adapter.submitList(it.betDetailData?.bettingDetailList)
        }
        adapter.notifyDataSetChanged()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        this.clearFindViewByIdCache()
    }
}