import 'package:didvan/models/enums.dart';
import 'package:didvan/models/new_statistic/exchange_model.dart';
import 'package:didvan/models/new_statistic/metal_model.dart';
import 'package:didvan/models/new_statistic/total_model.dart';
import 'package:didvan/models/requests/newstats_general.dart';
import 'package:didvan/providers/core.dart';
import 'package:didvan/services/network/request.dart';
import 'package:didvan/services/network/request_helper.dart';

class NewStockState extends CoreProvier {
  int? categories;
  int type = 1;
  int page = 1;
  int cat = 1;
  String search = "";
  String lastSearch = "";
  int lastPage = 1;
  List contents = [];
  int? length;

  Future<void> getStock({required int page}) async {
    this.page = page;

    lastSearch = search;
    if (page == 1) {
      contents.clear();
      appState = AppState.busy;
    }

    final service = RequestService(RequestHelper.statisticGeneral(
        args: StatisticsGeneralRequestArgs(
            page: page,
            q: search == '' ? null : search,
            categories: 6,
            type: type)));

    await service.httpGet();
    if (service.isSuccess) {
      if (type == 1) {
        contents.clear();
        final content = ExchangeContentModel.fromJson(service.result);
        contents.addAll(content.contents);
        length = contents.length;
      }

      if (type == 2) {
        contents.clear();
        final content = ExchangeContentModel.fromJson(service.result);
        contents.addAll(content.contents);
        length = contents.length;
      }

      if (type == 3) {
        contents.clear();
        final content = TotalContentModel.fromJson(service.result);
        contents.addAll(content.contents);
        length = contents.length;
      }

      if (type == 4) {
        contents.clear();
        final content = MetalContentModel.fromJson(service.result);
        contents.addAll(content.contents);
        length = contents.length;
      }

      appState = AppState.idle;
      return;
    }
    appState = AppState.failed;
  }

  void init() {
    Future.delayed(Duration.zero, () {
      getStock(page: 1);
    });
  }
}
