package ch.spherIC.recurvebowsight.ui.userguide;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayout;

import org.jetbrains.annotations.NotNull;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import ch.spherIC.recurvebowsight.R;
import ch.spherIC.recurvebowsight.databinding.FragmentUserguideBinding;
import ch.spherIC.recurvebowsight.utilities.UnitConverter;

public class UserguideFragment extends Fragment {

    private FragmentUserguideBinding binding;
    private View root;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        UserguideViewModel slideshowViewModel = new ViewModelProvider(this).get(UserguideViewModel.class);
        this.binding = FragmentUserguideBinding.inflate(inflater, container, false);
        this.root = this.binding.getRoot();

        attachTouchListener(root, R.id.card_view_ug_riser);
        attachTouchListener(root, R.id.card_view_ug_sight);
        attachTouchListener(root, R.id.card_view_ug_as);

        return root;
    }

    @Override
    public void onConfigurationChanged(@NotNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        FlexboxLayout flexboxLayout = root.findViewById(R.id.flexboxlayout_userguide);
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) flexboxLayout.getLayoutParams();

        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            layoutParams.width = ConstraintLayout.LayoutParams.WRAP_CONTENT;
            layoutParams.height = ConstraintLayout.LayoutParams.MATCH_PARENT;
            flexboxLayout.setFlexDirection(FlexDirection.COLUMN);
        } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            layoutParams.width = ConstraintLayout.LayoutParams.MATCH_PARENT;
            layoutParams.height = ConstraintLayout.LayoutParams.WRAP_CONTENT;
            flexboxLayout.setFlexDirection(FlexDirection.ROW);
        }
        flexboxLayout.requestLayout();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    private void attachTouchListener(View root, int cardViewId) {
        View setupCardView = root.findViewById(cardViewId);
        if (setupCardView != null) {
            setupCardView.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    float alpha = v.getAlpha();
                    int layoutWidth = v.getWidth();
                    int layoutHeigth = v.getHeight();
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        alpha = 0.5f;
                        layoutWidth = UnitConverter.dpToPxH(140);
                        layoutHeigth = UnitConverter.dpToPxV(140);
                    } else if (event.getAction() == MotionEvent.ACTION_UP) {
                        alpha = 1.0f;
                        layoutWidth = UnitConverter.dpToPxH(150);
                        layoutHeigth = UnitConverter.dpToPxV(150);
                    }
                    v.setAlpha(alpha);

                    FlexboxLayout.LayoutParams layoutParams = (FlexboxLayout.LayoutParams) v.getLayoutParams();
                    layoutParams.width = layoutWidth;
                    layoutParams.height = layoutHeigth;
                    v.requestLayout();

                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        if (v.getId() == R.id.card_view_ug_riser) {
                            Intent userGuideRiserIntent = new Intent(v.getContext(), UserGuideRiserActivity.class);
                            startActivity(userGuideRiserIntent);
                        } else if (v.getId() == R.id.card_view_ug_sight) {
                            Intent userGuideSightIntent = new Intent(v.getContext(), UserGuideSightActivity.class);
                            startActivity(userGuideSightIntent);
                        } else if (v.getId() == R.id.card_view_ug_as) {
                            Intent userGuideArcherySetupIntent = new Intent(v.getContext(), UserGuideArcherySetupActivity.class);
                            startActivity(userGuideArcherySetupIntent);
                        }
                    }
                    return true;
                }
            });
        }
    }
}