package com.idle.wall.idleactivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Toast;

import com.idle.wall.R;
import com.idle.wall.databinding.ActivityIdleListBinding;
import com.idle.wall.idleadapter.OnItemclick;
import com.idle.wall.idlebean.IdleData;
import com.idle.wall.idleitem.ItemIdleList;
import com.idle.wall.idletool.Comutils;
import com.idle.wall.idletool.Itemhelper;
import com.idle.wall.idletool.ObectManager;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import androidx.recyclerview.widget.GridLayoutManager;

public class IdleListActivity extends BaseActivity implements OnItemclick {
    private ActivityIdleListBinding vb;
    private String categoryName;
    public static final String key_index = "index";
    private final ExecutorService executor = Executors.newSingleThreadExecutor();
    private final Handler mainHandler = new Handler(Looper.getMainLooper());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Comutils.initFull(this, false);
        vb = ActivityIdleListBinding.inflate(getLayoutInflater());
        setContentView(vb.getRoot());
        categoryName = getIntent().getStringExtra(key_index);
        vb.name.setText(categoryName);
        vb.imageviewBack.setOnClickListener(v -> finish());
        loadDataAndInitList();
    }
    private void loadDataAndInitList() {
        vb.list.setVisibility(View.GONE);
        executor.execute(() -> {
            try {
                List<IdleData> data = ObectManager.queryCategoryAll(categoryName);
                mainHandler.post(() -> {
                    initRecyclerView(data);
                    vb.list.setVisibility(View.VISIBLE);
                });
            } catch (Exception e) {
                mainHandler.post(() -> {
                    Toast.makeText(IdleListActivity.this, R.string.falsed, Toast.LENGTH_SHORT).show();
                    finish();
                });
                e.printStackTrace();
            }
        });
    }
    private void initRecyclerView(List<IdleData> data) {
        Itemhelper itemHelper = new Itemhelper(10, 10, 0);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);

        vb.list.addItemDecoration(itemHelper);
        vb.list.setLayoutManager(gridLayoutManager);

        int height = Comutils.dp2Px(180);
        ItemIdleList itemList = new ItemIdleList(this, height, false);
        itemList.setDataInfo(data);
        itemList.setInfoWallpaperListener(this);
        vb.list.setAdapter(itemList);
    }

    @Override
    public void onItemClickAction(IdleData info) {
        Intent intent = new Intent(this, IdleWallActivity.class);
        intent.putExtra(Comutils.key_info, info);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        executor.shutdown(); // 关闭线程池
        mainHandler.removeCallbacksAndMessages(null); // 清空未执行的回调
    }
}