// ignore_for_file: constant_identifier_names, non_constant_identifier_names

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:gozap_package/components/apiservices.dart';
import 'package:gozap_package/models/swap/swap_count_model.dart';
import 'package:gozap_package/models/swap/swap_history_model.dart';
import 'package:gozap_package/models/swap/swap_model.dart';
import 'package:gozap_package/models/user/user_model.dart';
import 'package:provider/provider.dart';

enum DashboardModelStatus {
  Ended,
  Loading,
  Error,
}

class DashboardModel extends ChangeNotifier {
  late DashboardModelStatus _status;
  late String _errorCode;
  late String _errorMessage;

  late SwapModule? _swapModule;

  String get errorCode => _errorCode;
  String get errorMessage => _errorMessage;
  DashboardModelStatus get status => _status;
  SwapModule? get swapModule => _swapModule;

  DashboardModel();

  DashboardModel.instance(BuildContext context) {
    // ignore: todo
    //TODO Add code here
    getter(context);
  }

  Future<void> getter(BuildContext context) async {
    _status = DashboardModelStatus.Loading;
    notifyListeners();
    try {
      String UserID = Provider.of<userChangeNotifier>(context, listen: false)
          .user!
          .UserID
          .toString();
      var mapData = await APIService(context).postData(
          "User/",
          {"UserID": UserID},
          false);
      if (mapData != null) {
        UserModule userModule = UserModule.fromJson(mapData["UserData"]);
        SwapCountModule remaingSwapModule =
            SwapCountModule.fromJson(mapData["SwapData"]["RemainingData"]);
        SwapCountModule referSwapModule =
            SwapCountModule.fromJson(mapData["SwapData"]["ReferData"]);
        SwapCountModule rechargeSwapModule =
            SwapCountModule.fromJson(mapData["SwapData"]["RechargeData"]);
        SwapCountModule freeSwapModule =
            SwapCountModule.fromJson(mapData["SwapData"]["FreeData"]);
        List<SwapHistoryModule> swapHistoryList = [];

        for (var element in mapData["SwapData"]["HistoryData"]["History"]) {
          swapHistoryList.add(SwapHistoryModule.fromJson(element));
        }
        SwapHistoryMetaModule swapHistoryMetaModule = SwapHistoryMetaModule(
            TotalCount: mapData["SwapData"]["HistoryData"]["TotalCount"],
            FreeSwapCount: mapData["SwapData"]["HistoryData"]["FreeSwapCount"],
            PaidSwapCount: mapData["SwapData"]["HistoryData"]["PaidSwapCount"],
            SwapHistoryList: swapHistoryList);

        _swapModule = SwapModule(
            User: userModule,
            RemaingSwap: remaingSwapModule,
            ReferSwap: referSwapModule,
            RechargeSwap: rechargeSwapModule,
            FreeSwap: freeSwapModule,
            SwapHistoryMeta: swapHistoryMetaModule);
      }
    } catch (e) {
      _swapModule = null;
      if (kDebugMode) {
        print(e);
      }
    }
    _status = DashboardModelStatus.Ended;
    notifyListeners();
  }

  void setter() {
    _status = DashboardModelStatus.Loading;
    notifyListeners();

    // ignore: todo
    //TODO Add code here

    _status = DashboardModelStatus.Ended;
    notifyListeners();
  }

  void update() {
    _status = DashboardModelStatus.Loading;
    notifyListeners();

    // ignore: todo
    //TODO Add code here

    _status = DashboardModelStatus.Ended;
    notifyListeners();
  }

  void remove() {
    _status = DashboardModelStatus.Loading;
    notifyListeners();

    // ignore: todo
    //TODO Add code here

    _status = DashboardModelStatus.Ended;
    notifyListeners();
  }
}
