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

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

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

import com.app.simplitend.R;
import com.app.simplitend.databinding.WelcomeFragmentBinding;
import com.app.simplitend.welcome.welcomepatient.mvvm.WelcomeViewModel;

public class WelcomeFragment extends Fragment {

    // view binding
    protected WelcomeFragmentBinding binding;

    public WelcomeFragment(){
        // required empty const.
    }

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

        setUpDynamicTextSizes();

        clickEvents();

        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Navigation.findNavController(binding.getRoot())
                .popBackStack(R.id.welcomeFragment, false);
    }

    private void setUpDynamicTextSizes() {
        // Calculate screen dimensions in pixels
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        int screenWidth = displayMetrics.widthPixels;
        int screenHeight = displayMetrics.heightPixels;

        // Calculate aspect ratio
        float aspectRatio = (float) screenHeight / (float) screenWidth;

        if (aspectRatio > 2.1){
            // screen is taller
            binding.title.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.welcome_title_tall));
            binding.subTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.welcome_sub_title_tall));
        }else if (aspectRatio > 2){
            // screen is normal size
            binding.title.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.welcome_title_tall_medium));
            binding.subTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.welcome_sub_title_tall_medium));
        }else {
            // screen is smaller
            binding.title.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.welcome_title_small));
            binding.subTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.welcome_sub_title_small));
        }
    }

    // Register click events
    private void clickEvents(){
        binding.nextBtn.setOnClickListener(v ->
                Navigation.findNavController(v)
                        .navigate(R.id.action_welcomeFragment_to_chooseRoleFragment)
        );
    }

}
