package com.draw.painting.canvas.presentation;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.draw.painting.canvas.CanvasApp;
import com.draw.painting.canvas.ad.AdsInsUtil;
import com.draw.painting.canvas.ad.LoadListener;
import com.draw.painting.canvas.ad.ShowListener;
import com.draw.painting.canvas.adapters.SketchPreviewAdapter;
import com.draw.painting.canvas.databinding.ActivityCategoryDetailBinding;
import com.draw.painting.canvas.core.SketchRepository;
import com.draw.painting.canvas.core.SystemHelper;
import com.tradplus.ads.base.bean.TPAdInfo;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class CategoryDetailActivity extends BaseActivity {

    private ActivityCategoryDetailBinding binding;
    private String dirStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 启用 EdgeToEdge 全屏显示
        SystemHelper.enableEdgeToEdge(this);
        
        binding = ActivityCategoryDetailBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        
        // 设置 WindowInsets 监听器
        setupInsets();
        
        dirStr = getIntent().getStringExtra(CanvasApp.Key_ViewDir);
        
        // 设置分类名称
        String categoryName = dirStr.substring(dirStr.lastIndexOf("_") + 1);
        binding.categoryName.setText(categoryName);
        
        // 获取 sketch 列表并设置数量
        List<String> list = SystemHelper.fileExistsInAssets(this, dirStr);
        int count = list.size();
        String countText = count + (count == 1 ? " sketch available" : " sketches available");
        binding.categoryCount.setText(countText);
        
        // 设置分类说明
        String description = SketchRepository.getCategoryDescription(dirStr);
        binding.categoryDescription.setText(description);
        
        onInitList(list);
        onInitClick();
    }

    private void setupInsets() {
        // 设置根视图的 WindowInsets（处理顶部状态栏）
        SystemHelper.setupWindowInsetsListener(binding.getRoot());
        
        // 为 RecyclerView 添加底部系统栏间距，避免内容被遮挡
        SystemHelper.setupBottomNavPadding(binding.recyclerview);
    }
    
    private void onInitList(List<String> list) {
        // 检查列表是否为空
        if (list == null || list.isEmpty()) {
            // 显示空状态
            binding.recyclerview.setVisibility(View.GONE);
            binding.emptyState.getRoot().setVisibility(View.VISIBLE);
        } else {
            // 显示列表
            binding.recyclerview.setVisibility(View.VISIBLE);
            binding.emptyState.getRoot().setVisibility(View.GONE);
            
            SketchPreviewAdapter preViewAdapter = new SketchPreviewAdapter(this, list);
            binding.recyclerview.setAdapter(preViewAdapter);
            
            // 改为纵向线性布局
            LinearLayoutManager layoutcore = new LinearLayoutManager(this);
            binding.recyclerview.setLayoutManager(layoutcore);
        }
    }

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

    @Override
    protected boolean shouldInterceptBackPress() {
        return true;
    }

    @Override
    protected void onInterceptBackPressed() {
        AdsInsUtil.INSTANCE.showAd(this, AdsInsUtil.Placement.TOP_ON_AD_TOW, new ShowListener() {
            @Override
            public void onAdShown(@Nullable TPAdInfo ad) {
                loadAd();
            }

            @Override
            public void onAdClosed() {
                loadAd();
            }
        });
        if (getBackPressedCallback() != null) {
            getBackPressedCallback().setEnabled(false);
        }
        getOnBackPressedDispatcher().onBackPressed();
    }

    private void loadAd(){
        AdsInsUtil.INSTANCE.loadAd(this, AdsInsUtil.Placement.TOP_ON_AD_TOW, new LoadListener() {
            @Override
            public void loadFailed(@NotNull String error) {

            }
        });
    }
}