package com.auto.clicker.autoclicker.ui.dialog;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.auto.clicker.autoclicker.R;
import com.auto.clicker.autoclicker.ui.activity.main.MainActivity;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

public class CreateModeSelectionDialog extends BottomSheetDialogFragment {

    public static final String EXTRA_FLOATING_MODE = "extra_floating_mode";
    public static final int FLOATING_SINGLE = 1;
    public static final int FLOATING_MULTI = 2;

    private int selectedMode = FLOATING_SINGLE;

    private ConstraintLayout layoutSingleMode;
    private ConstraintLayout layoutMultiMode;
    private ImageView iconSingle;
    private ImageView iconMulti;

    public CreateModeSelectionDialog() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_create_mode_selection, container, false);

        ImageView btnClose = view.findViewById(R.id.btn_close_dialog);
        layoutSingleMode = view.findViewById(R.id.layout_single_mode);
        layoutMultiMode = view.findViewById(R.id.layout_multi_mode);
        Button btnStartCreate = view.findViewById(R.id.btn_start_create);

        iconSingle = view.findViewById(R.id.icon_single);
        iconMulti = view.findViewById(R.id.icon_multi);

        btnClose.setOnClickListener(v -> dismiss());

        layoutSingleMode.setOnClickListener(v -> {
            setSelectedMode(FLOATING_SINGLE);
        });

        layoutMultiMode.setOnClickListener(v -> {
            setSelectedMode(FLOATING_MULTI);
        });

        btnStartCreate.setOnClickListener(v -> {
            if (selectedMode == 0) {
                Toast.makeText(getContext(), "请选择模式", Toast.LENGTH_SHORT).show();
                return;
            }
            navigateToMainActivity(selectedMode);
        });

        updateModeSelectionUI();

        return view;
    }

    private void setSelectedMode(int mode) {
        if (this.selectedMode != mode) {
            this.selectedMode = mode;
            updateModeSelectionUI();
        }
    }

    private void updateModeSelectionUI() {
        if (selectedMode == FLOATING_SINGLE) {
            layoutSingleMode.setSelected(true);
            iconSingle.setImageResource(R.drawable.ic_circle_filled_check);

            layoutMultiMode.setSelected(false);
            iconMulti.setImageResource(R.drawable.ic_circle_hollow);
        } else if (selectedMode == FLOATING_MULTI) {
            layoutMultiMode.setSelected(true);
            iconMulti.setImageResource(R.drawable.ic_circle_filled_check);

            layoutSingleMode.setSelected(false);
            iconSingle.setImageResource(R.drawable.ic_circle_hollow);
        }
    }

    private void navigateToMainActivity(int mode) {
        Intent intent = new Intent(getActivity(), MainActivity.class);
        intent.putExtra(EXTRA_FLOATING_MODE, mode);
        startActivity(intent);
        dismiss();
    }
}