package com.air.airhornprank.activity;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.GridLayoutManager;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.air.airhornprank.R;
import com.air.airhornprank.adapter.ListAdapter;
import com.air.airhornprank.data.MyDataBase;
import com.air.airhornprank.data.Mydata;
import com.air.airhornprank.databinding.ActivityListBinding;
import com.air.airhornprank.tool.Mytools;
import com.air.airhornprank.tool.StaticValue;
import com.air.airhornprank.topon.AdManager;
import com.air.airhornprank.topon.onActionListener;

import java.util.ArrayList;
import java.util.List;

public class ListActivity extends AppCompatActivity {
    ActivityListBinding binding;
    private List<Mydata> allData = new ArrayList<>();
    private ListAdapter thumAdapter;
    private int color;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityListBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        AdManager.loadAllAd();

        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.activity_list), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initData();

        binding.listBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AdManager.showTopOn(ListActivity.this, new onActionListener() {
                    @Override
                    public void onAction() {
                        finish();
                    }
                });
            }
        });

    }

    private void initData() {
        Intent intent = getIntent();
        color = intent.getIntExtra(StaticValue.KEY_Color, 0);
        String name = intent.getStringExtra(StaticValue.KEY_CategoryName);
        binding.preListTitle.setText(name);
        Mytools.runIO(new Runnable() {
            @Override
            public void run() {
                allData = MyDataBase.getInstance().likeDAO().getlist(name);
                if (!allData.isEmpty()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            initView(allData);
                        }
                    });
                }
            }
        });
    }

    private void initView(List<Mydata> allData) {
        // 调用 AdManager 中的 showTopOn 方法来展示广告
        AdManager.showTopOn(this, new onActionListener() {
            @Override
            public void onAction() {
                // 在广告展示完成后初始化 RecyclerView
                thumAdapter = new ListAdapter(ListActivity.this, allData, color);
                binding.preListRv.setAdapter(thumAdapter);
                binding.preListRv.setLayoutManager(new GridLayoutManager(ListActivity.this, 3));
            }
        });
    }

    @Override
    public void onBackPressed() {
        // 在按下系统返回键时调用 AdManager 的 showTopOn 方法
        super.onBackPressed();
        AdManager.showTopOn(this, new onActionListener() {
            @Override
            public void onAction() {
                ListActivity.super.onBackPressed();
            }
        });
    }


}