package com.newapp.paperwall.uiview;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import com.newapp.paperwall.BaseApp;
import com.newapp.paperwall.R;
import com.newapp.paperwall.adapter.ThreeLinesAdapter;
import com.newapp.paperwall.databinding.ActivityFindBinding;
import com.newapp.paperwall.javabean.Data;
import com.newapp.paperwall.listener.OnItemClickListener;
import com.newapp.paperwall.tools.UiUtils;
import com.newapp.paperwall.tools.DbManager;
import com.newapp.paperwall.tools.ItemSpace;

import java.util.List;

public class SearchActivity extends AppCompatActivity implements OnItemClickListener {

    private ThreeLinesAdapter adapterWallapper;
    private ActivityFindBinding vb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = ActivityFindBinding.inflate(getLayoutInflater());
        UiUtils.initFull(this, true);
        setContentView(vb.getRoot());
        initList();
        setBack();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("YYYYYYY", "onResume is start");
        startSearch(false);
    }

    private void initList() {
        int height = UiUtils.dp2Px(240);
        adapterWallapper = new ThreeLinesAdapter(this, height);
        adapterWallapper.setInfoWallpaperListener(this);
        ItemSpace recyclerItemSpace = new ItemSpace(10, 10, 15);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(SearchActivity.this, 3);
        vb.resultRecycler.setLayoutManager(gridLayoutManager);
        vb.resultRecycler.addItemDecoration(recyclerItemSpace);
        vb.resultRecycler.setAdapter(adapterWallapper);


        vb.et.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                startSearch(true);
                return true;
            }
            return false;
        });
        vb.et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String inputText = s.toString().trim();
                vb.btnSearch.setSelected(!inputText.isEmpty());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        vb.btnSearch.setOnClickListener(v -> startSearch(true));

    }

    private void closeKeyboard() {
        InputMethodManager imm = (InputMethodManager) SearchActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(vb.et.getWindowToken(), 0);
    }

    private void startSearch(boolean afterClick) {
        closeKeyboard();
        if (afterClick) {
            vb.searchPb.setVisibility(View.VISIBLE);
        }
        String string = vb.et.getText().toString();
        if (!string.isEmpty()) {
            List<Data> search = DbManager.search(string);
            if (!search.isEmpty()) {
                showEmpty(false);
                adapterWallapper.setDataInfo(search);
                vb.searchPb.setVisibility(View.GONE);
            } else {
                vb.searchPb.setVisibility(View.GONE);
                showEmpty(true);
            }

            Log.d(BaseApp.TAG, "--------------size=" + search.size());
            for (Data data : search) {
                Log.d(BaseApp.TAG, "--------------data=" + data.getDescription());
            }
        } else {
            if (afterClick) {
                vb.searchPb.setVisibility(View.GONE);
                Toast.makeText(SearchActivity.this, getString(R.string.please_enter), Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void showEmpty(boolean showEmpty) {
        if (showEmpty) {
            vb.emptyLayout.setVisibility(View.VISIBLE);
            vb.resultRecycler.setVisibility(View.GONE);
        } else {
            vb.emptyLayout.setVisibility(View.GONE);
            vb.resultRecycler.setVisibility(View.VISIBLE);
        }

    }

    private void setBack() {
        vb.imgBack.setOnClickListener(v -> {
            finish();
        });
    }

    @Override
    public void onItemClickAction(Data info) {
        Intent intent = new Intent(SearchActivity.this, DetailActivity.class);
        intent.putExtra(UiUtils.key_info, info);
        startActivity(intent);
    }


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