import 'dart:convert';
import 'package:design/design.dart';
import 'package:guru_app/analytics/guru_analytics.dart';
import 'package:guru_app/financial/iap/iap_manager.dart';
import 'package:guru_app/financial/product/product_model.dart';
import 'package:guru_app/guru_app.dart';
import 'package:guru_ui/pages/store/store_page.dart';
import 'package:guru_app/controller/assets_aware.dart';
import 'package:guru_popup/guru_popup.dart';
import 'package:guru_widgets/assetbar/guru_asset_bar.dart';
import 'package:guru_widgets/dialog/guru_dialog.dart';
import 'package:guru_utils/controller/lifecycle_controller.dart';
import 'package:guru_ui/localizations/ui_strings.dart';
import 'package:guru_widgets/theme/guru_theme.dart';
import 'package:rxdart/rxdart.dart';

class SubscriptionController extends LifecycleController with AssetsAware {
  final ScrollController scrollController = ScrollController();

  final BehaviorSubject<int> _selectProductIndexSubject = BehaviorSubject.seeded(1);

  Stream<int> get observableSelectProductIndex => _selectProductIndexSubject.stream;

  void updateSelectedProductIndex(int index) {
    _selectProductIndexSubject.add(index);
  }

  Future restoreSubscription() async {
    final oldStore = currentIapAssetStore.clone();
    await restorePurchases();
    final newStore = currentIapAssetStore;
    final added = <String>[];
    newStore.data.forEach((key, value) {
      if (oldStore.getAsset(key) == null) {
        added.add(key.sku);
      }
    });
    // GuruAnalytics.instance.logEventEx("premium_restore_clk",
    //     itemCategory: from,
    //     itemName: added.isEmpty ? "non" : added.join(","),
    //     parameters: {"level": RuntimeProperty.instance.gameAudit.currentLevel});
  }

  // Future purchase(IapProduct? product, String? offerId) async {
  //   GuruAnalytics.instance.logEventEx("premium_click",
  //       itemCategory: from,
  //       itemName: "${product?.sku}${offerId != null ? "_$offerId" : ""}",
  //       parameters: {"level": RuntimeProperty.instance.gameAudit.currentLevel});
  //   if (product == null) {
  //     return;
  //   }
  //   final appStrings = AppStrings.get();
  //   final loadingCompleter =
  //       DialogUtils.showLoadingDialog(appStrings.processing, onCompleted: () {});
  //   final success = await requestProduct(product).catchError((error, stacktrace) {
  //     Log.w("requestProduct error! $error", stackTrace: stacktrace);
  //     return false;
  //   });
  //   loadingCompleter.complete();
  //   if (success) {
  //     if (showBonusDialog) {
  //       ToastUtils.showCommonToast(appStrings.purchaseSuccess);
  //       if (await AppDB.instance.insertTodayPremiumDailyBonusEntity()) {
  //         await DialogUtils.showPremiumDailyBonusDialog("premium");
  //       }
  //       RouteCenter.instance.backPageAndClearTop(mainPath: Routes.premium.mainPath, result: true);
  //     } else {
  //       RouteCenter.instance.backPageAndClearTop(mainPath: Routes.premium.mainPath, result: true);
  //       ToastUtils.showCommonToast(appStrings.purchaseSuccess);
  //     }
  //     GuruAnalytics.instance.logEventEx("premium_scs",
  //         itemCategory: from,
  //         itemName: "${product.sku}${offerId != null ? "_$offerId" : ""}",
  //         parameters: {"level": RuntimeProperty.instance.gameAudit.currentLevel});
  //   } else {
  //     if (isIapCanceled) {
  //       ToastUtils.showCommonToast(appStrings.iapSuspended);
  //     } else {
  //       ToastUtils.showCommonToast(appStrings.checkNetworkAndGp);
  //     }
  //     RouteCenter.instance.back();
  //   }
  // }
}
