import 'dart:convert';

import 'package:tour_travel_agr/api/response_api.dart';
import 'package:tour_travel_agr/helper/prefs.dart';
import 'package:tour_travel_agr/main.dart';
import 'package:tour_travel_agr/resource/strings.dart';

class Api {
  static Future<ApiResponse> login(
    String mobilePhone,
    String password,
  ) async {
    String apiUrl = "$baseUrl${endPoint}login";
    try {
      Map<String, dynamic> data = {
        "mobile_phone": mobilePhone,
        "password": password,
        "app_version": getAppVersion(),
      };
      String bodies = jsonEncode(data);
      dynamic jsonObject = await httpRequest(
        typePost,
        apiUrl,
        "login",
        bodies: bodies,
      );
      if (jsonObject == false) {
        return ApiResponse(error: true, msg: Strings.cantConnectToServer);
      } else {
        if (jsonObject['status'] == 'ok') {
          setSessionId(jsonObject['data']['session_id']);
          return ApiResponse(error: false, msg: "Success login");
        } else {
          return ApiResponse(
            error: true,
            msg: jsonObject['msg'],
          );
        }
      }
    } catch (e) {
      return ApiResponse(error: true, msg: Strings.serverError);
    }
  }

  static Future<ApiResponse> register(String name, String nik, String mobile,
      String password, String cPassword) async {
    String apiUrl = "$baseUrl${endPoint}registration";
    try {
      Map<String, dynamic> data = {
        "name": name,
        "nik": nik,
        "mobile_phoen": mobile,
        "password": password,
        "confirm_password": cPassword,
      };

      String bodies = jsonEncode(data);
      dynamic jsonObject = await httpRequest(
        typePost,
        apiUrl,
        "register",
        bodies: bodies,
      );
      if (jsonObject == false) {
        return ApiResponse(error: true, msg: Strings.cantConnectToServer);
      } else {
        if (jsonObject['status'] == 'ok') {
          Map<String, dynamic> returnData = {
            "register_id": jsonObject['data']['register_id'],
          };
          return ApiResponse(
            error: false,
            msg: "registration success",
            data: returnData,
          );
        } else {
          return ApiResponse(error: true, msg: jsonObject['msg']);
        }
      }
    } catch (e) {
      return ApiResponse(error: true, msg: Strings.serverError);
    }
  }

  static Future<ApiResponse> forgotPassword(String phone) async {
    String apiUrl = "$baseUrl${endPoint}forgot_password";
    try {
      Map<String, dynamic> data = {
        "mobile_phone": phone,
      };
      String bodies = jsonEncode(data);
      dynamic jsonObject = httpRequest(
        typePost,
        apiUrl,
        "forgotPassword",
        bodies: bodies,
      );
      if (jsonObject == false) {
        return ApiResponse(error: true, msg: Strings.cantConnectToServer);
      } else {
        if (jsonObject['status'] == 'ok') {
          return ApiResponse(
            error: false,
            msg: jsonObject['msg'],
          );
        } else {
          return ApiResponse(
            error: true,
            msg: jsonObject['msg'],
          );
        }
      }
    } catch (e) {
      return ApiResponse(error: false, msg: Strings.serverError);
    }
  }

  static Future<ApiResponse> resendOtpVerification(String registerId) async {
    String apiUrl = "$baseUrl${endPoint}resend_otp_registration";
    try {
      Map<String, dynamic> data = {
        "registration_id": registerId,
      };

      String bodies = jsonEncode(data);
      dynamic jsonObject = await httpRequest(
        typePost,
        apiUrl,
        "resendOtpVerification",
        bodies: bodies,
      );

      if (jsonObject == false) {
        return ApiResponse(error: true, msg: Strings.cantConnectToServer);
      } else {
        if (jsonObject['status'] == 'ok') {
          return ApiResponse(error: false, msg: "Success");
        } else {
          return ApiResponse(
            error: true,
            msg: jsonObject['msg'],
          );
        }
      }
    } catch (e) {
      return ApiResponse(error: true, msg: Strings.serverError);
    }
  }

  static Future<ApiResponse> resetPassword(
      String id, String password, String cPassword) async {
    String apiUrl = "$baseUrl${endPoint}reset_password";
    try {
      Map<String, dynamic> data = {
        "param_id": id,
        "password": password,
        "password_confirmation": cPassword,
      };
      String bodies = jsonEncode(data);
      dynamic jsonObject = await httpRequest(
        typePost,
        apiUrl,
        "resetPassword",
        bodies: bodies,
      );
      if (jsonObject == false) {
        return ApiResponse(error: true, msg: Strings.cantConnectToServer);
      } else {
        if (jsonObject['status'] == 'ok') {
          return ApiResponse(error: false, msg: "Success");
        } else {
          return ApiResponse(
            error: true,
            msg: jsonObject['msg'],
          );
        }
      }
    } catch (e) {
      return ApiResponse(error: true, msg: Strings.serverError);
    }
  }
}
