import 'package:flutter/cupertino.dart';
import 'package:yumi/shared/business_logic/models/res/mic_res.dart';

import 'package:yumi/app/constants/sc_global_config.dart';
import 'package:yumi/shared/tools/sc_gift_vap_svga_manager.dart';
import 'package:yumi/shared/business_logic/models/res/gift_res.dart';

typedef GiftProvider = SocialChatGiftSystemManager;

class SocialChatGiftSystemManager extends ChangeNotifier {
  ///幸运礼物就不显示这个特效
  bool hideLGiftAnimal = false;

  ///挡位是否已经播放了
  Map<num, bool> isPlayed = {
    10: false,
    20: false,
    30: false,
    50: false,
    66: false,
    88: false,
    100: false,
    200: false,
    300: false,
    400: false,
    500: false,
    666: false,
    777: false,
    888: false,
    1000: false,
    1500: false,
    2000: false,
    3000: false,
    5000: false,
    10000: false,
    15000: false,
    20000: false,
    25000: false,
    30000: false,
    35000: false,
    40000: false,
    45000: false,
    50000: false,
    55000: false,
    60000: false,
    65000: false,
    70000: false,
    75000: false,
    80000: false,
    85000: false,
    90000: false,
    95000: false,
    100000: false,
  };

  ///幸运礼物获得的金币总额
  num luckGiftObtainCoins = 0;

  ///单次中奖的金币
  num awardAmount = 0;

  ///赠送的数量
  num number = 0;

  bool isManyPeople = false;

  MicRes? toUser;

  SocialChatGiftRes? gift;

  double giftAnimSize = 1;
  double obtainCoinsAnimSize = 1;

  double awardAmountAnimSize = 1;

  void toggleGiftAnimationVisibility(bool isHide) {
    hideLGiftAnimal = isHide;
    notifyListeners();
  }

  void clearAllGiftData() {
    gift = null;
    luckGiftObtainCoins = 0;
    number = 0;
    isManyPeople = false;
    toUser = null;

    isPlayed = {
      10: false,
      20: false,
      30: false,
      50: false,
      66: false,
      88: false,
      100: false,
      200: false,
      300: false,
      400: false,
      500: false,
      666: false,
      777: false,
      888: false,
      1000: false,
      1500: false,
      2000: false,
      3000: false,
      5000: false,
      10000: false,
      15000: false,
      20000: false,
      25000: false,
      30000: false,
      35000: false,
      40000: false,
      45000: false,
      50000: false,
      60000: false,
      65000: false,
      70000: false,
      75000: false,
      80000: false,
      85000: false,
      90000: false,
      95000: false,
      100000: false,
    };
    notifyListeners();
  }

  void modifyLuckyGiftCount(
    num n,
    bool isManyPeople,
    MicRes first,
    SocialChatGiftRes? checkedGift,
  ) {
    this.number = number + n;
    this.isManyPeople = isManyPeople;
    this.toUser = first;
    this.gift = checkedGift;
    giftAnimSize = 1.4;
    notifyListeners();
    startGiftAnimation();
  }

  void updateLuckyRewardAmount(num n) {
    this.awardAmount = n;
    this.luckGiftObtainCoins = luckGiftObtainCoins + n;
    this.obtainCoinsAnimSize = 1.4;
    this.awardAmountAnimSize = 1.4;
    notifyListeners();
  }

  void startGiftAnimation() {
    isPlayed.forEach((k, v) {
      if (k < number + 1 && !v) {
        playVisualEffect(k);
      }
    });
  }

  void playVisualEffect(num n) {
    if (!(isPlayed[n] ?? false)) {
      if (SCGlobalConfig.isLuckGiftSpecialEffects) {
        if (n > 9999) {
          SCGiftVapSvgaManager().play(
            "sc_images/room/anim/luck_gift_count_5000_mor.mp4",
            priority: 200,
          );
        } else {
          SCGiftVapSvgaManager().play(
            "sc_images/room/anim/luck_gift_count_$n.mp4",
            priority: 200,
          );
        }
      }
    }
    isPlayed[n] = true;
  }
}
