package com.app.simplitend.welcome.welcomepatient.fragments;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import com.app.simplitend.R;
import com.app.simplitend.databinding.ChooseRoleFragmentBinding;

public class ChooseRoleFragment extends Fragment {

    protected ChooseRoleFragmentBinding binding;

    private static final String SELECTED_ROLE_KEY = "selected_role";
    private static final int SENIOR = 1;
    private static final int CAREGIVER = 2;

    private int selected_role = SENIOR;

    public ChooseRoleFragment(){
        // required empty fragment
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = ChooseRoleFragmentBinding.inflate(inflater, container, false);

        initViews(savedInstanceState);

        clickEvents();

        return binding.getRoot();
    }

    // registers all click listeners
    private void clickEvents() {

        binding.seniorRole.setOnClickListener(v -> {
            makeRoleSelection(SENIOR);
            Navigation.findNavController(v).navigate(R.id.action_chooseRoleFragment_to_signInSignUpFragment);
        });

        binding.caregiverRole.setOnClickListener(v -> {
            makeRoleSelection(CAREGIVER);
            Navigation.findNavController(binding.getRoot()).navigate(R.id.action_chooseRoleFragment_to_cgOnBoardFragment);
        });

    }

    // initialize views1257
    private void initViews(Bundle savedInstanceState) {
        // checking if any previous instance existed
        if (savedInstanceState != null){
            // previously instance existed
            // thus, retrieving selected_role chosen

            selected_role = savedInstanceState.getInt(SELECTED_ROLE_KEY, SENIOR);
        }

        // making the role selection
        makeRoleSelection(selected_role);
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(SELECTED_ROLE_KEY, selected_role);
    }

    /*
        makes the check for each role visible or invisible according to the selected role
     */
    private void makeRoleSelection(int selectedRole){
        selected_role = selectedRole;

        if (selectedRole == SENIOR){
            binding.seniorCheck.setVisibility(View.VISIBLE);
            binding.caregiverCheck.setVisibility(View.GONE);
        }else{
            binding.caregiverCheck.setVisibility(View.VISIBLE);
            binding.seniorCheck.setVisibility(View.GONE);
        }
    }
}
