package com.wall.wallpaper.hd.ui_view;

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.wall.wallpaper.hd.R;
import com.wall.wallpaper.hd.WallApp;
import com.wall.wallpaper.hd.ad.AdManager;
import com.wall.wallpaper.hd.ad.onActionListener;
import com.wall.wallpaper.hd.adapter.AdapterList;
import com.wall.wallpaper.hd.databinding.FragSearchBinding;
import com.wall.wallpaper.hd.javabean.Data;
import com.wall.wallpaper.hd.listener.ItemClickListener;
import com.wall.wallpaper.hd.tools.CommonUtils;
import com.wall.wallpaper.hd.tools.DbManager;
import com.wall.wallpaper.hd.tools.ItemSpace;

import java.util.List;


public class FragmentSearch extends Fragment  implements ItemClickListener {



    private AdapterList adapterWallapper;

    private FragSearchBinding vb;


    public FragmentSearch() {

    }

    public static FragmentSearch newInstance() {
        Bundle bundle = new Bundle();
        FragmentSearch fragment = new FragmentSearch();
        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 = FragSearchBinding.inflate(getLayoutInflater());
        initList();
        return vb.getRoot();
    }


    private void initList() {
        int height = CommonUtils.dp2Px(180);
          adapterWallapper = new AdapterList(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) {
                    AdManager.showTopOn(requireActivity(), new onActionListener() {
                        @Override
                        public void onAction() {
                            startSearch();
                        }
                    });
                    return true;
                }
                return false;
            }
        });
        vb.btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AdManager.showTopOn(requireActivity(), new onActionListener() {
                    @Override
                    public void onAction() {
                        startSearch();
                    }
                });

            }
        });

    }

    private void closeKeyboard() {
        InputMethodManager imm = (InputMethodManager) requireContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(vb.et.getWindowToken(), 0);
    }
    private void startSearch() {
        closeKeyboard();
        vb.searchPb.setVisibility(View.VISIBLE);
        String string = vb.et.getText().toString();
        if (!string.isEmpty()) {
            List<Data> search = DbManager.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(WallApp.TAG, "--------------size=" + search.size());
            for (Data data : search) {
                Log.d(WallApp.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(), ActivitySetWallpaper.class);
        intent.putExtra(CommonUtils.key_info, info);
        startActivity(intent);
    }
}