package com.tool.applockpro.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

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

import com.tool.applockpro.adapter.AppAdapter;
import com.tool.applockpro.data.MyDataBase;
import com.tool.applockpro.data.Mydao;
import com.tool.applockpro.data.Mydata;
import com.tool.applockpro.databinding.FragmentAllBinding;
import com.tool.applockpro.tool.Mytools;

import androidx.lifecycle.Observer;
import androidx.annotation.Nullable;

import java.util.List;

public class AllFragment extends Fragment {
    FragmentAllBinding binding;
    private String tabargs;
    private AppAdapter thumAdapter;
    private Mydao mydao;
//    private List<Mydata> list1 = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentAllBinding.inflate(inflater, container, false);
        thumAdapter = new AppAdapter(requireContext(), requireActivity());
        binding.allRv.setLayoutManager(new LinearLayoutManager(requireContext()));
        binding.allRv.setAdapter(thumAdapter);
        mydao = MyDataBase.getINSTANCE().mydao();
        tabargs = getArguments().getString("tab");
        refreshData();


        return binding.getRoot();
    }


    public void refesh() {
        mydao.getAppsBySystem(false).observe(getViewLifecycleOwner(), new Observer<List<Mydata>>() {
            @Override
            public void onChanged(List<Mydata> mydata) {

            }
        });
        Mytools.runIO(new Runnable() {
            @Override
            public void run() {
                List<Mydata> list1 = MyDataBase.getINSTANCE().mydao().getappissystem(false);
                requireActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        updateRecyclerView(list1);
                    }
                });

            }
        });
    }

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

    private void refreshData() {

//        Log.e("zzj", tabargs);
        if (tabargs.equals("System")) {
            Mytools.runIO(new Runnable() {
                @Override
                public void run() {
                    List<Mydata> list = MyDataBase.getINSTANCE().mydao().getappissystem(true);
                    requireActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            updateRecyclerView(list);
                        }
                    });
                }
            });
        } else if (tabargs.equals("Third")) {
            Mytools.runIO(new Runnable() {
                @Override
                public void run() {
                    List<Mydata> list1 = MyDataBase.getINSTANCE().mydao().getappissystem(false);
                    requireActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            updateRecyclerView(list1);
                        }
                    });
                }
            });
        } else if (tabargs.equals("Lock")) {
            mydao.getLockApps(true).observe(getViewLifecycleOwner(), new Observer<List<Mydata>>() {
                @Override
                public void onChanged(@Nullable List<Mydata> mydataList) {
                    updateRecyclerView(mydataList);
                }
            });
        }
    }

    private void updateRecyclerView(List<Mydata> mydataList) {
        thumAdapter.update(mydataList);
    }

    public static AllFragment newInstance(String pos) {

        Bundle args = new Bundle();
        AllFragment fragment = new AllFragment();
        args.putString("tab", pos);
        fragment.setArguments(args);
        return fragment;
    }

}