import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:test_sa/controllers/api_routes/api_manager.dart';
import 'package:test_sa/controllers/api_routes/urls.dart';
import 'package:test_sa/models/service_request/spare_parts.dart';

class PartsProvider extends ChangeNotifier {
  // number of items call in each request
  final pageItemNumber = 20;

  //reset provider data
  void reset() {
    _parts = null;
    _stateCode = null;
  }

  // state code of current request to defied error message
  // like 400 customer request failed
  // 500 service not available
  int? _stateCode;

  int? get stateCode => _stateCode;

  // true if there is next pagein product list and false if not
  bool _nextPage = true;

  bool get nextPage => _nextPage;

  // contain user data
  // when user not login or register _user = null
  List<SparePartsWorkOrders>? _parts;

  List<SparePartsWorkOrders>? get parts => _parts;

  // when categories in-process _loading = true
  // done _loading = true
  // failed _loading = false
  bool _loading = false;

  bool get isLoading => _loading;

  set isLoading(bool isLoading) {
    _loading = isLoading;
    notifyListeners();
  }

  /// return -2 if request in progress
  /// return -1 if error happen when sending request
  /// return state code if request complete may be 200, 404 or 403
  /// for more details check http state manager
  /// lib\controllers\http_status_manger\http_status_manger.dart
  // Future<int> getParts({String? title}) async {
  //   if (_loading == true) return -2;
  //   _loading = true;
  //   notifyListeners();
  //   late Response response;
  //   try {
  //     response = await ApiManager.instance.post(URLs.getPartNumber, body: {if (title != null && title.isNotEmpty) "partName": title});
  //     _stateCode = response.statusCode;
  //     if (response.statusCode >= 200 && response.statusCode < 300) {
  //       // client's request was successfully received
  //       List categoriesListJson = json.decode(utf8.decode(response.bodyBytes));
  //       List<SparePart> page = categoriesListJson.map((part) => SparePart.fromJson(part)).toList();
  //       _parts ??= [];
  //       _parts!.addAll(page.map((e) => SparePartsWorkOrders(sparePart: e)).toList());
  //       _nextPage = page.length >= pageItemNumber;
  //     }
  //     _loading = false;
  //     notifyListeners();
  //     return response.statusCode;
  //   } catch (error) {
  //     _loading = false;
  //     _stateCode = -1;
  //     notifyListeners();
  //     return -1;
  //   }
  // }

  /// return -2 if request in progress
  /// return -1 if error happen when sending request
  /// return state code if request complete may be 200, 404 or 403
  /// for more details check http state manager
  /// lib\controllers\http_status_manger\http_status_manger.dart
  Future<List<SparePart>> getPartsList({num? assetId, String? partNo, String? partName}) async {
    late Response response;
    try {
      if (partNo != null) {
        response = await ApiManager.instance.post(URLs.getPartNumber, body: {"partNo": partNo, });
      } else {
        response = await ApiManager.instance.post(URLs.getPartNumber, body: {"partName": partName, });
      }
      List<SparePart> page = [];
      if (response.statusCode >= 200 && response.statusCode < 300) {
        // client's request was successfully received
        List categoriesListJson = json.decode(response.body)["data"];
        page = categoriesListJson.map((part) => SparePart.fromJson(part,false)).toList();
      }
      return page;
    } catch (error) {
      return [];
    }
  }
  // Implement this only for spare part request for now show and search on display name ....

  Future<List<SparePart>> getPartsListByDisplayName({num? assetId, String? displayName}) async {
    late Response response;
    try {
        response = await ApiManager.instance.post(URLs.getPartNumber, body: {"displayName": displayName, });
      List<SparePart> page = [];
      if (response.statusCode >= 200 && response.statusCode < 300) {
        // client's request was successfully received
        List categoriesListJson = json.decode(response.body)["data"];
        page = categoriesListJson.map((part) => SparePart.fromJson(part,true)).toList();
      }
      return page;
    } catch (error) {
      return [];
    }
  }

}
