package me.elieraad.bottomsheet;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;

import com.getcapacitor.Logger;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import static android.view.Gravity.CENTER_VERTICAL;

public class BottomSheet extends BottomSheetDialogFragment {

    public interface OnSelectListener {
        void onSelect(int index);
    }

    public interface OnCancelListener {
        void onCancel();
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
        this.cancelListener.onCancel();
    }

    private String title;
    private ActionSheetOption[] options;

    private OnSelectListener listener;
    private OnCancelListener cancelListener;

    public void setTitle(String title) {
        this.title = title;
    }

    public void setOptions(ActionSheetOption[] options) {
        this.options = options;
    }

    public void setOnSelectedListener(OnSelectListener listener) {
        this.listener = listener;
    }

    public void setOnCancelListener(OnCancelListener listener) {
        this.cancelListener = listener;
    }

    private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                dismiss();
            }
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {}
    };

    @Override
    @SuppressLint("RestrictedApi")
    public void setupDialog(Dialog dialog, int style) {
        super.setupDialog(dialog, style);

        if (options == null) {
            return;
        }

        CoordinatorLayout parentLayout = new CoordinatorLayout(getContext());

        LinearLayout layout = new LinearLayout(getContext());
        layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setMinimumHeight(800);
        for (int i = 0; i < options.length; i++) {
            final int optionIndex = i;

            LinearLayout linearLayout = new LinearLayout(getContext());
            linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            linearLayout.setPadding(32, 32, 32, 32);

            ImageView imageView = new ImageView(getContext());
            imageView.setImageResource(getResourceId("ic_" + options[i].getIcon(), "drawable", 0));
            imageView.setId(R.id.icon);

            LinearLayout.LayoutParams ivParams = new LinearLayout.LayoutParams(64, 64);
            ivParams.setMargins(8,8,8,8);
            imageView.setLayoutParams(ivParams);
            linearLayout.addView(imageView);

            TextView tv = new TextView(getContext());
            tv.setTextColor(Color.parseColor("#000000"));
            tv.setText(options[i].getTitle());
            tv.setTextSize(16);
            LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            tvParams.setMargins(8,8,8,8);
            tvParams.gravity = CENTER_VERTICAL;
            imageView.setLayoutParams(tvParams);
            tv.setPadding(16,8,8,8);

            linearLayout.addView(tv);

            linearLayout.setOnClickListener(
                    view -> {
                        Logger.debug("CliCKED: " + optionIndex);

                        if (listener != null) {
                            listener.onSelect(optionIndex);
                        }
                    }
            );
            layout.addView(linearLayout);
        }

        ScrollView scrollView = new ScrollView(getContext());
        scrollView.addView(layout);

        parentLayout.addView(scrollView);
        dialog.setContentView(parentLayout.getRootView());

        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) parentLayout.getParent()).getLayoutParams();
        CoordinatorLayout.Behavior behavior = params.getBehavior();

        if (behavior != null && behavior instanceof BottomSheetBehavior) {
            ((BottomSheetBehavior) behavior).addBottomSheetCallback(mBottomSheetBehaviorCallback);
        }
    }

    private int getResourceId(String name, String defType, int fallback) {
        try {
            if (name.isEmpty()) return fallback;
            int resId = getActivity().getResources().getIdentifier(name, defType, getActivity().getPackageName());
            return resId == 0 ? fallback : resId;
        } catch (Exception ex) {
            System.out.println("Couldn't find image");
            return fallback;
        }
    }
}
