import 'dart:io';

import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:puzzleweave/l10n/app_localizations.dart';
import 'package:puzzleweave/rating/rating_dialog.dart';
import 'package:puzzleweave/rating/rating_helper.dart';
import 'package:rate_my_app/rate_my_app.dart';

final Logger _log = Logger('rating_utils');

////////////////// rate relate /////////////////////
showRateDialog(BuildContext context) {
  _log.info("showRateDialog...");
  if (Platform.isIOS) {
    showIOSRateDialog(context);
  } else {
    showSelfRateDialog(context);
  }
}

showIOSRateDialog(BuildContext context) async {
  RateMyApp rateMyApp = RateMyApp(
    preferencesPrefix: 'rateMyApp_',
    minDays: 0, // Show rate popup on first day of install.
    minLaunches: 2, // Show rate popup after 1 launches of app after minDays is passed.
    googlePlayIdentifier: 'jigsort.solitaire.jigsaw.match.games',
    appStoreIdentifier: '6499138123',
  );
  await rateMyApp.init();

  if (context.mounted && rateMyApp.shouldOpenDialog) {
    rateMyApp.showRateDialog(context);
    RatingHelper.onRateSubmmit(5); // IOS跟踪不到评分，只能都写个5颗星
  } else {
    _log.info("should show iOS rate dialog but ios not allow to");
  }
}

showSelfRateDialog(BuildContext context) async {
  final dialog = RatingDialog(
    initialRating: 5.0,
    title: Text(
      AppLocalizations.of(context)!.rateUs,
      textAlign: TextAlign.center,
      style: const TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
    ),
    // encourage your user to leave a high rating?
    message: Text(AppLocalizations.of(context)!.rateMessage, textAlign: TextAlign.center, style: const TextStyle(fontSize: 15)),
    image: Image.asset("assets/icons/icon_round.png", height: 100),
    submitButtonText: AppLocalizations.of(context)!.submit,
    submitButtonTextStyle: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: Colors.white),
    enableComment: false,
    onCancelled: () {
      RatingHelper.onRateCancel();
    },
    onSubmitted: (response) {
      _log.info('rating: ${response.rating}, comment: ${response.comment}');
      RatingHelper.onRateSubmmit(response.rating);
    },
  );
  showDialog(
    context: context,
    barrierDismissible: false, // set to false if you want to force a rating
    builder: (context) => dialog,
  );
}
