package com.draw.painting.canvas.presentation;

import android.app.Dialog;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.draw.painting.canvas.R;
import com.draw.painting.canvas.databinding.FragmentPreferencesBinding;
import com.draw.painting.canvas.core.SystemHelper;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

public class PreferencesFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private String mParam1;
    private String mParam2;
    private FragmentPreferencesBinding binding;

    public PreferencesFragment() {

    }
    public static PreferencesFragment newInstance() {
        PreferencesFragment fragment = new PreferencesFragment();
        Bundle args = new Bundle();

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentPreferencesBinding.inflate(getLayoutInflater());
        onInit();
        return binding.getRoot();
    }

    private void onInit(){
        String version = "1.0";
        PackageInfo info = SystemHelper.getInfo(requireContext());
        if(info!=null){
            version = info.versionName;
        }
        String format = String.format(getString(R.string.version_code), version);
        binding.textAppVersion.setText(format);
        
        // 设置 Rate Us 点击事件
        binding.rlRateUs.setOnClickListener(v -> showRateUsDialog());
    }
    
    /**
     * 显示评分对话框
     */
    private void showRateUsDialog() {
        View dialogView = LayoutInflater.from(requireContext()).inflate(R.layout.dialog_rating, null);
        
        Dialog dialog = new MaterialAlertDialogBuilder(requireContext())
                .setView(dialogView)
                .setCancelable(true)
                .create();
        
        RatingBar ratingBar = dialogView.findViewById(R.id.ratingBar);
        MaterialButton btnMaybeLater = dialogView.findViewById(R.id.btnMaybeLater);
        MaterialButton btnRateNow = dialogView.findViewById(R.id.btnRateNow);
        
        // 初始状态：未选择星星时，"Rate Now" 按钮置灰
        btnRateNow.setEnabled(false);
        btnRateNow.setAlpha(0.5f);
        
        // 监听评分变化
        ratingBar.setOnRatingBarChangeListener((bar, rating, fromUser) -> {
            if (rating > 0) {
                // 选择了星星，启用按钮
                btnRateNow.setEnabled(true);
                btnRateNow.setAlpha(1.0f);
            } else {
                // 未选择星星，禁用按钮
                btnRateNow.setEnabled(false);
                btnRateNow.setAlpha(0.5f);
            }
        });
        
        // "Maybe Later" 按钮
        btnMaybeLater.setOnClickListener(v -> dialog.dismiss());
        
        // "Rate Now" 按钮
        btnRateNow.setOnClickListener(v -> {
            float rating = ratingBar.getRating();
            
            if (rating == 0) {
                // 未选择星星，提示用户
                Toast.makeText(requireContext(), "Please select a rating", Toast.LENGTH_SHORT).show();
                return;
            }
            
            if (rating >= 4.0f) {
                dialog.dismiss() ;
            } else {
                // 评分低于 4 星，显示感谢信息
                Toast.makeText(requireContext(), R.string.thank_you_rating, Toast.LENGTH_SHORT).show();
            }
            
            dialog.dismiss();
        });
        
        dialog.show();
    }
    

}