package com.khmer9.lotto.screen.report

import android.annotation.SuppressLint
import android.media.MediaPlayer
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.View
import android.widget.ImageView
import androidx.core.view.isVisible
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.khmer9.lotto.R
import com.khmer9.lotto.app.App
import com.khmer9.lotto.extension.btnClick
import com.khmer9.lotto.app.ShareActivity
import com.khmer9.lotto.base.BaseFragment
import com.khmer9.lotto.remote.SocketMessageDataModel
import com.khmer9.lotto.remote.User
import com.khmer9.lotto.util.Const
import com.khmer9.lotto.util.ModelPreferencesManager
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: ReportAdapter
    lateinit var adapterTb2: ReportAdapter
    lateinit var adapterTb3: ReportAdapter

    lateinit var rvTab1: RecyclerView
    lateinit var rvTab2: RecyclerView
    lateinit var rvTab3: RecyclerView
    private lateinit var betTouch : MediaPlayer
    lateinit var user: User

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        (requireActivity() as ShareActivity).apply {
            betTouch = MediaPlayer.create(this, R.raw.bettouch)
            user = userShare

        }
    }

    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)



        (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.remoteReport(userId = user.id)
            }
        }
        shareViewModel.remoteReport(userId = user.id)

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

        })


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

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

        }

        v_tab2.setOnClickListener {
            bg_select_tab1.visibility=View.INVISIBLE
            bg_select_tab2.visibility=View.VISIBLE
            bg_select_tab3.visibility= View.INVISIBLE
            rvTab1.visibility=View.INVISIBLE
            rvTab2.visibility=View.VISIBLE
            tab3.visibility=View.INVISIBLE

        }

        v_tab3.setOnClickListener {
            bg_select_tab1.visibility=View.INVISIBLE
            bg_select_tab2.visibility=View.INVISIBLE
            bg_select_tab3.visibility= View.VISIBLE
            rvTab1.visibility=View.INVISIBLE
            rvTab2.visibility=View.INVISIBLE
            tab3.visibility=View.VISIBLE


        }

    }

    fun getReport(state: ReportViewState){

        progress.isVisible = state.isProgress

        if (!state.winList.isNullOrEmpty()){
            adapterTb1 = ReportAdapter(Type.WIN)
            adapterTb1.submitList(state.winList)
            rvTab1.setHasFixedSize(true)
            rvTab1.layoutManager = LinearLayoutManager(context)
            rvTab1.adapter = adapterTb1

            adapterTb1.printButtonListener={
                    ticketID, gameID, betDate, betType, betCast, winLose->
                //val item = adapter.currentList[index] as ListItem
                printSomePrintable(user.userName,ticketID, gameID, betDate, betType, betCast, winLose)
            }

        }

        if (!state.winLostList.isNullOrEmpty()){
            adapterTb2 = ReportAdapter(Type.WIN_LOST)
            adapterTb2.submitList(state.winLostList)
            rvTab2.setHasFixedSize(true)
            rvTab2.layoutManager = LinearLayoutManager(context)
            rvTab2.adapter = adapterTb2

            adapterTb2.printButtonListener={
                    ticketID, gameID, betDate, betType, betCast, winLose->
                //val item = adapter.currentList[index] as ListItem
                printSomePrintable(user.userName,ticketID, gameID, betDate, betType, betCast, winLose)
            }


        }
        if (!state.statementList.isNullOrEmpty()){
            adapterTb3 = ReportAdapter(Type.STATEMENT)
            adapterTb3.submitList(state.statementList)
            rvTab3.setHasFixedSize(true)
            rvTab3.layoutManager = LinearLayoutManager(context)
            rvTab3.adapter = adapterTb3


        }

    }
    @SuppressLint("SetTextI18n")
    fun getMessage(loto: SocketMessageDataModel){

        if (loto.isCountDowning() && loto.countDown ==128){
            (requireActivity() as? ShareActivity)?.apply {
                shareViewModel.remoteReport(userId = user.id)

            }
        }

    }
}
