package com.wall.proshot.wallpapers.myacctivity;

import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.wall.proshot.wallpapers.R;

public class MySettingActivity extends AppCompatActivity {

    // 声明星星控件（方便统一管理）
    private ImageView star1, star2, star3, star4, star5;
    // 记录用户选择的星级
    private int selectedStar = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_my_setting);

        LinearLayout llRateUs = findViewById(R.id.ll_rate_us);
        ImageView ivBack = findViewById(R.id.iv_back3);

        initVersionName();

        // 设置点击事件
        llRateUs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 弹出评分弹窗
                showRateUsDialog();
            }
        });
        if (ivBack != null) {
            ivBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 构建跳转 Intent
                    Intent intent = new Intent(MySettingActivity.this, MyMainActivity.class);
                    //关闭
                    finish();
                }
            });
        }
    }


    private void showRateUsDialog() {
        Dialog rateDialog = new Dialog(this);
        rateDialog.setContentView(R.layout.my_rate_us);

        // 关键：取消窗口默认背景（设为null），仅显示CardView的圆角区域
        rateDialog.getWindow().setBackgroundDrawable(null);
        // 1. 找到弹窗中的所有星星和按钮
        star1 = rateDialog.findViewById(R.id.star1);
        star2 = rateDialog.findViewById(R.id.star2);
        star3 = rateDialog.findViewById(R.id.star3);
        star4 = rateDialog.findViewById(R.id.star4);
        star5 = rateDialog.findViewById(R.id.star5);

        //TextView tvSelectedStar = rateDialog.findViewById(R.id.tv_selected_star);

        View btnCancel = rateDialog.findViewById(R.id.btn_cancel);
        View btnRateIt = rateDialog.findViewById(R.id.btn_rate_it);

        // 2. 给每个星星设置点击事件
        setStarClickListener();

        // 3. 取消按钮点击事件
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rateDialog.dismiss(); // 关闭弹窗
                // 引用字符串资源
                Toast.makeText(MySettingActivity.this,
                        getString(R.string.toast_cancel_rate),
                        Toast.LENGTH_SHORT).show();
            }
        });

        // 4. 评分按钮点击事件
        btnRateIt.setOnClickListener(v -> {
            if (selectedStar > 0) {
                // 格式化字符串（替换星级占位符）
                String rateToast = String.format(getString(R.string.toast_thank_for_rate), selectedStar);
                Toast.makeText(MySettingActivity.this, rateToast, Toast.LENGTH_SHORT).show();
            } else {
                // 引用字符串资源
                Toast.makeText(MySettingActivity.this,
                        getString(R.string.toast_please_select_star),
                        Toast.LENGTH_SHORT).show();
                return; // 未选星级时不关闭弹窗
            }
            rateDialog.dismiss();
        });

        rateDialog.show();
    }

    // 封装星星点击事件的方法
    private void setStarClickListener() {
        // 星星1点击事件
        star1.setOnClickListener(v -> {
            selectedStar = 1;
            updateStarState();
        });

        // 星星2点击事件
        star2.setOnClickListener(v -> {
            selectedStar = 2;
            updateStarState();
        });

        // 星星3点击事件
        star3.setOnClickListener(v -> {
            selectedStar = 3;
            updateStarState();
        });

        // 星星4点击事件
        star4.setOnClickListener(v -> {
            selectedStar = 4;
            updateStarState();
        });

        // 星星5点击事件
        star5.setOnClickListener(v -> {
            selectedStar = 5;
            updateStarState();
        });
    }

    // 更新星星的显示状态（黄色/灰色）
    private void updateStarState() {
        // 先重置所有星星为灰色
        resetAllStarsToGray();

        // 根据选中的星级设置对应星星为黄色
        switch (selectedStar) {
            case 1:
                star1.setImageResource(R.drawable.star_y);
                break;
            case 2:
                star1.setImageResource(R.drawable.star_y);
                star2.setImageResource(R.drawable.star_y);
                break;
            case 3:
                star1.setImageResource(R.drawable.star_y);
                star2.setImageResource(R.drawable.star_y);
                star3.setImageResource(R.drawable.star_y);
                break;
            case 4:
                star1.setImageResource(R.drawable.star_y);
                star2.setImageResource(R.drawable.star_y);
                star3.setImageResource(R.drawable.star_y);
                star4.setImageResource(R.drawable.star_y);
                break;
            case 5:
                star1.setImageResource(R.drawable.star_y);
                star2.setImageResource(R.drawable.star_y);
                star3.setImageResource(R.drawable.star_y);
                star4.setImageResource(R.drawable.star_y);
                star5.setImageResource(R.drawable.star_y);
                break;
            default:
                break;
        }

    }

    // 重置所有星星为灰色
    private void resetAllStarsToGray() {
        star1.setImageResource(R.drawable.star_g);
        star2.setImageResource(R.drawable.star_g);
        star3.setImageResource(R.drawable.star_g);
        star4.setImageResource(R.drawable.star_g);
        star5.setImageResource(R.drawable.star_g);
    }
    private void initVersionName() {
        TextView tvVersion = findViewById(R.id.tv_version); // 注意：需要给布局中的版本TextView设置id为tv_version
        try {
            // 获取包管理器
            PackageManager packageManager = getPackageManager();
            // 获取当前应用的包信息（第二个参数传0表示获取基本信息）
            PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
            // 获取版本名
            String versionName = packageInfo.versionName;
            // 设置版本名（拼接V前缀，和原布局风格一致）
            tvVersion.setText("V" + versionName);
        } catch (PackageManager.NameNotFoundException e) {
            // 异常处理：获取失败时显示默认值
            e.printStackTrace();
            tvVersion.setText(getString(R.string.setting_version));
        }
    }
}