package com.exquisite.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.exquisite.KeyboardSkin;
import com.exquisite.activity.AllActivity;
import com.exquisite.activity.SearchActivity;
import com.exquisite.adapter.ThumAdapter;
import com.exquisite.data.Mydata;
import com.exquisite.data.MydataBase;
import com.exquisite.R;
import com.exquisite.tools.Mytools;
import com.exquisite.tools.SpaceItem;

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


public class MainFragment extends Fragment {
    private RecyclerView mainrv;
    private LinearLayout viewall;
    private ImageView seach;
    private SpaceItem spaceItem = new SpaceItem(10, 20, 10);
    private ThumAdapter thumAdapter;
    private List<Mydata> mainrandomlist = new ArrayList<>();

    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_page_one, container, false);
        viewall = view.findViewById(R.id.view_all);
        seach = view.findViewById(R.id.search_image);
        mainrv = view.findViewById(R.id.main_keyboard_rv);

        viewall.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), AllActivity.class);
            startActivity(intent);
        });

        seach.setOnClickListener(v -> {
            Intent intent1 = new Intent(getActivity(), SearchActivity.class);
            startActivity(intent1);
        });

        initData();

        return view;
    }

    private void initData() {

        List<Mydata> mainrvdataList = KeyboardSkin.getMyDataArrayList();
        mainrvdataList.subList(0, 20);
        for (Mydata data : mainrvdataList) {
            if (mainrandomlist.size() < 20) {
                mainrandomlist.add(data);
            }
        }
        thumAdapter = new ThumAdapter(requireContext(), mainrandomlist);
        mainrv.setAdapter(thumAdapter);
        mainrv.setLayoutManager(new GridLayoutManager(requireContext(), 2));
        mainrv.addItemDecoration(spaceItem);
//        Mytools.runIO(new Runnable() {
//            @Override
//            public void run() {
//                List<Mydata> mydata = MydataBase.getInstance().myDAO().getAlldata();
//                requireActivity().runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
////                        Collections.shuffle(mydata);
//                        List<Mydata> newList = mydata.subList(10, 20);
//                        thumAdapter = new ThumAdapter(requireContext(), newList);
//                        mainrv.setAdapter(thumAdapter);
//                        mainrv.setLayoutManager(new GridLayoutManager(requireContext(), 2));
//                        mainrv.addItemDecoration(spaceItem);
//                    }
//                });
//            }
//        });
//        MydataBase.getInstance().myDAO().getAll().observe(getViewLifecycleOwner(), new Observer<List<Mydata>>() {
//            @Override
//            public void onChanged(List<Mydata> mydata) {
//                Collections.shuffle(mydata);
//                List<Mydata> newList = mydata.subList(0, 20);
//                thumAdapter = new ThumAdapter(requireContext(), newList);
//                mainrv.setAdapter(thumAdapter);
//                mainrv.setLayoutManager(new GridLayoutManager(requireContext(), 2));
//                mainrv.addItemDecoration(spaceItem);
//            }
//        });
    }
}