import 'dart:convert';

import 'package:unit2/utils/request.dart';

import '../../model/profile/other_information/organization_memberships.dart';
import 'package:http/http.dart' as http;

import '../../utils/urls.dart';

class OrganizationMembershipServices {
  static final OrganizationMembershipServices _instance =
      OrganizationMembershipServices();
  static OrganizationMembershipServices get instance => _instance;

  Future<List<OrganizationMembership>> getOrgMemberships(
      int profileId, String token) async {
    List<OrganizationMembership> orgMemberships = [];
    String authToken = "Token $token";
    String path = "${Url.instance.getOrgMemberShips()}$profileId/";
    Map<String, String> headers = {
      'Content-Type': 'application/json; charset=UTF-8',
      'Authorization': authToken
    };
    try {
      http.Response response = await Request.instance
          .getRequest(path: path, param: {}, headers: headers);
      if (response.statusCode == 200) {
        Map data = jsonDecode(response.body);
        if (data['data'] != null) {
          data['data'].forEach((var org) {
            OrganizationMembership organizationMembership =
                OrganizationMembership.fromJson(org);
            orgMemberships.add(organizationMembership);
          });
        }
      }
    } catch (e) {
      throw e.toString();
    }
    return orgMemberships;
  }
}
