package com.draw.painting.line.ui;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.draw.painting.line.R;

public class RatingDialog extends Dialog {
    private Context context;
    private int selectedRating = 0;
    private ImageView[] stars;
    private TextView btnRateIt;

    public RatingDialog(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_rating);
        
        // 设置弹框样式
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = (int) (context.getResources().getDisplayMetrics().widthPixels * 0.85);
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(params);
            window.setBackgroundDrawableResource(android.R.color.transparent);
        }

        initViews();
        setupStars();
        setupButtons();
    }

    private void initViews() {
        stars = new ImageView[5];
        stars[0] = findViewById(R.id.star1);
        stars[1] = findViewById(R.id.star2);
        stars[2] = findViewById(R.id.star3);
        stars[3] = findViewById(R.id.star4);
        stars[4] = findViewById(R.id.star5);
        btnRateIt = findViewById(R.id.btnRateIt);
        
        // 初始化星星为未选中状态
        for (ImageView star : stars) {
            star.setImageResource(R.drawable.ic_star_off);
        }
    }

    private void setupStars() {
        for (int i = 0; i < stars.length; i++) {
            final int rating = i + 1;
            stars[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectedRating = rating;
                    updateStars();
                    updateRateButton();
                }
            });
        }
    }

    private void updateStars() {
        for (int i = 0; i < stars.length; i++) {
            if (i < selectedRating) {
                stars[i].setImageResource(R.drawable.ic_star_on);
            } else {
                stars[i].setImageResource(R.drawable.ic_star_off);
            }
        }
    }

    private void updateRateButton() {
        if (selectedRating > 0) {
            btnRateIt.setTextColor(Color.parseColor("#FFFFFF"));
        } else {
            btnRateIt.setTextColor(Color.parseColor("#CCCCCC"));
        }
    }

    private void setupButtons() {
        TextView btnCancel = findViewById(R.id.btnCancel);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        btnRateIt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dismiss();
            }
        });
    }


}

