package com.wall.photography.picture.fragment;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.wall.photography.picture.databinding.DialogRateUsBinding;

public class RatingDialog extends DialogFragment {

    private DialogRateUsBinding binding;
    private int selectedRating = 0;
    private ImageView[] stars;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DialogRateUsBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 初始化星星数组
        stars = new ImageView[]{
                binding.star1,
                binding.star2,
                binding.star3,
                binding.star4,
                binding.star5
        };

        // 设置初始状态为灰色
        updateStarsDisplay(0);

        // 为每个星星设置点击事件
        for (int i = 0; i < stars.length; i++) {
            final int rating = i + 1;
            stars[i].setOnClickListener(v -> {
                selectedRating = rating;
                updateStarsDisplay(rating);
                binding.btnSubmit.setEnabled(true);
            });
        }

        // 取消按钮
        binding.btnCancel.setOnClickListener(v -> dismiss());

        // 提交按钮
        binding.btnSubmit.setOnClickListener(v -> {
            if (selectedRating > 0) {
                Toast.makeText(getContext(), 
                    "Thank you for rating us " + selectedRating + " stars!", 
                    Toast.LENGTH_SHORT).show();
                dismiss();
            }
        });
    }

    private void updateStarsDisplay(int rating) {
        for (int i = 0; i < stars.length; i++) {
            if (i < rating) {
                // 选中的星星 - 金色
                stars[i].setColorFilter(Color.parseColor("#FFD700"), PorterDuff.Mode.SRC_IN);
            } else {
                // 未选中的星星 - 灰色
                stars[i].setColorFilter(Color.parseColor("#CCCCCC"), PorterDuff.Mode.SRC_IN);
            }
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        if (dialog.getWindow() != null) {
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        return dialog;
    }

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

