package com.khmer9.yuki.screen.report

import android.annotation.SuppressLint
import android.media.MediaPlayer
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.ConcatAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.khmer9.yuki.R
import com.khmer9.yuki.adapter.*
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.remote.main.SocketClientTimeDataModel
import com.khmer9.yuki.screen.detail.BettingDetailFragment
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import kotlinx.android.synthetic.main.fragment_report.*

/**
 * A simple [Fragment] subclass as the second destination in the navigation.
 */
class StatementFragment : BaseFragment(R.layout.fragment_report) {
    private val shareViewModel = App.injectShareViewModel()
    lateinit var adapterTb1: BettingStatementAdapter
    lateinit var adapterTb1Header: BettingStatementHeaderAdapter
    lateinit var adapterTb2: WithdrawStatementAdapter
    lateinit var adapterTb2Header: WithdrawStatementHeaderAdapter
    lateinit var adapterTb3: DepositStatementAdapter
    lateinit var adapterTb3Header: DepositStatementHeaderAdapter
    lateinit var adapterTb4: ResultStatementAdapter
    lateinit var adapterTb4Header: ResultStatementHeaderAdapter
    lateinit var rvTab1: RecyclerView //betting statement
    lateinit var user: User
    lateinit var rvTab2: RecyclerView //deposit statement
    lateinit var rvTab3: RecyclerView //withdraw statement
    lateinit var rvTab4: RecyclerView //result statement

