package com.wall.various.wallpaper.ui;

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.various.wallpaper.MyApp;
import com.wall.various.wallpaper.R;
import com.wall.various.wallpaper.ad.AdManager;
import com.wall.various.wallpaper.ad.onActionListener;
import com.wall.various.wallpaper.databinding.FragUiSearchBinding;
import com.wall.various.wallpaper.helper.Common;
import com.wall.various.wallpaper.helper.Dbhelper;
import com.wall.various.wallpaper.helper.ItemHelper;
import com.wall.various.wallpaper.inter.ItemClickInterface;
import com.wall.various.wallpaper.myjson.Data;
import com.wall.various.wallpaper.recycler.RecyclerList;

import java.util.List;


public class FragUiSearch extends Fragment  implements ItemClickInterface {



    private RecyclerList adapterWallapper;

    private FragUiSearchBinding vb;


    public FragUiSearch() {

    }

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


    private void initList() {
        int height = Common.dp2Px(180);
          adapterWallapper = new RecyclerList(requireContext(),height,true);
        adapterWallapper.setInfoWallpaperListener(this);
        ItemHelper recyclerItemHelper = new ItemHelper(10, 10, 15);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), 2);
        vb.resultRecycler.setLayoutManager(gridLayoutManager);
        vb.resultRecycler.addItemDecoration(recyclerItemHelper);
        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) {

                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 = Dbhelper.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(), UiSetWallpaper.class);
        intent.putExtra(Common.key_info, info);
        startActivity(intent);
    }
}