// ignore_for_file: unnecessary_overrides, unrelated_type_equality_checks

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:IQ/app/global/static_informs.dart';
import 'package:IQ/app/modules/home/providers/language_currency.dart';
import 'package:IQ/app/modules/home/providers/packages.dart';
import 'package:IQ/app/modules/home/providers/service.dart';

import '../../home/models/packages_model.dart';

class AvailablePackagesController extends GetxController
    with WidgetsBindingObserver {
  final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();
  bool fetchPackagesData = false;
  // ignore: prefer_typing_uninitialized_variables
  var fetchLoadingPackagesDataVar;

  Future<void> onRefresh() async {
    fetchPackagesData = false;
    packages = null;
    fetchLoadingPackagesDataVar = fetchLoadingPackagesData();
    update();
  }

  fetchLoadingPackagesData() async {
    await getUserPackagesInforms();
    if (getUserPackagesInformsLoads) {
      fetchPackagesData = true;
      listOfPackages = packages?.data ?? [];
      update();
    }
  }

  String activiationType = '${service?.data?.profileName}';
  String subscriptionCurrency = languagesCurrencyModel?.data?.currency ?? '';
  String profileName = service?.data?.profileName ?? '';
  final oCcy = NumberFormat.currency(
    locale: 'ar',
    customPattern: '#,### \u00a4',
    decimalDigits: 0,
    symbol: '',
  );
  String subscribed = languagesCurrencyModel?.data?.currency ?? '';
  List<Data>? listOfPackages = packages?.data ?? [];

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.inactive ||
        state == AppLifecycleState.detached ||
        state == AppLifecycleState.paused) return;
    if (state == AppLifecycleState.resumed) {
      defaultLocale = Platform.localeName.split('_')[0];
      snackBarText = 'back_twice'.tr;
    }
  }

  @override
  void onInit() {
    fetchLoadingPackagesDataVar = fetchLoadingPackagesData();
    WidgetsBinding.instance.addObserver(this);

    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    WidgetsBinding.instance.removeObserver(this);
  }
}
