package com.photo.wallpaper.category.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import com.photo.wallpaper.category.MyApp;
import com.photo.wallpaper.category.R;
import com.photo.wallpaper.category.ad.AdManager;
import com.photo.wallpaper.category.ad.onActionListener;
import com.photo.wallpaper.category.api.ApiItemClick;
import com.photo.wallpaper.category.bean.Data;
import com.photo.wallpaper.category.bean.DataBaseHelper;
import com.photo.wallpaper.category.databinding.FragmentLayoutSearchBinding;
import com.photo.wallpaper.category.itemAdapter.AdThumb;
import com.photo.wallpaper.category.utils.CommonHelper;
import com.photo.wallpaper.category.utils.ItemSpace;

import java.util.List;


public class FragSearch extends Fragment  implements ApiItemClick {



    private AdThumb adapterWallapper;

    private FragmentLayoutSearchBinding vb;


    public FragSearch() {

    }

    public static FragSearch newInstance() {
        Bundle bundle = new Bundle();
        FragSearch fragment = new FragSearch();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        vb = FragmentLayoutSearchBinding.inflate(getLayoutInflater());
        initList();
        return vb.getRoot();
    }


    private void initList() {
        int height = CommonHelper.dp2Px(180);
          adapterWallapper = new AdThumb(requireContext(),height);
        adapterWallapper.setInfoWallpaperListener(this);
        ItemSpace recyclerItemSpace = new ItemSpace(10, 10, 15);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), 2);
        vb.resultRecycler.setLayoutManager(gridLayoutManager);
        vb.resultRecycler.addItemDecoration(recyclerItemSpace);
        vb.resultRecycler.setAdapter(adapterWallapper);


        vb.et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    startSearch();
                    return true;
                }
                return false;
            }
        });
        vb.btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startSearch();
            }
        });

    }

    private void closeKeyboard() {
        InputMethodManager imm = (InputMethodManager) requireContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(vb.et.getWindowToken(), 0);
    }
    private void startSearch() {
        AdManager.showTopOn(requireActivity(), new onActionListener() {
            @Override
            public void onAction() {
                closeKeyboard();
                vb.searchPb.setVisibility(View.VISIBLE);
                String string = vb.et.getText().toString();
                if (!string.isEmpty()) {
                    List<Data> search = DataBaseHelper.search(string);
                    if (search.size() > 0) {
                        showEmpty(false);
                        adapterWallapper.setDataInfo(search);

                        vb.searchPb.setVisibility(View.GONE);
                    } else {
                        vb.searchPb.setVisibility(View.GONE);
                        showEmpty(true);
                    }

                    Log.d(MyApp.TAG, "--------------size=" + search.size());
                    for (Data data : search) {
                        Log.d(MyApp.TAG, "--------------data=" + data.getDescription());
                    }
                } else {
                    vb.searchPb.setVisibility(View.GONE);
                    Toast.makeText(requireContext(), getString(R.string.please_input_keyword), 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);
        }

    }


    @Override
    public void onItemClickAction(Data info) {
        Intent intent = new Intent(requireContext(), ActivityApply.class);
        intent.putExtra(CommonHelper.key_info, info);
        startActivity(intent);
    }
}