package com.wall.turing.wallpapers.turingfragments;

import android.app.Dialog;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;

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

import com.wall.turing.wallpapers.R;
import com.wall.turing.wallpapers.databinding.DialogRateUs01Binding;
import com.wall.turing.wallpapers.databinding.SettingsPageBinding;

public class SettingsPage extends Fragment {

    private SettingsPageBinding vb;

    public static SettingsPage newInstance() {
        return new SettingsPage();
    }

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

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        // 添加状态栏高度适配
        com.wall.turing.wallpapers.uutils.AppUtils.addStatusBarPadding(vb.topTitleBar, requireContext());
        
        // 设置版本号
        try {
            PackageInfo packageInfo = requireActivity().getPackageManager()
                    .getPackageInfo(requireActivity().getPackageName(), 0);
            vb.tvVersion.setText(packageInfo.versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        
        // Rate Us 点击事件
        vb.layoutRateUs.setOnClickListener(v -> showRateUsDialog());
    }
    
    private void showRateUsDialog() {
        Dialog dialog = new Dialog(requireContext());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        DialogRateUs01Binding dialogBinding = DialogRateUs01Binding.inflate(getLayoutInflater());
        dialog.setContentView(dialogBinding.getRoot());
        
        // 设置对话框背景透明
        if (dialog.getWindow() != null) {
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        
        // 星星数组
        ImageView[] stars = {
            dialogBinding.star1,
            dialogBinding.star2,
            dialogBinding.star3,
            dialogBinding.star4,
            dialogBinding.star5
        };
        
        final int[] currentRating = {0};
        
        // 设置星星点击事件
        for (int i = 0; i < stars.length; i++) {
            final int rating = i + 1;
            stars[i].setOnClickListener(v -> {
                currentRating[0] = rating;
                updateStars(stars, rating);
            });
        }
        
        // 取消按钮
        dialogBinding.btnCancel.setOnClickListener(v -> dialog.dismiss());
        
        // 评分按钮
        dialogBinding.btnRate.setOnClickListener(v -> dialog.dismiss());
        
        dialog.show();
    }
    
    private void updateStars(ImageView[] stars, int rating) {
        for (int i = 0; i < stars.length; i++) {
            if (i < rating) {
                stars[i].setImageResource(R.drawable.star_xuanzw);
            } else {
                stars[i].setImageResource(R.drawable.star_weixuanze);
            }
        }
    }

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

