package com.house.wallpapers.myactivity;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.house.wallpapers.R;

public class RateUsDialog extends Dialog {
    private ImageView star1, star2, star3, star4, star5;
    private int selectedStars = 0;
    private String packageName;

    public RateUsDialog(@NonNull Context context) {
        // 关键修改1：指定透明主题，避免系统默认背景干扰
        super(context, android.R.style.Theme_Translucent_NoTitleBar);
        this.packageName = context.getPackageName();
        initView();
    }

    private void initView() {
        setContentView(R.layout.activity_rate_us_dialog);
        Window window = getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.getDecorView().setPadding(0, 0, 0, 0);
            window.setLayout(
                    (int) (getContext().getResources().getDisplayMetrics().widthPixels * 0.8),
                    android.view.WindowManager.LayoutParams.WRAP_CONTENT
            );
        }

        // 可选修改：恢复点击外部可关闭弹窗，提升交互体验
        setCanceledOnTouchOutside(true);
        // 保留点击返回键可关闭弹窗（默认true，可显式声明）
        setCancelable(true);

        // 绑定星级控件
        star1 = findViewById(R.id.iv_star1);
        star2 = findViewById(R.id.iv_star2);
        star3 = findViewById(R.id.iv_star3);
        star4 = findViewById(R.id.iv_star4);
        star5 = findViewById(R.id.iv_star5);

        // 星级点击事件（保留原有逻辑）
        star1.setOnClickListener(v -> setStars(1));
        star2.setOnClickListener(v -> setStars(2));
        star3.setOnClickListener(v -> setStars(3));
        star4.setOnClickListener(v -> setStars(4));
        star5.setOnClickListener(v -> setStars(5));

        // 取消按钮事件（保留原有逻辑）
        findViewById(R.id.btn_cancel).setOnClickListener(v -> dismiss());

        // 关键修改4：完善评分按钮的跳转逻辑，跳转到应用商店
        findViewById(R.id.btn_rate).setOnClickListener(v -> {
            if (selectedStars > 0) {
                dismiss();
            } else {
                // 未选择星级时提示用户
                Toast.makeText(getContext(), "Please select a star rating first!", Toast.LENGTH_SHORT).show();
            }
        });
    }

    // 星级选中逻辑（保留原有逻辑，无修改）
    private void setStars(int count) {
        selectedStars = count;
        // 重置所有星级为未选中
        star1.setImageResource(R.drawable.star_unselected);
        star2.setImageResource(R.drawable.star_unselected);
        star3.setImageResource(R.drawable.star_unselected);
        star4.setImageResource(R.drawable.star_unselected);
        star5.setImageResource(R.drawable.star_unselected);

        // 根据选中数量设置星级为选中
        for (int i = 1; i <= count; i++) {
            switch (i) {
                case 1:
                    star1.setImageResource(R.drawable.star_selected);
                    break;
                case 2:
                    star2.setImageResource(R.drawable.star_selected);
                    break;
                case 3:
                    star3.setImageResource(R.drawable.star_selected);
                    break;
                case 4:
                    star4.setImageResource(R.drawable.star_selected);
                    break;
                case 5:
                    star5.setImageResource(R.drawable.star_selected);
                    break;
            }
        }
    }
}