package com.wall.dream.wallpapers.View;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.DialogFragment;

import com.wall.dream.wallpapers.R;

public class RateUsDialog extends DialogFragment {

    private View rootView;
    private ImageView[] stars;
    private int selectedRating = 0;
    private Drawable starFilled;
    private Drawable starOutline;
    private Handler mainHandler;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置对话框样式，减少创建开销
        setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_DeviceDefault_Dialog);
        mainHandler = new Handler(Looper.getMainLooper());
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.dialog_rate_us_001, container, false);
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 立即初始化，但使用轻量方式
        if (getContext() != null) {
            // 预加载drawable资源
            starFilled = ContextCompat.getDrawable(requireContext(), R.drawable.star_filled_gag);
            starOutline = ContextCompat.getDrawable(requireContext(), R.drawable.star_outline_gas);
            initViews();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            dialog.setCanceledOnTouchOutside(true);
            Window window = dialog.getWindow();
            if (window != null) {
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        }
    }

    private void initViews() {
        stars = new ImageView[5];
        stars[0] = rootView.findViewById(R.id.star1);
        stars[1] = rootView.findViewById(R.id.star2);
        stars[2] = rootView.findViewById(R.id.star3);
        stars[3] = rootView.findViewById(R.id.star4);
        stars[4] = rootView.findViewById(R.id.star5);

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

        // 取消按钮
        TextView btnCancel = rootView.findViewById(R.id.btn_cancel);
        btnCancel.setOnClickListener(v -> dismiss());

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

    private void updateStars() {
        if (starFilled == null || starOutline == null) {
            return;
        }
        // 使用预加载的drawable，避免每次调用setImageResource
        for (int i = 0; i < stars.length; i++) {
            if (i < selectedRating) {
                stars[i].setImageDrawable(starFilled);
            } else {
                stars[i].setImageDrawable(starOutline);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // 清理资源
        if (mainHandler != null) {
            mainHandler.removeCallbacksAndMessages(null);
        }
        starFilled = null;
        starOutline = null;
        rootView = null;
    }
}
