import 'dart:convert';
import 'package:initial_folder/base_service.dart';
import 'package:initial_folder/helper/user_info.dart';
import 'package:initial_folder/models/update_incomplete_profile_model.dart';
import 'package:initial_folder/models/update_data_diri_model.dart';
import 'package:initial_folder/models/user_info_incomplete_model.dart';
import 'package:initial_folder/models/user_info_model.dart';
import 'package:initial_folder/models/data_diri_model.dart';
import 'package:http/http.dart' as http;

class UserInfoService {
  Future<UserInfoModel> getUserInfo(email) async {
    String? token = await UsersInfo().getToken();
    Uri url = Uri.parse('$baseUrl/auth/me?email=$email');

    var response = await http.get(url, headers: headerWithToken(token!));

    print(response.body);

    if (response.statusCode == 200) {
      var data = jsonDecode(response.body);
      UserInfoModel userInfoModel = UserInfoModel.fromJson(data);
      await UsersInfo()
          .setIdUser(int.tryParse(userInfoModel.data[0].idUser ?? ''));
      return userInfoModel;
    } else if (response.statusCode == 202) {
      var data = jsonDecode(response.body);
      UserInfoModel userInfoModel = UserInfoModel.fromJson(data);
      await UsersInfo()
          .setIdUser(int.tryParse(userInfoModel.data[0].idUser ?? ''));
      return userInfoModel;
    } else {
      throw Exception('Gagal ambil data');
    }
  }

  Future<DataDiriModel> getDataDiri() async {
    int? id = await UsersInfo().getIdUser();
    String? token = await UsersInfo().getToken();
    Uri url = Uri.parse('$baseUrl/users/users-info/$id');

    var response = await http.get(url, headers: headerWithToken(token!));

    if (response.statusCode == 200) {
      return DataDiriModel.fromJson(jsonDecode(response.body));
    } else if (response.statusCode == 202) {
      return DataDiriModel.fromJson(jsonDecode(response.body));
    } else {
      throw Exception('Gagal ambil data');
    }
  }

  Future getDataDiriADMIN() async {
    int? id = await UsersInfo().getIdUser();
    String? token = await UsersInfo().getToken();
    Uri url = Uri.parse('$baseUrl/users/users-info/$id');

    var response;

    if (token != null) {
      response = await http.get(url, headers: headerWithToken(token));
    } else {
      response = await http.get(url);
    }

    if (response.statusCode == 401) {
      return response.statusCode;
    } else if (response.statusCode == 200) {
      return response.statusCode;
    } else {
      return response.statusCode;
    }
  }

  Future<UpdateDataDiriModel> updateDataDiri({
    String? fullname,
    String? biograph,
    String? phone,
    String? email,
    String? twitter,
    String? facebook,
    String? linkedin,
    String? instagram,
    String? datebirth,
    String? gender,
    String? headline,
  }) async {
    int? id = await UsersInfo().getIdUser();
    String? token = await UsersInfo().getToken();
    Uri url = Uri.parse('$baseUrl/users/profile/user-profile/$id');

    var body = jsonEncode({
      "full_name": fullname,
      "biography": biograph,
      "phone": phone,
      "email": email,
      // "datebirth": datebirth,
      // "jenis_kel": gender,
      "headline": headline,
      "social_link": {
        "twitter": twitter,
        "facebook": facebook,
        "linkedin": linkedin,
        "instagram": instagram
      }
    });
    var response =
        await http.put(url, headers: headerWithToken(token!), body: body);
    if (response.statusCode == 200) {
      return UpdateDataDiriModel.fromJson(jsonDecode(response.body));
    } else if (response.statusCode == 403) {
      return UpdateDataDiriModel.fromJson(jsonDecode(response.body));
    } else {
      throw Exception('Gagal update data');
    }
  }

  Future<UpdateIncompleteProfileModel> updateIncompleteProfile({
    String? fullname,
    String? phone,
    String? email,
    String? newPassword,
    String? newConfirmPassword,
  }) async {
    String? token = await UsersInfo().getToken();
    Uri url = Uri.parse('$baseUrl/users/profile-completion');

    var body = jsonEncode({
      "phone": phone,
      "password": newPassword,
      "confirm_password": newConfirmPassword,
    });
    var response =
        await http.put(url, headers: headerWithToken(token!), body: body);
    if (response.statusCode == 200 ||
        response.statusCode == 400 ||
        response.statusCode == 404) {
      return UpdateIncompleteProfileModel.fromJson(jsonDecode(response.body));
    } else {
      throw Exception(
          'Gagal update data: update incomplete profile: ${response.body}');
    }
  }

  Future<UserInfoIncompleteModel> getUserInfoIncomplete() async {
    String? token = await UsersInfo().getToken();
    Uri url = Uri.parse('$baseUrl/users/users-info');

    var response = await http.get(url, headers: headerWithToken(token!));
    if (response.statusCode == 200) {
      UserInfoIncompleteModel userInfoIncompleteModel =
          UserInfoIncompleteModel.fromJson(jsonDecode(response.body));
      return userInfoIncompleteModel;
    } else if (response.statusCode == 202) {
      UserInfoIncompleteModel userInfoIncompleteModel =
          UserInfoIncompleteModel.fromJson(jsonDecode(response.body));

      return userInfoIncompleteModel;
    } else {
      throw Exception('Gagal ambil data');
    }
  }

  Future updatePassword(
      {required idUser,
      required String? email,
      required String? oldPassword,
      required String? password,
      required String? newPasswordConfirm}) async {
    String? token = await UsersInfo().getToken();
    Uri url = Uri.parse('$baseUrl/users/profile/user-credentials/$idUser');

    var body = jsonEncode({
      'email': email,
      'old_password': oldPassword,
      "password": password,
      "new_password_confirm": newPasswordConfirm,
    });

    var response =
        await http.put(url, headers: headerWithToken(token!), body: body);

    if (response.statusCode == 200) {
      var data = jsonDecode(response.body);
      var status = data['status'];
      if (status == 200) {
        return;
      } else {
        throw Exception('Gagal update password');
      }
    } else {
      throw Exception('Gagal update password');
    }
  }
}
