package com.key.vibekeyboard.ui.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;

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.LinearLayoutManager;

import com.key.vibekeyboard.R;
import com.key.vibekeyboard.data.bean.Category;
import com.key.vibekeyboard.data.database.entity.WallpaperInfo;
import com.key.vibekeyboard.ui.adapter.WallpaperAdapter;
import com.key.vibekeyboard.utils.ItemDecoration;
import com.key.vibekeyboard.utils.JsonUtils;
import com.key.vibekeyboard.databinding.ActivitySearchBinding;
import com.key.vibekeyboard.topon.AdManager;
import com.key.vibekeyboard.topon.onActionListener;

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

public class SearchActivity extends AppCompatActivity {

    private ActivitySearchBinding binding;
    private final List<Category> categories = JsonUtils.parseJson("keyboard.json");
    private final List<WallpaperInfo> filteredList = new ArrayList<>();
    private WallpaperAdapter adapter;

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

        binding = ActivitySearchBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

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

        AdManager.loadAllAd();

        initData();
        initEvent();
    }

    private void initData(){
        AdManager.showTopOn(this, new onActionListener() {
            @Override
            public void onAction() {

            }
        });

        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new WallpaperAdapter(this, filteredList);
        ItemDecoration itemDecoration = new ItemDecoration(12, 10, 9);
        binding.recyclerView.addItemDecoration(itemDecoration);

        binding.recyclerView.setAdapter(adapter);
    }

    private void initEvent(){
        binding.back.setOnClickListener(v -> finish());

        binding.searchEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                filter(charSequence.toString()); // 调用过滤方法
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
    }

    private void filter(String text) {
        filteredList.clear(); // 清空之前的结果

        for (Category category : categories) {
            for (WallpaperInfo wallpaper : category.getList()) {
                if (wallpaper.getTitle().toLowerCase().contains(text.toLowerCase())) {
                    filteredList.add(wallpaper); // 添加匹配的结果
                }
            }
        }

        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
    }

}
