package com.example.quiz;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

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

import com.example.quiz.data.model.dtos.auth.User;
import com.example.quiz.databinding.FragmentUpdatePinBinding;
import com.example.quiz.utils.AuthNavigator;
import com.example.quiz.utils.LoaderDialog;
import com.example.quiz.utils.MessageDialog;
import com.example.quiz.utils.Result;
import com.example.quiz.utils.SessionManager;
import com.example.quiz.viewModel.LoginViewModel;
import com.example.quiz.viewModel.LogsViewModel;

import dagger.hilt.android.AndroidEntryPoint;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link UpdatePin#newInstance} factory method to
 * create an instance of this fragment.
 */
@AndroidEntryPoint
public class UpdatePin extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER

    // TODO: Rename and change types of parameters
    FragmentUpdatePinBinding binding;

    String oldPin;
    String newPin;
    String confirmation;

    LogsViewModel logsViewModel;

    AuthNavigator authNavigator;

    LoginViewModel viewModel;

    LoaderDialog loaderDialog;

    public UpdatePin() {
        // Required empty public constructor
    }

    // TODO: Rename and change types and number of parameters
    public static UpdatePin newInstance() {
        UpdatePin fragment = new UpdatePin();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        viewModel = new ViewModelProvider(this).get(LoginViewModel.class);
        logsViewModel = new ViewModelProvider(this).get(LogsViewModel.class);
        loaderDialog = new LoaderDialog(getContext());
        authNavigator = new AuthNavigator(getContext(), getParentFragmentManager(), new SessionManager(getContext()), new ViewModelProvider(this).get(LoginViewModel.class),new ViewModelProvider(this).get(LogsViewModel.class),this);
        // Inflate the layout for this fragment
        binding = FragmentUpdatePinBinding.inflate(inflater, container, false);

        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding.validate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                oldPin = binding.oldPin.getText().toString();
                newPin = binding.newPin.getText().toString();
                confirmation = binding.pinConfirmation.getText().toString();
                if(oldPin.length()<4 || newPin.length()<4 || confirmation.length()<4){
                    MessageDialog.showError(getContext(), "Le pin doit au mois avoir 4 Caractères!");
                    return;
                }
                if(!newPin.equals(confirmation)){
                    MessageDialog.showError(getContext(), "Les pins ne sont pas identiques!");
                    return;
                }
                viewModel.changePin(oldPin, newPin).observe(getViewLifecycleOwner(), new Observer<Result<User>>(){

                    @Override
                    public void onChanged(Result<User> userResult) {
                        switch (userResult.status){
                            case LOADING:{
                                loaderDialog.show("Mis à jour du pin");
                                break;
                            }
                            case ERROR:{
                                loaderDialog.dismiss();
                                MessageDialog.showError(getContext(), userResult.message);
                                break;
                            }
                            case SUCCESS:{
                                loaderDialog.dismiss();

                                MessageDialog.showSuccess(getContext(), "Pin mis à jour avec succès");
                                logsViewModel.insertLog(String.valueOf(userResult.data.getId()), "MIS A JOUR PIN", "Mise à jour du pin", System.currentTimeMillis());
                                SessionManager sessionManager = new SessionManager(getContext());
                                authNavigator.showPinDialog(()->{
                                    sessionManager.updateLastExpiredDate();
                                    getParentFragmentManager().popBackStack();
                                });
                            }
                        }
                    }
                });
            }
        });
        binding.cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getParentFragmentManager().popBackStack();
            }
        });
    }
}