package com.wall.engine.wallpaper;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

public class SettingFragment extends Fragment {

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

        // 1. 设置“收藏”按钮点击事件
        setupCollectionButton(view);
        // 2. 设置“Rate Us”按钮点击事件（评分弹窗）
        setupRateUsButton(view);


        return view;
    }

    // 收藏按钮逻辑（原代码保留）
    private void setupCollectionButton(View view) {
        View collectionLayout = view.findViewById(R.id.linearLayout1_image3);
        if (collectionLayout != null) {
            collectionLayout.setOnClickListener(v -> navigateToFavoriteActivity());
        }
    }

    private void navigateToFavoriteActivity() {
        Intent intent = new Intent(getActivity(), FavoriteActivity.class);
        startActivity(intent);
        if (getActivity() != null) {
            getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
    }

    private void setupRateUsButton(View view) {
        View rateUsLayout = view.findViewById(R.id.linearLayout1);
        if (rateUsLayout != null) {
            rateUsLayout.setOnClickListener(v -> showRateUsDialog());
        }
    }

    private void showRateUsDialog() {
        Context context = getContext();
        if (context == null) return;

        View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_rate_us, null);
        RatingBar ratingBar = dialogView.findViewById(R.id.ratingBar);
        Button btnCancel = dialogView.findViewById(R.id.btn_cancel);
        Button btnRateIt = dialogView.findViewById(R.id.btn_rate_it);

        AlertDialog dialog = new AlertDialog.Builder(context)
                .setView(dialogView)
                .create();

        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

        btnCancel.setOnClickListener(v -> dialog.dismiss());

        btnRateIt.setOnClickListener(v -> {
            float userRating = ratingBar.getRating();
            dialog.dismiss();
            String toastMsg;
            if (userRating == 5) {
                toastMsg = "Thank you so much for your five-star review！";
            } else if (userRating >= 3) {
                toastMsg = "Thank you" + userRating + "Star rating！";
            } else {
                toastMsg = "We apologize for not meeting your expectations, and we will continue to improve！";
            }
            Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show();
        });

        dialog.show();
    }

}