import 'package:flutter/cupertino.dart';
import 'package:fluttertoast/fluttertoast.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/new_models/site.dart';
import 'package:test_sa/modules/demo_module/models/demo_request_model.dart';
import 'dart:convert';

class DemoProvider extends ChangeNotifier {
  Future<bool> addDemoRequest(Map<String, dynamic> body) async {
    try {
      Response response = await ApiManager.instance.post(URLs.addDemoRequest, body: body, showToast: false);
      if (response.statusCode >= 200 && response.statusCode < 300) {
        // String message = (jsonDecode(response.body)["data"] ?? "") + " " + (jsonDecode(response.body)["message"] ?? "");
        String message = (jsonDecode(response.body)["message"] ?? "");
        Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
        return true;
      }
      return false;
    } catch (error) {
      return false;
    }
  }

  bool isLoading = false;
  bool isSiteLoading = false;

  Future<DemoRequestModel?> getDemoById(int id) async {
    DemoRequestModel? demoRequestModel;
    try {
      Response response = await ApiManager.instance.get(URLs.getDemoRequestById + "/$id");
      if (response.statusCode >= 200 && response.statusCode < 300) {
        demoRequestModel = DemoRequestModel.fromJson(json.decode(response.body)["data"]);
      }
    } catch (error) {
      print(error);
    }
    return demoRequestModel;
  }

  Future<bool> acceptRejectRequest({required int requestId, required bool status}) async {
    try {
      Map<String, dynamic> payload = {"demoRequestId": requestId, "isApproved": status};
      Response response = await ApiManager.instance.post(URLs.updateDemoRequestByAssessor, body: payload, showToast: false);
      if (response.statusCode >= 200 && response.statusCode < 300) {
        String message = (jsonDecode(response.body)["message"] ?? "");
        Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
        return true;
      }
      return false;
    } catch (error) {
      return false;
    }
  }

  Future<bool> cancelRequest(int requestId) async {
    try {
      Response response = await ApiManager.instance.post(URLs.cancelDemoRequest + "?demoRequestId=$requestId", body: {}, showToast: false);
      if (response.statusCode >= 200 && response.statusCode < 300) {
        String message = (jsonDecode(response.body)["message"] ?? "");
        Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
        return true;
      }
      return false;
    } catch (error) {
      return false;
    }
  }

  Future<bool> updateDemoRequest(Map<String, dynamic> body) async {
    try {
      Response response = await ApiManager.instance.post(URLs.updateDemoRequest, body: body, showToast: false);
      if (response.statusCode >= 200 && response.statusCode < 300) {
        String message = (jsonDecode(response.body)["message"] ?? "");
        Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
        return true;
      } else if (response.statusCode >= 400 && response.statusCode < 500) {
        String message = (jsonDecode(response.body)["message"] ?? "");
        Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
      }
      return false;
    } catch (error) {
      return false;
    }
  }

  Future<bool> updateDemoPeriod({required int demoPeriodId, required int requestId, required bool isUpdateRequired}) async {
    try {
      Map<String, dynamic> payload = {"demoRequestId": requestId, "demoPeriodId": demoPeriodId, "isDemoPeriodExtentionRequired": isUpdateRequired};
      Response response = await ApiManager.instance.post(URLs.updateDemoPeriodByRequester, body: payload, showToast: false);
      if (response.statusCode >= 200 && response.statusCode < 300) {
        String message = (jsonDecode(response.body)["message"] ?? "");
        Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
        return true;
      }
      return false;
    } catch (error) {
      return false;
    }
  }

  Future<Site> getSiteData({required num? siteId}) async {
    Site siteObj = Site();
    isSiteLoading = true;
    notifyListeners();

    try {
      final response = await ApiManager.instance.get(
        "${URLs.getSiteAutoCompleteWithoutConditionSites}?id=$siteId",
      );

      if (response.statusCode >= 200 && response.statusCode < 300) {
        final List? siteData = json.decode(response.body)["data"];
        if (siteData != null && siteData.isNotEmpty) {
          siteObj = Site.fromJson(siteData.first);
        }
      }
    } catch (_) {}

    isSiteLoading = false;
    notifyListeners();
    return siteObj;
  }

  Future<bool> addDemoTrialOutcome(Map<String, dynamic> body) async {
    try {
      Response response = await ApiManager.instance.post(URLs.addDemoTrialOutcome, body: body);
      if (response.statusCode >= 200 && response.statusCode < 300) {
        // String message = (jsonDecode(response.body)["message"] ?? "");
        // Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
        return true;
      }
      return false;
    } catch (error) {
      return false;
    }
  }

// Future<DemoFormModel> getSiteData({
//   required num? siteId,
// }) async {
//   isSiteLoading = true;
//    DemoFormModel model=DemoFormModel();
//   notifyListeners();
//
//   try {
//     final response = await ApiManager.instance.get(
//       "${URLs.getSiteAutoCompleteWithoutConditionSites}?id=$siteId",
//     );
//
//     if (response.statusCode >= 200 && response.statusCode < 300) {
//       final List? siteData = json.decode(response.body)["data"];
//
//       if (siteData != null && siteData.isNotEmpty) {
//         model.site = Site.fromJson(siteData.first);
//       }
//
//       model.building = model.site?.buildings?.firstWhere(
//             (e) => e.value == model.building?.value,
//         orElse: () => Building(),
//       );
//
//       model.floor = model.building?.floors?.firstWhere(
//             (e) => e.value == model.floor?.value,
//         orElse: () => Floor(),
//       );
//
//       model.department = model.floor?.departments?.firstWhere(
//             (e) => e.departmentName == model.department?.departmentName,
//         orElse: () => Department(),
//       );
//
//     }
//
//     isSiteLoading = false;
//     notifyListeners();
//     return model;
//   } catch (e) {
//     isSiteLoading = false;
//     notifyListeners();
//     return model;
//   }
// }
}
