package com.keyboard.journey

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import com.gyf.immersionbar.ktx.immersionBar
import com.keyboard.journey.adapter.CategoryDetailsDataAdapter
import com.keyboard.journey.bean.CategoryDataBean
import com.keyboard.journey.bean.ItemDataBean
import com.keyboard.journey.bean.MainDataBean
import com.keyboard.journey.databinding.JourneyCategoryDetailsActivityBinding
import com.keyboard.journey.topon.AdManager
import com.keyboard.journey.util.NetworkCallback
import com.keyboard.journey.util.NetworkUtil
import org.libpag.PAGFile

class JourneyCategoryDetailsActivity : AppCompatActivity() {


    companion object {
        const val CATEGORY_DETAILS_BEAN_KEY = "category_details_bean_key"
    }

    private lateinit var binding: JourneyCategoryDetailsActivityBinding
    private var offset = 0
    private var pageSize = 20

    private var bean: MainDataBean? = null

    private var adapter: CategoryDetailsDataAdapter? = null
    private var contentBeans: MutableList<ItemDataBean> = mutableListOf()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = JourneyCategoryDetailsActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
        AdManager.showAD(this@JourneyCategoryDetailsActivity){

        }
        bean = intent.getSerializableExtra(CATEGORY_DETAILS_BEAN_KEY) as MainDataBean?
        if (bean == null) {
            finish()
        }
        initBar()
        initView()
        initData()


    }

    private fun initBar() {
        immersionBar {
            statusBarDarkFont(true)
            statusBarView(binding.view)
        }
    }

    private fun initView() {
        binding.titleTv.text = bean?.title
        binding.backBtn.setOnClickListener { finish() }

        binding.refreshLayout.setEnableRefresh(false)
        binding.refreshLayout.setOnLoadMoreListener {
            refreshData()
        }
        adapter = CategoryDetailsDataAdapter(this, contentBeans)
        binding.rv.layoutManager = GridLayoutManager(this, 2)
        binding.rv.adapter = adapter
    }

    private fun initData() {
        loadingPlay()
        offset = 0
        NetworkUtil().fetchCategory(
            bean?.key!!,
            offset,
            pageSize,
            object : NetworkCallback<List<CategoryDataBean>> {
                @SuppressLint("NotifyDataSetChanged")
                override fun onSuccess(data: List<CategoryDataBean>) {
                    contentBeans.clear()
                    contentBeans.addAll(data[0].items)
                    runOnUiThread {
                        if (!isFinishing) {
                            offset += pageSize
                            loadingClose()
                            adapter?.notifyDataSetChanged()
                        }
                    }
                }

                override fun onFailure(errorMessage: String) {

                }
            })
    }

    private fun refreshData() {
        NetworkUtil().fetchCategory(
            bean?.key!!,
            offset,
            pageSize,
            object : NetworkCallback<List<CategoryDataBean>> {
                @SuppressLint("NotifyDataSetChanged")
                override fun onSuccess(data: List<CategoryDataBean>) {
                    contentBeans.addAll(data[0].items)
                    runOnUiThread {
                        if (!isFinishing) {
                            binding.refreshLayout.finishLoadMore()
                            val startPosition = contentBeans.size
                            contentBeans.addAll(data[0].items)
                            offset += pageSize
                            adapter?.notifyItemRangeInserted(startPosition, data[0].items.size)
                        }
                    }
                }

                override fun onFailure(errorMessage: String) {

                }
            })
    }

    private fun loadingPlay(){
        binding.loadingLayout.visibility = View.VISIBLE
        val pagFile = PAGFile.Load(assets, "loading.pag")
        binding.pagView.composition = pagFile
        binding.pagView.setRepeatCount(0)
        binding.pagView.play()
    }

    private fun loadingClose(){
        binding.loadingLayout.visibility = View.GONE
        if(binding.pagView.isPlaying){
            binding.pagView.stop()
        }
    }
}