package com.example.wallisyn.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.example.wallisyn.R;

public class AppSettingsFragment extends Fragment {

    private int selectedRating = 0;
    private AlertDialog ratingDialog;

    // 静态实例化方法
    public static AppSettingsFragment newInstance() {
        return new AppSettingsFragment();
    }

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

    private void initViews(View view) {
        if (getActivity() == null) return;

        // 仅保留Rate us功能
        View rateUsLayout = view.findViewById(R.id.rate_us_layout);
        if (rateUsLayout != null) {
            rateUsLayout.setOnClickListener(v -> showCustomRatingDialog());
        }
    }

    /**
     * 显示自定义评分弹窗
     */
    private void showCustomRatingDialog() {
        if (getContext() == null || ratingDialog != null && ratingDialog.isShowing()) {
            return;
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
        View dialogView = LayoutInflater.from(requireContext()).inflate(R.layout.dialog_rating, null);
        builder.setView(dialogView);

        ratingDialog = builder.create();
        if (ratingDialog.getWindow() != null) {
            ratingDialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
        }
        ratingDialog.setCancelable(true);

        // 初始化星星控件
        ImageView star1 = dialogView.findViewById(R.id.star1);
        ImageView star2 = dialogView.findViewById(R.id.star2);
        ImageView star3 = dialogView.findViewById(R.id.star3);
        ImageView star4 = dialogView.findViewById(R.id.star4);
        ImageView star5 = dialogView.findViewById(R.id.star5);
        if (star1 == null || star2 == null || star3 == null || star4 == null || star5 == null) {
            ratingDialog.dismiss();
            return;
        }

        // 星星点击监听器
        View.OnClickListener starClickListener = v -> {
            if (v.getTag() == null) return;
            try {
                int rating = Integer.parseInt(v.getTag().toString());
                updateStars(rating, star1, star2, star3, star4, star5);
                selectedRating = rating;
                TextView descriptionText = dialogView.findViewById(R.id.descriptionText);
                if (descriptionText != null) {
                    descriptionText.setTextColor(getResources().getColor(android.R.color.black));
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        };

        star1.setOnClickListener(starClickListener);
        star2.setOnClickListener(starClickListener);
        star3.setOnClickListener(starClickListener);
        star4.setOnClickListener(starClickListener);
        star5.setOnClickListener(starClickListener);

        // 按钮事件
        Button cancelButton = dialogView.findViewById(R.id.cancelButton);
        Button confirmButton = dialogView.findViewById(R.id.confirmButton);
        if (cancelButton != null) {
            cancelButton.setOnClickListener(v -> ratingDialog.dismiss());
        }
        if (confirmButton != null) {
            confirmButton.setOnClickListener(v -> onConfirmRating(dialogView));
        }

        ratingDialog.show();
    }

    /**
     * 处理评分确认逻辑（删除应用商店跳转，仅提示评分成功）
     */
    private void onConfirmRating(View dialogView) {
        if (selectedRating == 0) {
            TextView descriptionText = dialogView.findViewById(R.id.descriptionText);
            if (descriptionText != null) {
                descriptionText.setText("Please select a rating first!");
                descriptionText.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
            }
            return;
        }

        // 提示用户评分成功，替换原应用商店跳转逻辑
        Toast.makeText(getContext(), "Thanks for your " + selectedRating + "-star rating!", Toast.LENGTH_SHORT).show();
        ratingDialog.dismiss();
    }

    /**
     * 更新星星显示状态
     */
    private void updateStars(int rating, ImageView... stars) {
        for (ImageView star : stars) {
            star.setImageResource(R.drawable.ic_star_empty);
        }
        for (int i = 0; i < rating && i < stars.length; i++) {
            stars[i].setImageResource(R.drawable.ic_star_filled);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (ratingDialog != null && ratingDialog.isShowing()) {
            ratingDialog.dismiss();
        }
    }
}