package com.fun.airhornprank.fragment;

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.StaggeredGridLayoutManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fun.airhornprank.R;
import com.fun.airhornprank.activity.SettingActivity;
import com.fun.airhornprank.adapter.ThumAdapter;
import com.fun.airhornprank.data.MyDataBase;
import com.fun.airhornprank.data.Mydata;
import com.fun.airhornprank.databinding.FragmentMainBinding;
import com.fun.airhornprank.tool.Mytools;

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


public class MainFragment extends Fragment {
    FragmentMainBinding binding;
    private List<Mydata> allData = new ArrayList<>();
    private List<Mydata> maindata = new ArrayList<>();
    private ThumAdapter thumAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentMainBinding.inflate(inflater, container, false);

        initData();

        binding.mainSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(requireContext(), SettingActivity.class);
                startActivity(intent);
            }
        });

        return binding.getRoot();
    }

    private void initData() {
        MyDataBase.getInstance().likeDAO().livegetAll().observe(getViewLifecycleOwner(), new Observer<List<Mydata>>() {
            @Override
            public void onChanged(List<Mydata> list) {
//                maindata = chekData(list);
                initView(chekData(list));

            }
        });
//        Mytools.runIO(new Runnable() {
//            @Override
//            public void run() {
//                allData = MyDataBase.getInstance().likeDAO().getAll();
//                chekData(allData);
//                if (!chekData(allData).isEmpty()) {
//                    maindata = chekData(allData);
//                    requireActivity().runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            initView(maindata);
//                        }
//                    });
//                }
//            }
//        });
    }

    private void initView(List<Mydata> maindata) {
        thumAdapter = new ThumAdapter(requireActivity(), maindata);
        binding.marinRv.setAdapter(thumAdapter);
        binding.marinRv.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
    }

    private static List<Mydata> chekData(List<Mydata> data) {
//        Collections.shuffle(data);
//        data.subList(0, 10);
        List<Mydata> main = new ArrayList<>();
        Set<String> name = new HashSet<>();
        for (Mydata mydata : data) {
            if (!name.contains(mydata.getCategoryname())) {
                main.add(mydata);
                name.add(mydata.getCategoryname());
            }
        }
        return main;
    }

//    @Override
//    public void onResume() {
//        super.onResume();
//        initData();
//    }
}