package com.example.applock.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
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.example.applock.R;
import com.example.applock.allmanager.MyWorker;
import com.example.applock.lockadapter.SearchAdapter;
import com.example.applock.roombase.AppDataBase;
import com.example.applock.roombase.AppEntity;
import com.example.applock.roombase.SoftWareLockRoom;
import com.example.applock.value.MyAllValues;
import com.example.applock.value.Mytool;

import java.util.List;

public class SearchActivity extends AppCompatActivity {

    private RecyclerView recycle_search;
    private EditText search_text;
    private ImageView start_search;
    private String put_name;
    private ImageView search_back;
    private String search_name;
    private List<AppEntity> list;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        Mytool.getAllAd();
        recycle_search = findViewById(R.id.recycle_search);
        search_text = findViewById(R.id.search_text);
        start_search = findViewById(R.id.start_search);
        search_back = findViewById(R.id.search_back);
        search_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getData();
        start_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search_name = search_text.getText().toString();
                //setRecycle();
            }
        });
    }

    private void setRecycle() {
        SoftWareLockRoom.RUNIO(new Runnable() {
            @Override
            public void run() {
                list = AppDataBase.getDatabaseInstance().getAppEntityDao().QueryByInputName(search_name);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        SearchAdapter searchAdapter = new SearchAdapter(SearchActivity.this,list);
                        recycle_search.setAdapter(searchAdapter);
                        recycle_search.setLayoutManager(new GridLayoutManager(SearchActivity.this,1));
                    }
                });
            }
        });
    }

    private void getData() {
        Intent intent = getIntent();
        search_name = intent.getStringExtra(MyAllValues.search_name);
        search_text.setText(search_name);
        setRecycle();
    }

}