/*
 * Copyright (C) 2022 - Florindo Smilari (spherIC)
 */
package ch.spherIC.recurvebowsight.ui.equipment;

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

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import ch.spherIC.recurvebowsight.R;
import ch.spherIC.recurvebowsight.databinding.FragmentEquipmentAddListBinding;
import ch.spherIC.recurvebowsight.ui.configuration.ArcherySetupConfigurationActivity;
import ch.spherIC.recurvebowsight.ui.configuration.ArrowConfigurationActivity;
import ch.spherIC.recurvebowsight.ui.configuration.RiserConfigurationActivity;
import ch.spherIC.recurvebowsight.ui.configuration.SightConfigurationActivity;

public class AddEquipmentFragment extends Fragment {

    private FragmentEquipmentAddListBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentEquipmentAddListBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        attachTouchListener(root, R.id.card_view_riser_border, R.id.card_view_riser_setup);
        attachTouchListener(root, R.id.card_view_sight_border, R.id.card_view_sight_setup);
        attachTouchListener(root, R.id.card_view_arrow_border, R.id.card_view_arrow_setup);
        attachTouchListener(root, R.id.card_view_equipment_border, R.id.card_view_equipment_setup);

        return root;
    }

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

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

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    float alpha = setupCardView.getAlpha();
                    int horzMargin = ((ConstraintLayout.LayoutParams) borderCardView.getLayoutParams()).leftMargin;
                    int vertMargin = ((ConstraintLayout.LayoutParams) borderCardView.getLayoutParams()).topMargin;
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        alpha = 0.95f;
                        horzMargin = v.getResources().getDimensionPixelSize(R.dimen.nav_header_horizontal_spacing_onclick);
                        vertMargin = v.getResources().getDimensionPixelSize(R.dimen.nav_header_vertical_spacing_onclick);
                    } else if (event.getAction() == MotionEvent.ACTION_UP) {
                        alpha = 1.0f;
                        horzMargin = v.getResources().getDimensionPixelSize(R.dimen.nav_header_horizontal_spacing);
                        vertMargin = v.getResources().getDimensionPixelSize(R.dimen.nav_header_vertical_spacing);
                    }

                    setupCardView.setAlpha(alpha);

                    ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) borderCardView.getLayoutParams();
                    layoutParams.setMargins(horzMargin, vertMargin, horzMargin, vertMargin);
                    borderCardView.requestLayout();

                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        if (v.getId() == R.id.card_view_riser_border) {
                            Intent riserConfigurationIntent = new Intent(v.getContext(), RiserConfigurationActivity.class);
                            startActivity(riserConfigurationIntent);
                        } else if (v.getId() == R.id.card_view_sight_border) {
                            Intent sightConfigurationIntent = new Intent(v.getContext(), SightConfigurationActivity.class);
                            startActivity(sightConfigurationIntent);
                        } else if (v.getId() == R.id.card_view_arrow_border) {
                            Intent arrowConfigurationIntent = new Intent(v.getContext(), ArrowConfigurationActivity.class);
                            startActivity(arrowConfigurationIntent);
                        } else if (v.getId() == R.id.card_view_equipment_border) {
                            Intent equipmentConfigurationIntent = new Intent(v.getContext(), ArcherySetupConfigurationActivity.class);
                            startActivity(equipmentConfigurationIntent);
                        }
                    }
                    return true;
                }
            });
        }
    }
}