package com.exquisite.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 androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.exquisite.KeyboardSkin;
import com.exquisite.adapter.ThumAdapter;
import com.exquisite.data.Mydata;
import com.exquisite.R;
import com.exquisite.tools.SpaceItem;

import java.util.List;

public class SearchActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView back;
    private EditText searchText;
    private RecyclerView searchrv;
    private String name;
    private String searchname;
    private TextView search, faile;

    private final SpaceItem spaceItem = new SpaceItem(10, 20, 10);

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.search_activity), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initView();
        faile.setVisibility(View.GONE);
        searchText.requestFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
        searchText.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                search();
                return true;
            }
            return false;
        });
        searchrv.addItemDecoration(spaceItem);
        initEvent();
    }

    private void initEvent() {
        back.setOnClickListener(this);
        search.setOnClickListener(this);
    }

    private void initView() {
        back = findViewById(R.id.search_back);
        searchrv = findViewById(R.id.searc_rv);
        searchText = findViewById(R.id.edit_search);
        search = findViewById(R.id.cancel);
        faile = findViewById(R.id.faile);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_back:
                finish();
                break;
            case R.id.cancel:
                search();
                break;
        }
    }

    private void search() {
        if (TextUtils.isEmpty(searchText.getText().toString())) {
            faile.setVisibility(View.VISIBLE);
            searchrv.setVisibility(View.GONE);
            faile.setText(R.string.text_tips);
        } else {
            faile.setVisibility(View.GONE);
            searchrv.setVisibility(View.VISIBLE);
            Mydata mydata = new Mydata();
            searchname = searchText.getText().toString();
            List<Mydata> filteredData = mydata.filterByName(searchname, KeyboardSkin.getMyDataArrayList());
            if (filteredData.isEmpty()) {
                faile.setVisibility(View.VISIBLE);
                faile.setText(R.string.text_null);
                searchrv.setVisibility(View.GONE);
            } else {
                faile.setVisibility(View.GONE);
                searchrv.setVisibility(View.VISIBLE);
                searchrv.setAdapter(new ThumAdapter(this, filteredData));
                searchrv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
            }
        }
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(searchText.getWindowToken(), 0);

    }


}