package com.app.simplitend.welcome.welcomecg.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 androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import androidx.viewpager2.widget.ViewPager2;

import com.app.simplitend.R;
import com.app.simplitend.databinding.CgOnboardFragmentBinding;
import com.app.simplitend.welcome.welcomecg.CgOnBoardAdapter;

public class CgOnBoardFragment extends Fragment {

    protected CgOnboardFragmentBinding binding;

    public CgOnBoardFragment(){
        // required
    }

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

        initViews();

        clickEvents();

        return binding.getRoot();
    }

    private void clickEvents() {

//        binding.nextBtn.setOnClickListener(v -> {
//            int current_item = binding.viewPager.getCurrentItem();
//
//            if (current_item >= 0 && current_item < 2){
//                binding.viewPager.setCurrentItem(current_item + 1, true);
//            }
//
//        });
//
//        binding.getsStarted.setOnClickListener(v -> {
//            Navigation.findNavController(v).navigate(R.id.action_cgOnBoardFragment_to_cgSignInFragment);
//        });
//
//        binding.skip.setOnClickListener(v -> {
//            Navigation.findNavController(v).navigate(R.id.action_cgOnBoardFragment_to_cgSignInFragment);
//        });

        binding.login.setOnClickListener(v -> {
            Navigation.findNavController(v).navigate(R.id.action_cgOnBoardFragment_to_cgSignInFragment);
        });

        binding.registerBtn.setOnClickListener(v -> {
            Navigation.findNavController(v).navigate(R.id.action_cgOnBoardFragment_to_cgHowToSetUpFragment);
        });

    }

    private void initViews() {

        setUpDynamicTextSizes();

        // initiating view pager
        CgOnBoardAdapter adapter = new CgOnBoardAdapter(requireActivity());
        binding.viewPager.setAdapter(adapter);

        binding.indicators.setViewPager(binding.viewPager);

        binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);

//                if (position == 2){
//                    binding.getsStarted.setVisibility(View.VISIBLE);
//                    binding.nextBtn.setVisibility(View.GONE);
//                    binding.skip.setVisibility(View.GONE);
//                }else {
//                    binding.getsStarted.setVisibility(View.GONE);
//                    binding.nextBtn.setVisibility(View.VISIBLE);
//                    binding.skip.setVisibility(View.VISIBLE);
//                }

                String title, subtitle;

                switch (position){
                    case 2:
                        title = getString(R.string.setup_a_geofence_to_protect_your_loved_one);
                        subtitle = getString(R.string.be_notified_if_your_loved_one_ventures_too_far_from_home_or_a_designated_safe_area);
                        break;
                    case 1:
                        title = getString(R.string.check_your_loved_one_s_medication_schedule);
                        subtitle = getString(R.string.application_will_notify_you_when_it_is_time_for_your_loved_one_to_take_his_her_medication);
                        break;
                    default:
                        title = getString(R.string.reinventing_connected_ncaregiving);
                        subtitle = getString(R.string.no_new_gadgets_monitor_your_loved_one_s_activity_using_his_or_her_smartphone);
                }

                binding.title.setText(title);
                binding.subTitle.setText(subtitle);

            }
        });

    }

    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.cg_title_tall));
            binding.subTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.cg_sub_title_tall));
        }else if (aspectRatio > 2){
            // screen is normal size
            binding.title.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.cg_title_tall_medium));
            binding.subTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.cg_sub_title_tall_medium));
        }else {
            // screen is smaller
            binding.title.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.cg_title_small));
            binding.subTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.cg_sub_title_small));
        }
    }

}
