package com.example.applock.fragment;

import android.annotation.SuppressLint;
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.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.SoftWarePerssionDialog;
import com.example.applock.application.SoftWareLockApplication;
import com.example.applock.lockadapter.LockedAdapter;
import com.example.applock.lockadapter.SystemAdapter;
import com.example.applock.lockadapter.ThirdAdapter;
import com.example.applock.roombase.AppDataBase;
import com.example.applock.roombase.AppEntity;
import com.example.applock.roombase.SoftWareLockRoom;
import com.example.applock.value.onShowDialogCallback;

import java.util.List;

public class LockedFragment extends Fragment {
    private RecyclerView recyclerView_locked;
    private boolean locked = true;
    private List<AppEntity> list;
    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_locked, container, false);
        recyclerView_locked = view.findViewById(R.id.recyclerView_locked);
       setRecycleLocked();
        return view;
    }

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

    private void setRecycleLocked() {
        SystemAdapter lockedAdapter = new SystemAdapter(requireActivity());
        lockedAdapter.setOnShowDialogCallback(new onShowDialogCallback() {
            @Override
            public void onShowDialog(String msg) {
                Log.d(SoftWareLockApplication.TAG,"---lockFragemnt--onShowDialog---------------");
                msgDialog = new MsgDialog(msg);
                showDialog = true;

            }

            @Override
            public void onShowStepDialog() {
                if (dialog == null) {
                    dialog = new SoftWarePerssionDialog(requireActivity());
                }
                dialog.show(requireActivity().getSupportFragmentManager(), "");
            }
        });
        recyclerView_locked.setAdapter(lockedAdapter);
        recyclerView_locked.setLayoutManager(new GridLayoutManager(getActivity(),1));
        AppDataBase.getDatabaseInstance().getAppEntityDao().queryApp(locked).observe(getViewLifecycleOwner(), new Observer<List<AppEntity>>() {
            @Override
            public void onChanged(List<AppEntity> appEntities) {
                lockedAdapter.setData(appEntities);
                Log.d(SoftWareLockApplication.TAG,"---Locked---onChanged");
            }
        });

    }

}