import 'package:package_info/package_info.dart';

import '../../api/apiResponse.dart';
import '../../api/mhsl/breaker/getBreakers/getBreakersCache.dart';
import '../../api/mhsl/breaker/getBreakers/getBreakersResponse.dart';
import '../dataHolder.dart';

class BreakerProps extends DataHolder {
  GetBreakersResponse? _getBreakersResponse;
  GetBreakersResponse get getBreakersResponse => _getBreakersResponse!;

  PackageInfo? packageInfo;
  
  String? isBlocked(BreakerArea? type) {
    if(packageInfo == null) {
      PackageInfo.fromPlatform().then((value) => packageInfo = value);
      return null;
    }

    if(primaryLoading()) return null;
    GetBreakersResponse breakers = _getBreakersResponse!;

    if(breakers.global.areas.contains(type)) return breakers.global.message;

    int selfVersion = int.parse(packageInfo!.buildNumber);
    for(var key in breakers.regional.keys) {
      GetBreakersReponseObject value = breakers.regional[key]!;

      if(int.parse(key.split("b")[1]) >= selfVersion) {
        if(value.areas.contains(type)) return value.message;
      }
    }

    return null;
  }

  @override
  List<ApiResponse?> properties() {
    return [_getBreakersResponse];
  }

  @override
  void run() {
    GetBreakersCache(
      onUpdate: (GetBreakersResponse getBreakersResponse) {
        _getBreakersResponse = getBreakersResponse;
        notifyListeners();
      }
    );
  }
}