package com.key.coolkeyboard.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 android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.key.coolkeyboard.CoolKeyboardSkin;
import com.key.coolkeyboard.R;
import com.key.coolkeyboard.adapter.ThumAdapter;
import com.key.coolkeyboard.data.MydataBase;
import com.key.coolkeyboard.data.Otherdata;
import com.key.coolkeyboard.databinding.ActivitySerachBinding;
import com.key.coolkeyboard.tool.Mytool;
import com.key.coolkeyboard.tool.SpaceItem;
import com.key.coolkeyboard.tool.StaticValue;
import com.key.coolkeyboard.topon.AdManager;
import com.key.coolkeyboard.topon.onActionListener;

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

public class SerachActivity extends AppCompatActivity {
    ActivitySerachBinding binding;
    private String text;
    private final SpaceItem spaceItem = new SpaceItem(12, 12, 12);
    private List<Otherdata> searchdata = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySerachBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

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

        Intent intent = getIntent();
        text = intent.getStringExtra(StaticValue.KEY_TEXT);
        binding.searcRv.addItemDecoration(spaceItem);

        search(text);
        initData();
        initEvent();

    }

    private void initEvent() {
        binding.searchBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.searchSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search(binding.searchEdit.getText().toString());
            }
        });
    }

    private void initData() {

        binding.faile.setVisibility(View.GONE);
        if (TextUtils.isEmpty(text)) {
            binding.searchEdit.requestFocus();
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
        }

    }

    private void hideKeyView() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(binding.searchEdit.getWindowToken(), 0);
        binding.searchEdit.clearFocus();
    }

    private void search(String text) {
        if (TextUtils.isEmpty(text)) {
            binding.faile.setText(R.string.text_tips);
            binding.faile.setVisibility(View.VISIBLE);
            binding.searcRv.setVisibility(View.GONE);
        } else {

            binding.searchEdit.setText(text);
            binding.faile.setVisibility(View.GONE);
            binding.searcRv.setVisibility(View.VISIBLE);

            Mytool.runIO(new Runnable() {
                @Override
                public void run() {
                    searchdata = MydataBase.getInstance().myDAO().getSearchData(text);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (searchdata.isEmpty()) {
                                binding.faile.setText(R.string.text_null);
                                binding.faile.setVisibility(View.VISIBLE);
                                binding.searcRv.setVisibility(View.GONE);
                            } else {
                                binding.faile.setVisibility(View.GONE);
                                binding.searcRv.setVisibility(View.VISIBLE);
                                binding.searcRv.setAdapter(new ThumAdapter(SerachActivity.this, searchdata));
                                binding.searcRv.setLayoutManager(new GridLayoutManager(SerachActivity.this, 2));
                            }
                        }
                    });

                }
            });
        }
        hideKeyView();
    }

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


}