package com.wall.dream.wallpapers.View;

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

import androidx.core.content.ContextCompat;

import com.wall.dream.wallpapers.R;

public class RateUsDialogHelper {

    private Dialog dialog;
    private ImageView[] stars;
    private int selectedRating = 0;
    private Drawable starFilled;
    private Drawable starOutline;

    public void show(Context context) {
        if (dialog != null && dialog.isShowing()) {
            return;
        }

        // 预加载资源（提前加载，避免在show时加载）
        if (starFilled == null) {
            starFilled = ContextCompat.getDrawable(context, R.drawable.star_filled_gag);
        }
        if (starOutline == null) {
            starOutline = ContextCompat.getDrawable(context, R.drawable.star_outline_gas);
        }

        // 创建对话框
        dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        // 加载布局
        dialog.setContentView(R.layout.dialog_rate_us_001);
        
        // 设置窗口属性
        Window window = dialog.getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(params);
        }

        dialog.setCanceledOnTouchOutside(true);
        
        // 初始化视图
        initViews();
        
        // 直接显示对话框
        dialog.show();
    }

    private void initViews() {
        if (dialog == null) return;
        
        // dialog.setContentView后，可以直接使用dialog.findViewById查找视图
        stars = new ImageView[5];
        stars[0] = dialog.findViewById(R.id.star1);
        stars[1] = dialog.findViewById(R.id.star2);
        stars[2] = dialog.findViewById(R.id.star3);
        stars[3] = dialog.findViewById(R.id.star4);
        stars[4] = dialog.findViewById(R.id.star5);

        // 设置星星点击事件
        for (int i = 0; i < stars.length; i++) {
            final int rating = i + 1;
            if (stars[i] != null) {
                // 确保星星可点击
                stars[i].setClickable(true);
                stars[i].setFocusable(true);
                stars[i].setOnClickListener(v -> {
                    selectedRating = rating;
                    updateStars();
                });
            }
        }

        // 取消按钮
        TextView btnCancel = dialog.findViewById(R.id.btn_cancel);
        if (btnCancel != null) {
            btnCancel.setOnClickListener(v -> dismiss());
        }

        // 评分按钮
        TextView btnRate = dialog.findViewById(R.id.btn_rate);
        if (btnRate != null) {
            btnRate.setOnClickListener(v -> {
                if (selectedRating > 0) {
                    // 可以在这里添加打开Google Play的逻辑
                }
                dismiss();
            });
        }
    }

    private void updateStars() {
        if (starFilled == null || starOutline == null) {
            return;
        }
        for (int i = 0; i < stars.length; i++) {
            if (stars[i] != null) {
                if (i < selectedRating) {
                    stars[i].setImageDrawable(starFilled);
                } else {
                    stars[i].setImageDrawable(starOutline);
                }
            }
        }
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public void cleanup() {
        dismiss();
        starFilled = null;
        starOutline = null;
        stars = null;
        dialog = null;
    }
}
