package com.apps.pixwall.view

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity

import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import com.ad.maxlibrary.MaxManager
import com.apps.pixwall.adapter.DoubleLineAdapter

import com.apps.pixwall.data.MainViewModel
import com.apps.pixwall.data.Paper
import com.apps.pixwall.databinding.ActivityMorePaperBinding
import com.apps.pixwall.listener.MainInListener

class MorePaperActivity : AppCompatActivity(),MainInListener {
    private lateinit var binding:ActivityMorePaperBinding
    private lateinit var viewModel: MainViewModel
    private lateinit var adapter:DoubleLineAdapter

    companion object{
        var titleName:String="name"
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding=ActivityMorePaperBinding.inflate(layoutInflater)
        init()
        setContentView(binding.root)
        MaxManager.onLoadAd()
        MaxManager.startShowMaxAd(this@MorePaperActivity){

        }
    }

    private fun init(){
        viewModel=ViewModelProvider(this)[MainViewModel::class.java]
        titleName= intent.getStringExtra(titleName)!!
        binding.name.text= titleName
        binding.searchPb.visibility= View.VISIBLE
        binding.imageBack.setOnClickListener { finish() }
        adapter=DoubleLineAdapter()
        adapter.setListener(this)
        binding.list.layoutManager=GridLayoutManager(this,2)
        binding.list.adapter=adapter
        viewModel.categoryList.observe(this) { list ->
            // 在这里处理返回的数据
            if(list.isNotEmpty()){
                updateUI(list)
            }
        }
        if(viewModel.categoryList.value.isNullOrEmpty()){
            Log.d("Therererer","startGet")
            viewModel.getCategoryList(titleName)
        }
    }
    private fun updateUI(list: List<Paper>){
        Log.d("MorePaperActivity","list size is"+list.size.toString())
        adapter.setList(list)
        binding.searchPb.visibility= View.GONE
        adapter.notifyDataSetChanged()
    }

    override fun inItemClick(paper: Paper) {
        val intent= Intent(this,PaperActivity::class.java)
        intent.putExtra(PaperActivity.paperKey,paper)
        startActivity(intent)
    }
}