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

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;
import ch.spherIC.recurvebowsight.R;
import ch.spherIC.recurvebowsight.databinding.FragmentEquipmentChooseBinding;
import ch.spherIC.recurvebowsight.model.IArcherySetup;
import ch.spherIC.recurvebowsight.placeholder.PlaceholderContent.PlaceholderItem;

/**
 * {@link RecyclerView.Adapter} that can display a {@link PlaceholderItem}.
 * TODO: Replace the implementation with code for your data type.
 */
public class EquipmentRecyclerViewAdapter extends RecyclerView.Adapter<EquipmentRecyclerViewAdapter.ViewHolder> {

    private final List<IArcherySetup> archerySetups;
    private final Context context;

    public EquipmentRecyclerViewAdapter(List<IArcherySetup> items, Context context) {
        this.context = context;
        this.archerySetups = items;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(FragmentEquipmentChooseBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        IArcherySetup archerySetup = archerySetups.get(position);
        holder.archerySetup = archerySetup;
        holder.mSetupNameView.setText(getSetupName(archerySetup));
        holder.mRiserNameView.setText(getRiserName(archerySetup));
        holder.mSightNameView.setText(getSightName(archerySetup));

        holder.itemView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                View setupCardView = v.findViewById(R.id.card_view_setup);
                View setupCardViewBorder = v.findViewById(R.id.card_view_border);
                float alpha = setupCardView.getAlpha();
                int horzMargin = ((ConstraintLayout.LayoutParams) setupCardViewBorder.getLayoutParams()).leftMargin;
                int vertMargin = ((ConstraintLayout.LayoutParams) setupCardViewBorder.getLayoutParams()).topMargin;
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    alpha = 0.75f;
                    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) setupCardViewBorder.getLayoutParams();
                layoutParams.setMargins(horzMargin, vertMargin, horzMargin, vertMargin);
                setupCardViewBorder.requestLayout();
                return false;
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(EquipmentRecyclerViewAdapter.this.context,
                               context.getString(R.string.caption_setup_choosen) + holder.archerySetup,
                               Toast.LENGTH_SHORT).show();

                Bundle bundle = new Bundle();
                bundle.putParcelable("setup", holder.archerySetup);
                Navigation.findNavController(v).navigate(R.id.nav_calcsight, bundle);
            }
        });
    }

    private String getSetupName(IArcherySetup archerySetup) {
        return archerySetup.getId() == -999 ? this.context.getString(R.string.caption_NoSelection) : archerySetup.getName();
    }

    private String getRiserName(IArcherySetup archerySetup) {
        return archerySetup.getId() == -999 ? "-" : archerySetup.getRiserName();
    }

    private String getSightName(IArcherySetup archerySetup) {
        return archerySetup.getId() == -999 ? "-" : archerySetup.getSightName();
    }

    @Override
    public int getItemCount() {
        return archerySetups.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final TextView mSetupNameView;
        public final TextView mRiserNameView;
        public final TextView mSightNameView;
        public IArcherySetup archerySetup;

        public ViewHolder(FragmentEquipmentChooseBinding binding) {
            super(binding.getRoot());
            mRiserNameView = binding.riserName;
            mSightNameView = binding.sightName;
            mSetupNameView = binding.setupName;
        }

        @Override
        public String toString() {
            return super.toString() + " '" + mSetupNameView.getText() + "'";
        }
    }
}