package com.sambath.kunkhmer.screen.main.cflive.currentbets

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.core.view.isVisible
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.sambath.kunkhmer.R
import com.sambath.kunkhmer.app.App
import com.sambath.kunkhmer.app.ShareActivity
import com.sambath.kunkhmer.base.BaseFragment
import com.sambath.kunkhmer.remote.User
import com.sambath.kunkhmer.util.PrefHelper
import kotlinx.android.synthetic.*
import kotlinx.android.synthetic.main.fragment_current_bets.*

class CurrentBetsFragment : BaseFragment(R.layout.fragment_current_bets) {
    private val shareViewModel = App.injectShareViewModel()
    private lateinit var adapter: CurrentBetsAdapter
    private lateinit var user: User
    private lateinit var sharePref: PrefHelper
    private lateinit var currency: String
    private lateinit var currentBetsViewModel: CurrentBetsViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        sharePref = App.injectPrefHelper()
        (requireActivity() as ShareActivity).apply {
            user = userShare
        }
        currentBetsViewModel = CurrentBetsViewModel(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)
        currentBetsViewModel.getCurrentBetsData()
        adapter = CurrentBetsAdapter()
        val layoutBlueManager = LinearLayoutManager(requireActivity() as? ShareActivity)
        rv_current_bet.layoutManager = layoutBlueManager
        rv_current_bet.itemAnimator = DefaultItemAnimator()
        rv_current_bet.adapter = adapter
        adapter.printButtonListener = { fightNo, betDate, betType, amount, payout, payoutPrint, id ->
        }
        //label_amount.text = "Amount ($currency)"
        currentBetsViewModel.getCurrentBetsData()
        currentBetsViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
            getCurrentBets(it)
        })
        shareViewModel.getUpdateFlagMain.observe(viewLifecycleOwner, Observer {
            if (it) {
                currentBetsViewModel.getCurrentBetsData()
                shareViewModel.setUpdateFlagMain(false)
            }
        })
        shareViewModel.getDefinedResultAndChannel.observe(viewLifecycleOwner, Observer {
            currentBetsViewModel.getCurrentBetsData()
        })
    }

    private fun getCurrentBets(it: CurrentBetsViewState) {
        Log.d("callApi", "current report")
        progressCurrentBets.isVisible = it.isProgress
        if (it.currentBetsList != null) {
            Log.d("callApi", "currentBetsList")
            adapter.submitList(it.currentBetsList)
        }
        adapter.notifyDataSetChanged()
    }

    companion object {
        private const val ARG_SECTION_NUMBER = "section_number"

        @JvmStatic
        fun newInstance(sectionNumber: Int): CurrentBetsFragment {
            return CurrentBetsFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_SECTION_NUMBER, sectionNumber)
                }
            }
        }
    }

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