package com.apps.chillscreen.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.ad.toponlibrary.AdManager;
import com.ad.toponlibrary.onActionListener;
import com.apps.chillscreen.R;
import com.apps.chillscreen.adapter.GirdAdapter;
import com.apps.chillscreen.bean.Picture;
import com.apps.chillscreen.databinding.ActivityExploreBinding;
import com.apps.chillscreen.listener.CardClickListener;
import com.apps.chillscreen.tool.PictureManager;

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

public class ExploreActivity extends AppCompatActivity implements CardClickListener {
ActivityExploreBinding binding;
GirdAdapter girdAdapter;
private String searchKey="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding=ActivityExploreBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        init();
        AdManager.loadAllAd();
        AdManager.showTopOn(this, new onActionListener() {
            @Override
            public void onAction() {

            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        Log.d("YYYYYYY", "onResume is start");
        startSearch(false);
    }
    private void init(){
        girdAdapter=new GirdAdapter();
        girdAdapter.setPictureList(new ArrayList<>());
        girdAdapter.setClickListener(this);
        GridLayoutManager gridLayoutManager=new GridLayoutManager(this,3);
        binding.resultRecycler.setLayoutManager(gridLayoutManager);
        binding.resultRecycler.setAdapter(girdAdapter);

        binding.imgBack.setOnClickListener(v -> finish());
        binding.btnSearch.setOnClickListener(v -> startSearch(true));
        binding.et.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH||actionId==EditorInfo.IME_ACTION_DONE) {
                startSearch(true);
                return true;
            }
            return false;
        });
        binding.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) {
                searchKey=s.toString().trim();
                binding.btnSearch.setSelected(!searchKey.isEmpty());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
    private void startSearch(boolean afterClick){
        closeKeyboard();
        if (afterClick) {
            binding.searchPb.setVisibility(View.VISIBLE);
        }
        String string = binding.et.getText().toString();
        if (!string.isEmpty()) {
            List<Picture> result = PictureManager.search(string);
            if (!result.isEmpty()) {
                showEmpty(false);
                girdAdapter.setPictureList(result);
                binding.searchPb.setVisibility(View.GONE);
            } else {
                binding.searchPb.setVisibility(View.GONE);
                showEmpty(true);
            }

            Log.d("YYYYYY", "--------------size=" + result.size());
            for (Picture data : result) {
                Log.d("YYYYYY", "--------------data=" + data.getDescription());
            }
        } else {
            if (afterClick) {
                binding.searchPb.setVisibility(View.GONE);
                Toast.makeText(ExploreActivity.this, getString(R.string.please_enter), Toast.LENGTH_SHORT).show();
            }
        }
    }
    private void closeKeyboard() {
        InputMethodManager imm = (InputMethodManager) ExploreActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(binding.et.getWindowToken(), 0);
    }
    private void showEmpty(boolean showEmpty) {
        if (showEmpty) {
            binding.emptyLayout.setVisibility(View.VISIBLE);
            binding.resultRecycler.setVisibility(View.GONE);
        } else {
            binding.emptyLayout.setVisibility(View.GONE);
            binding.resultRecycler.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onDetailClick(Picture picture) {
        Intent intent = new Intent(ExploreActivity.this, ExtendedInfoActivity.class);
        intent.putExtra(ExtendedInfoActivity.pictureKey, picture);
        startActivity(intent);
    }
}