package com.key.simplekeyboard.activity;

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 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 com.key.simplekeyboard.R;
import com.key.simplekeyboard.SimpleKeyboardSkins;
import com.key.simplekeyboard.adapter.ThumAdapter;
import com.key.simplekeyboard.data.MydataBase;
import com.key.simplekeyboard.data.Otherdata;
import com.key.simplekeyboard.tools.Mytools;
import com.key.simplekeyboard.tools.SpaceItem;

import java.util.List;

public class SearchActivity extends AppCompatActivity {
    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(12, 12, 12);

    @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 hideKeyView() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(searchText.getWindowToken(), 0);
        searchText.clearFocus();
    }
    private void initEvent() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search();
            }
        });
    }

    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);
    }


    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);
            Otherdata mydata = new Otherdata();
            searchname = searchText.getText().toString();
            List<Otherdata> filteredData = mydata.filterByName(searchname, SimpleKeyboardSkins.getOtherdata());
            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 GridLayoutManager(this, 2));
            }


        }
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(searchText.getWindowToken(), 0);

    }

    @Override
    protected void onPause() {
        super.onPause();
        hideKeyView();
    }
}