package com.auto.clicker.autoclicker.ui.fragment.setting;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.auto.clicker.autoclicker.R;
import com.auto.clicker.autoclicker.databinding.FragmentActionBinding;

public class ActionFragment extends Fragment {
    private FragmentActionBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentActionBinding.inflate(inflater, container, false);

        binding.intervalSelectorContainer.setOnClickListener(this::showIntervalUnitSelection);

        binding.unitSelectorContainer.setOnClickListener(this::showSwipeDurationUnitSelection);

        return binding.getRoot();
    }

    private void showIntervalUnitSelection(View anchorView) {
        // 加载弹窗布局
        View popupView = getLayoutInflater().inflate(R.layout.dialog_unit_selection, null);
        PopupWindow popupWindow = new PopupWindow(
                popupView,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                true // 设置为 true，以便点击外部区域时自动关闭
        );

        // 设置背景，否则点击外部无法关闭
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setOutsideTouchable(true);

        // 获取弹窗中的TextViews
        TextView tvMs = popupView.findViewById(R.id.tv_unit_ms);
        TextView tvSec = popupView.findViewById(R.id.tv_unit_sec);
        TextView tvMin = popupView.findViewById(R.id.tv_unit_min);

        // Interval 单位有：毫秒，秒，分钟
        tvMs.setVisibility(View.VISIBLE);
        tvSec.setVisibility(View.VISIBLE);
        tvMin.setVisibility(View.VISIBLE);

        tvMs.setOnClickListener(v -> {
            binding.intervalSelectedUnit.setText(getString(R.string.milliseconds_unit));
            popupWindow.dismiss();
            showInputValue("Interval", binding.etIntervalValue.getText().toString(), getString(R.string.milliseconds_unit));
        });

        tvSec.setOnClickListener(v -> {
            binding.intervalSelectedUnit.setText(getString(R.string.seconds_unit));
            popupWindow.dismiss();
            showInputValue("Interval", binding.etIntervalValue.getText().toString(), getString(R.string.seconds_unit));
        });

        tvMin.setOnClickListener(v -> {
            binding.intervalSelectedUnit.setText(getString(R.string.minutes_unit));
            popupWindow.dismiss();
            showInputValue("Interval", binding.etIntervalValue.getText().toString(), getString(R.string.minutes_unit));
        });

        // 显示弹窗，使其显示在anchorView下方
        popupWindow.showAsDropDown(anchorView);
    }

    private void showSwipeDurationUnitSelection(View anchorView) {
        // 加载弹窗布局
        View popupView = getLayoutInflater().inflate(R.layout.dialog_unit_selection, null);
        PopupWindow popupWindow = new PopupWindow(
                popupView,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                true // 设置为 true，以便点击外部区域时自动关闭
        );

        // 设置背景，否则点击外部无法关闭
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setOutsideTouchable(true);

        // 获取弹窗中的TextViews
        TextView tvMs = popupView.findViewById(R.id.tv_unit_ms);
        TextView tvSec = popupView.findViewById(R.id.tv_unit_sec);
        TextView tvMin = popupView.findViewById(R.id.tv_unit_min);

        // Swipe duration 单位只有：毫秒，秒
        tvMs.setVisibility(View.VISIBLE);
        tvSec.setVisibility(View.VISIBLE);
        tvMin.setVisibility(View.GONE); // 隐藏分钟选项

        tvMs.setOnClickListener(v -> {
            binding.tvSelectedUnit.setText(getString(R.string.milliseconds_unit));
            popupWindow.dismiss();
            showInputValue("Swipe Duration", binding.etDurationValue.getText().toString(), getString(R.string.milliseconds_unit));
        });

        tvSec.setOnClickListener(v -> {
            binding.tvSelectedUnit.setText(getString(R.string.seconds_unit));
            popupWindow.dismiss();
            showInputValue("Swipe Duration", binding.etDurationValue.getText().toString(), getString(R.string.seconds_unit));
        });

        // 显示弹窗，使其显示在anchorView下方
        popupWindow.showAsDropDown(anchorView);
    }

    private void showInputValue(String type, String value, String unit) {
        String message = type + " 值: " + (value.isEmpty() ? "未输入" : value) + ", 单位: " + unit;
        Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();

         if (type.equals("Interval")) {
             long intervalValue = value.isEmpty() ? 0 : Long.parseLong(value);
             // 根据 unit 进行单位转换或保存
         } else if (type.equals("Swipe Duration")) {
             long durationValue = value.isEmpty() ? 0 : Long.parseLong(value);
             // 根据 unit 进行单位转换或保存
         }
    }

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