package com.sambath.klaklouk_main.screen.main.cflive.todayreport

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.klaklouk_main.R
import com.sambath.klaklouk_main.app.App
import com.sambath.klaklouk_main.app.ShareActivity
import com.sambath.klaklouk_main.base.BaseFragment
import kotlinx.android.synthetic.main.fragment_today_report.*

class TodayReportFragment : BaseFragment(R.layout.fragment_today_report) {
    private val shareViewModel = App.injectShareViewModel()
    private lateinit var adapter: TodayReportAdapter
    private lateinit var todayReportViewModel: TodayReportViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        todayReportViewModel = TodayReportViewModel(App.injectApiService(), App.injectPrefHelper())
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        adapter = TodayReportAdapter()
        val layoutBlueManager = LinearLayoutManager(requireActivity() as? ShareActivity)
        rv_today_report.layoutManager = layoutBlueManager
        rv_today_report.itemAnimator = DefaultItemAnimator()
        rv_today_report.adapter = adapter
        todayReportViewModel.getTodayReportData()
        (requireActivity() as ShareActivity).apply {
            refreshImageView.setOnClickListener {
                todayReportViewModel.getTodayReportData()
            }
        }
        todayReportViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
//            og.d("dataxzsd", "data live")L
            getTodayReport(it)
        })
        shareViewModel.getUpdateFlagMain.observe(viewLifecycleOwner, Observer {
            if (it){
//                Log.d("dataxzsdup", "update data live")
                todayReportViewModel.getTodayReportData()
                shareViewModel.setUpdateFlagMain(false)
            }
        })
        shareViewModel.getViewResult.observe(viewLifecycleOwner, Observer {
             todayReportViewModel.getTodayReportData()
        })
    }
    private fun getTodayReport(state: TodayReportViewState){
        Log.d("callApi", "today report")
        progressTodayReport.isVisible = state.isProgress
        if (!state.todayReportList.isNullOrEmpty()){
            adapter.submitList(state.todayReportList)
        }
        adapter.notifyDataSetChanged()
    }
    companion object {
        private const val ARG_SECTION_NUMBER = "section_number"
        @JvmStatic
        fun newInstance(sectionNumber: Int): TodayReportFragment {
            return TodayReportFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_SECTION_NUMBER, sectionNumber)
                }
            }
        }
    }
}