package com.lc.myapplication.security.locks.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

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.lc.myapplication.security.locks.R;
import com.lc.myapplication.security.locks.adapter.LockedAppAdapter;
import com.lc.myapplication.security.locks.adapter.SearchAdapter;
import com.lc.myapplication.security.locks.draw.MySpace;
import com.lc.myapplication.security.locks.lockroom.SecurityLockDataBase;
import com.lc.myapplication.security.locks.lockroom.SecurityLockEntity;
import com.lc.myapplication.security.locks.tool.MyTools;

import java.util.List;

public class SearchActivity extends AppCompatActivity {

    private EditText rearch_edit_text;
    private RecyclerView rearch_recycle_view;
    private List<SecurityLockEntity> list;
    private ImageView rearch_touch;
    private String rearch_appname;
    private ImageView search_app_back;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        rearch_edit_text = findViewById(R.id.rearch_edit_text);
        rearch_recycle_view = findViewById(R.id.rearch_recycle_view);
        rearch_touch = findViewById(R.id.rearch_touch);
        search_app_back = findViewById(R.id.search_apps_back);
        onMyClick();
    }

    private void onMyClick() {
        search_app_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        rearch_touch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rearch_appname = rearch_edit_text.getText().toString();
                setRecycleView();
                Log.d("dhn","dhn---------fin"+rearch_appname);
            }
        });
    }

    private void setRecycleView() {
        MyTools.RUNIO(new Runnable() {
            @Override
            public void run() {
                list = SecurityLockDataBase.getSecurityLockDataBase().getsecurityLockDAO().QueryByInputName(rearch_appname);
                Log.d("dhn1","dhn---------fin"+list.size());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        MySpace mySpace = new MySpace(5,20,3);
                        GridLayoutManager gridLayoutManager = new GridLayoutManager(SearchActivity.this,3);
                        rearch_recycle_view.setLayoutManager(gridLayoutManager);
                        rearch_recycle_view.addItemDecoration(mySpace);
                        SearchAdapter searchAdapter = new SearchAdapter(SearchActivity.this,list);
                        rearch_recycle_view.setAdapter(searchAdapter);
                    }
                });
            }
        });
    }
}