package com.keyboard.craft.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import com.keyboard.craft.ad.MaxManager
import com.keyboard.craft.adapter.KeyDetailsDataAdapter
import com.keyboard.craft.bean.CategoryDataBean
import com.keyboard.craft.bean.ItemDataBean
import com.keyboard.craft.bean.MainDataBean
import com.keyboard.craft.databinding.UiCategoryDetailsActivityBinding
import com.keyboard.craft.util.NetworkCallback
import com.keyboard.craft.util.NetworkUtil
import com.keyboard.craft.util.UIHelper

class CategoryDetailsActivity : AppCompatActivity() {


    companion object {
        const val CATEGORY_DETAILS_BEAN_KEY = "category_details_bean_key"
    }

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

    private var bean: MainDataBean? = null

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

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = UiCategoryDetailsActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
        MaxManager.onLoadAd()
        MaxManager.startShowMaxAd (this){  }
        bean = intent.getSerializableExtra(CATEGORY_DETAILS_BEAN_KEY) as MainDataBean?
        if (bean == null) {
            finish()
        }
        initBar()
        initView()
        initData()
        MaxManager.onLoadAd()
    }

    private fun initBar() {
        UIHelper.setupStatusBar(this, true, binding.view)
    }

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

            MaxManager.startShowMaxAd (this@CategoryDetailsActivity){
                finish()
            }

        }

        binding.refreshLayout.setEnableRefresh(false)
        binding.refreshLayout.setOnLoadMoreListener {
            refreshData()
        }
        adapter = KeyDetailsDataAdapter(this, contentBeans, "category")
        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
    }

    private fun loadingClose() {
        binding.loadingLayout.visibility = View.GONE
    }
}