package com.fun.originalityprank.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.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;

import com.fun.originalityprank.R;
import com.fun.originalityprank.adapter.ListAdapter;
import com.fun.originalityprank.adapter.ThumAdapter;
import com.fun.originalityprank.data.AllData;
import com.fun.originalityprank.data.MydataBase;
import com.fun.originalityprank.databinding.ActivityListBinding;
import com.fun.originalityprank.tool.Mytools;
import com.fun.originalityprank.tool.StaticValue;

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

public class ListActivity extends AppCompatActivity {
    ActivityListBinding binding;
    private List<AllData> allData = new ArrayList<>();
    private ListAdapter thumAdapter;
    private int color;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityListBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        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();


        initEvent();

    }

    private void initEvent() {
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }

    private void initData() {
        Intent intent = getIntent();
        color = intent.getIntExtra(StaticValue.KEY_Color, 0);
        String name = intent.getStringExtra(StaticValue.KEY_CategoryName);
        binding.prewListTitle.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<AllData> allData) {
        thumAdapter = new ListAdapter(this, allData,color);
        binding.prewListRv.setAdapter(thumAdapter);
        binding.prewListRv.setLayoutManager(new GridLayoutManager(this, 2));
    }


}