package com.draw.painting.line.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.draw.painting.line.DrawingApp;
import com.draw.painting.line.adapter.CategoryListAdapter;
import com.draw.painting.line.databinding.FragmentCategoryBinding;
import com.draw.painting.line.onClickListener;

public class CategoryFragment extends Fragment implements onClickListener {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";



    private FragmentCategoryBinding fragmentHomeBinding;

    public CategoryFragment() {

    }

    public static CategoryFragment newInstance() {
        CategoryFragment fragment = new CategoryFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        fragmentHomeBinding = FragmentCategoryBinding.inflate(getLayoutInflater());
        onInitRecycler();
        onInitRandomButton();
        return fragmentHomeBinding.getRoot();
    }

    private void onInitRecycler() {
        CategoryListAdapter categoryListAdapter = new CategoryListAdapter(requireContext());
        categoryListAdapter.setListener(this::onClickItem);
        fragmentHomeBinding.recyclerview.setAdapter(categoryListAdapter);
        fragmentHomeBinding.recyclerview.setLayoutManager(new LinearLayoutManager(requireContext()));
    }

    private void onInitRandomButton() {
        View.OnClickListener randomSketchListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 随机选择一个分类和图片
                java.util.List<String> allDirs = com.draw.painting.line.manager.CategoryConfig.getAllDir();
                if (!allDirs.isEmpty()) {
                    java.util.Random random = new java.util.Random();
                    String randomDir = allDirs.get(random.nextInt(allDirs.size()));
                    
                    // 获取该分类下的所有图片
                    java.util.List<String> images = com.draw.painting.line.manager.AppHelper.fileExistsInAssets(requireContext(), randomDir);
                    if (!images.isEmpty()) {
                        String randomImage = images.get(random.nextInt(images.size()));
                        Intent intent = new Intent(requireContext(), DrawActivity.class);
                        intent.putExtra(DrawingApp.Key_Camera, randomImage);
                        startActivity(intent);
                    }
                }
            }
        };
        
        // 设置整个卡片的点击事件
        fragmentHomeBinding.btnRandomSketch.setOnClickListener(randomSketchListener);
        
        // 设置 Start 按钮的点击事件（确保能响应）
        fragmentHomeBinding.btnStart.setOnClickListener(randomSketchListener);
    }



    @Override
    public void onClickItem(boolean viewAll,String dir) {
        Intent intent;
        if(viewAll){
            intent = new Intent(requireContext(), GalleryActivity.class);
            intent.putExtra(DrawingApp.Key_ViewDir, dir);
        }else {
            intent = new Intent(requireContext(), DrawActivity.class);
            intent.putExtra(DrawingApp.Key_Camera,dir);
        }
        startActivity(intent);
    }
}

