package com.tool.applockpro.activity;

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

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.tool.applockpro.ApplockSkin;
import com.tool.applockpro.R;
import com.tool.applockpro.adapter.AppAdapter;
import com.tool.applockpro.data.MyDataBase;
import com.tool.applockpro.data.Mydata;
import com.tool.applockpro.databinding.ActivitySearchBinding;
import com.tool.applockpro.tool.Mytools;

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

public class SearchActivity extends AppCompatActivity {
    ActivitySearchBinding binding;
    private List<Mydata> searchdata = new ArrayList<>();

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

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

        initData();

        initEvent();
    }

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



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

            binding.searchEdit.setText(text);
            binding.searchTips.setVisibility(View.GONE);
            binding.searchRv.setVisibility(View.VISIBLE);

            Mytools.runIO(new Runnable() {
                @Override
                public void run() {
                    searchdata = MyDataBase.getINSTANCE().mydao().getSearchDatas(text);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (searchdata.isEmpty()) {
                                binding.searchTips.setText(R.string.text_null);
                                binding.searchTips.setVisibility(View.VISIBLE);
                                binding.searchRv.setVisibility(View.GONE);
                            } else {
                                binding.searchTips.setVisibility(View.GONE);
                                binding.searchRv.setVisibility(View.VISIBLE);
                                AppAdapter appAdapter = new AppAdapter(SearchActivity.this, SearchActivity.this);
                                appAdapter.update(searchdata);
                                binding.searchRv.setAdapter(appAdapter);
                                binding.searchRv.setLayoutManager(new LinearLayoutManager(SearchActivity.this));
                            }
                        }
                    });

                }
            });
        }
        hideKeyView();
    }

    private void initData() {

        binding.searchTips.setVisibility(View.GONE);
        binding.searchEdit.requestFocus();
        binding.searchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    search(binding.searchEdit.getText().toString());
                    return true;
                }
                return false;
            }
        });

    }

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

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