package com.example.applock.fragment;

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

import androidx.annotation.LongDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
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.SuccessDialog;
import com.example.applock.listener.LockListener;
import com.example.applock.listener.SwitchListener;
import com.example.applock.lockadapter.SoftWareAdapter;
import com.example.applock.value.MyAllValues;
import com.example.applock.viewmodel.SoftWareLockVM;

import org.jetbrains.annotations.NotNull;


public class SoftWareLockFragment extends Fragment {

    private SoftWareLockVM mViewModel;
    private RecyclerView recyclerView;
    private SoftWareAdapter softAdapter;
    private boolean isLocked;
    private SuccessDialog successDialog;
    public static SoftWareLockFragment newInstancePage(boolean locked) {
        SoftWareLockFragment softWareLockFragment = new SoftWareLockFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(MyAllValues.FRAGMENT_TYPE,locked);
        softWareLockFragment.setArguments(bundle);
        return softWareLockFragment;
    }

    @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_soft_ware_lock, container, false);
        recyclerView = view.findViewById(R.id.recyclerView_apps);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(SoftWareLockVM.class);
        Bundle arguments = getArguments();
        if (arguments != null)
            isLocked = getArguments().getBoolean(MyAllValues.FRAGMENT_TYPE);
        mViewModel.UpDataAppLock(isLocked);
        initAppRecycle();
        mViewModel.getMutableLiveDataApp().observe(requireActivity(), list -> {
            softAdapter.setEntityApps(list);

        });

    }
    public void refresh() {
        mViewModel.UpDataAppLock(isLocked);
    }

    private void initAppRecycle() {
        softAdapter = new SoftWareAdapter(requireContext(), isLocked);
        softAdapter.setLockListener(new LockListener() {
            @Override
            public void onSwitch(String msg) {
                successDialog = new SuccessDialog(requireContext(),msg);
               successDialog.show(getChildFragmentManager(), "");
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
        recyclerView.setAdapter(softAdapter);

    }
}