package com.phone.wall.paper.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.ad.toponlibrary.AdManager;
import com.ad.toponlibrary.onActionListener;
import com.phone.wall.paper.R;
import com.phone.wall.paper.WallpaperApp;
import com.phone.wall.paper.adapter.AllAda;
import com.phone.wall.paper.data.DataBean;
import com.phone.wall.paper.databinding.FragSearchBinding;
import com.phone.wall.paper.listener.OnClick;
import com.phone.wall.paper.tools.Utils;
import com.phone.wall.paper.tools.RoomManager;
import com.phone.wall.paper.tools.ListSpace;

import java.util.List;


public class FragSearch extends Fragment  implements OnClick {



    private AllAda adapterWallapper;

    private FragSearchBinding 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 = FragSearchBinding.inflate(getLayoutInflater());
        initList();
        return vb.getRoot();
    }


    private void initList() {
        int height = Utils.dp2Px(180);
          adapterWallapper = new AllAda(requireContext(),height);
        adapterWallapper.setInfoWallpaperListener(this);
        ListSpace recyclerListSpace = new ListSpace(10, 10, 15);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), 2);
        vb.resultRecycler.setLayoutManager(gridLayoutManager);
        vb.resultRecycler.addItemDecoration(recyclerListSpace);
        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<DataBean> search = RoomManager.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(WallpaperApp.TAG, "--------------size=" + search.size());
                    for (DataBean dataBean : search) {
                        Log.d(WallpaperApp.TAG, "--------------data=" + dataBean.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(DataBean info) {
        Intent intent = new Intent(requireContext(), SetWall.class);
        intent.putExtra(Utils.key_info, info);
        startActivity(intent);
    }
}