package com.example.applock.fragment;

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

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

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

import com.example.applock.R;
import com.example.applock.allmanager.MsgDialog;
import com.example.applock.allmanager.MyWorker;
import com.example.applock.allmanager.SoftWarePerssionDialog;
import com.example.applock.application.SoftWareLockApplication;
import com.example.applock.lockadapter.SystemAdapter;
import com.example.applock.roombase.AppDataBase;
import com.example.applock.roombase.AppEntity;
import com.example.applock.roombase.SoftWareLockRoom;
import com.example.applock.value.Mytool;
import com.example.applock.value.onShowDialogCallback;
import com.pgl.ssdk.S;

import java.util.List;

public class SystemFragment extends Fragment {

    private RecyclerView recyclerView_apps;
    private List<AppEntity> list;


    public static String KEY = "IS_SYSTEM";

    private MsgDialog msgDialog;
    private SoftWarePerssionDialog dialog;

    private boolean showDialog = false;

    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_system, container, false);
        recyclerView_apps = view.findViewById(R.id.recyclerView_apps);
        setRecycleSystem();
        return view;
    }

    public static SystemFragment newInstance(boolean isSystem) {

        Bundle args = new Bundle();

        SystemFragment fragment = new SystemFragment();
        args.putBoolean(KEY,isSystem);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(SoftWareLockApplication.TAG,"=====system=onResume  showDialog="+showDialog);
        if(showDialog){
            showDialog = false;
            if(getActivity()!= null&& msgDialog != null){
                msgDialog.show(getActivity().getSupportFragmentManager(), "");
            }
        }
    }

    private void setRecycleSystem() {
        SystemAdapter systemAdapter = new SystemAdapter(requireActivity());
        systemAdapter.setOnShowDialogCallback(new onShowDialogCallback() {
            @Override
            public void onShowDialog(String msg) {
                Log.d(SoftWareLockApplication.TAG,"----system onShowDialog111");
                msgDialog = new MsgDialog(msg);
                showDialog = true;
            }

            @Override
            public void onShowStepDialog() {
                if (dialog == null) {
                    dialog = new SoftWarePerssionDialog(requireActivity());
                }
                dialog.show(requireActivity().getSupportFragmentManager(), "");
            }
        });

        recyclerView_apps.setAdapter(systemAdapter);
        recyclerView_apps.setLayoutManager(new GridLayoutManager(getActivity(),1));
        SoftWareLockRoom.RUNIO(new Runnable() {
            @Override
            public void run() {
                boolean aBoolean = getArguments().getBoolean(KEY, true);
                list = AppDataBase.getDatabaseInstance().getAppEntityDao().queryApplfrom(aBoolean);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        systemAdapter.setData(list);
                    }
                });
            }
        });
    }

}