    private lateinit var betTouch: MediaPlayer
    private lateinit var moshi: Moshi

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        (requireActivity() as ShareActivity).apply {
            betTouch = MediaPlayer.create(this, R.raw.bettouch)
            //user = userShare
            user = loginData.user!!
        }
        moshi = Moshi.Builder().build()
    }

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

        rvTab1 = view.findViewById(R.id.rv_tb1)
        rvTab2 = view.findViewById(R.id.rv_tb2)
        rvTab3 = view.findViewById(R.id.rv_tb3)
        rvTab4 = view.findViewById(R.id.rv_tb4)

        (requireActivity() as? ShareActivity)?.apply {

            groupToolbarIcons.isVisible = false

            view.findViewById<ImageView>(R.id.btn_close).setOnClickListener {
                if (!sharePref.getIsMute())
                    betTouch.start()

                it.btnClick().subscribe {
                    onBackPressed()
                }
            }

            refreshImageView.setOnClickListener {
                shareViewModel.remoteReportBettingStatement()
                shareViewModel.remoteReportWithdrawStatement()
                shareViewModel.remoteReportDepositStatement()
                shareViewModel.remoteReportResultStatement()
            }
        }

        //load data...
        shareViewModel.remoteReportBettingStatement()
        shareViewModel.remoteReportWithdrawStatement()
        shareViewModel.remoteReportDepositStatement()
        shareViewModel.remoteReportResultStatement()

        shareViewModel.getReportState.observe(viewLifecycleOwner, Observer {
            getReport(it)
        })

        shareViewModel.getClientTime.observe(viewLifecycleOwner, Observer {
            if (!it.isNullOrEmpty()) {
                val adapter: JsonAdapter<SocketClientTimeDataModel> = moshi.adapter(
                    SocketClientTimeDataModel::class.java
                )
                val socketClientTimeResponse = adapter.fromJson(it)
                if (socketClientTimeResponse != null) {
                    getMessage(socketClientTimeResponse)
                }
            }
        })

        v_tab1.setOnClickListener {
            bg_select_tab1.visibility = View.VISIBLE
            bg_select_tab2.visibility = View.INVISIBLE
            bg_select_tab3.visibility = View.INVISIBLE
            bg_select_tab4.visibility = View.INVISIBLE

            rvTab1.visibility = View.VISIBLE
            rvTab2.visibility = View.INVISIBLE
            tab3.visibility = View.INVISIBLE
            rvTab4.visibility = View.INVISIBLE

            shareViewModel.remoteReportBettingStatement()
        }

        v_tab2.setOnClickListener {
            bg_select_tab1.visibility = View.INVISIBLE
            bg_select_tab2.visibility = View.VISIBLE
            bg_select_tab3.visibility = View.INVISIBLE
            bg_select_tab4.visibility = View.INVISIBLE

            rvTab1.visibility = View.INVISIBLE
            rvTab2.visibility = View.VISIBLE
            tab3.visibility = View.INVISIBLE
            rvTab4.visibility = View.INVISIBLE

            shareViewModel.remoteReportDepositStatement()
        }

        v_tab3.setOnClickListener {
            bg_select_tab1.visibility = View.INVISIBLE
            bg_select_tab2.visibility = View.INVISIBLE
            bg_select_tab3.visibility = View.VISIBLE
            bg_select_tab4.visibility = View.INVISIBLE

            rvTab1.visibility = View.INVISIBLE
            rvTab2.visibility = View.INVISIBLE
            tab3.visibility = View.VISIBLE
            rvTab4.visibility = View.INVISIBLE

            shareViewModel.remoteReportWithdrawStatement()
        }

        v_tab4.setOnClickListener {
            bg_select_tab1.visibility = View.INVISIBLE
            bg_select_tab2.visibility = View.INVISIBLE
            bg_select_tab3.visibility = View.INVISIBLE
            bg_select_tab4.visibility = View.VISIBLE

            rvTab1.visibility = View.INVISIBLE
            rvTab2.visibility = View.INVISIBLE
            tab3.visibility = View.INVISIBLE
            rvTab4.visibility = View.VISIBLE

            shareViewModel.remoteReportResultStatement()
        }
    }

    private fun getReport(state: ReportViewState) {
        progress.isVisible = state.isProgress

        if (state.statementList != null) {
            adapterTb1 = BettingStatementAdapter()
            adapterTb1Header = BettingStatementHeaderAdapter(state.statementListTurnOver, state.statementListAmountWinLose)

            adapterTb1.submitList(state.statementList)
            adapterTb1.detailButtonListener = { invoiceId, invoiceNo ->
                if (!invoiceId.isNullOrEmpty()) {
                    (requireActivity() as? ShareActivity)?.apply {
                        supportFragmentManager.beginTransaction()
                            .setCustomAnimations(
                                R.anim.slide_in,
                                R.anim.slide_out,
                                R.anim.slide_in_exit,
                                R.anim.slide_out_exit
                            )
                            .add(R.id.container, BettingDetailFragment().apply {
                                arguments = Bundle().apply {
                                    putString("INVOICE_ID", invoiceId)
                                    putString("no", invoiceNo)
                                }
                            })
                            .addToBackStack(null)
                            .commit()
                    }
                }
            }

            rvTab1.setHasFixedSize(true)
            rvTab1.layoutManager = LinearLayoutManager(context)
            val concatAdapter = ConcatAdapter(adapterTb1Header, adapterTb1)
            rvTab1.adapter = concatAdapter
        }

        if (state.statementWithdrawList != null) {
            adapterTb2 = WithdrawStatementAdapter()
            adapterTb2Header = WithdrawStatementHeaderAdapter(state.statementWithdrawTotalAmount)

            adapterTb2.submitList(state.statementWithdrawList)
            rvTab2.setHasFixedSize(true)
            rvTab2.layoutManager = LinearLayoutManager(context)
            val concatAdapter = ConcatAdapter(adapterTb2Header, adapterTb2)
            rvTab2.adapter = concatAdapter
        }

        if (state.statementDepositList != null) {
            adapterTb3 = DepositStatementAdapter()
            adapterTb3Header = DepositStatementHeaderAdapter(state.statementDepositTotalAmount) //WithdrawStatementHeaderAdapter(state.statementDepositTotalAmount)

            adapterTb3.submitList(state.statementDepositList)
            rvTab3.setHasFixedSize(true)
            rvTab3.layoutManager = LinearLayoutManager(context)
            val concatAdapter = ConcatAdapter(adapterTb3Header, adapterTb3)
            rvTab3.adapter = concatAdapter
        }

        if (state.statementResultList != null) {
            adapterTb4 = ResultStatementAdapter()
            adapterTb4Header = ResultStatementHeaderAdapter()

            adapterTb4.submitList(state.statementResultList)
            rvTab4.setHasFixedSize(true)
            rvTab4.layoutManager = LinearLayoutManager(context)
            val concatAdapter = ConcatAdapter(adapterTb4Header, adapterTb4)
            rvTab4.adapter = concatAdapter
        }
    }

    @SuppressLint("SetTextI18n")
    fun getMessage(lotto: SocketClientTimeDataModel) {
        if (lotto.isCountDowning() && lotto.time == 128) {
            (requireActivity() as? ShareActivity)?.apply {
                //shareViewModel.remoteReport()
            }
        }
    }
}
