import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:unit2/model/login_data/employee_info/employee_info.dart';
import 'package:unit2/model/profile/basic_info.dart';
import 'package:unit2/model/profile/basic_information/adress.dart';
import 'package:unit2/model/profile/basic_information/citizenship.dart';
import 'package:unit2/model/profile/basic_information/contact_information.dart';
import 'package:unit2/model/profile/basic_information/identification_information.dart';
import 'package:unit2/model/profile/educational_background.dart';
import 'package:unit2/model/profile/eligibility.dart';
import 'package:unit2/model/profile/family_backround.dart';
import 'package:unit2/model/profile/learning_development.dart';
import 'package:unit2/model/profile/other_info.dart';
import 'package:unit2/model/profile/other_information/non_acedimic_recognition.dart';
import 'package:unit2/model/profile/profileInfomation.dart';
import 'package:unit2/model/profile/references.dart';
import 'package:unit2/model/profile/other_information/skills_and_hobbies.dart';
import 'package:unit2/model/profile/voluntary_works.dart';
import 'package:unit2/model/profile/work_history.dart';
import 'package:unit2/utils/request.dart';
import 'package:unit2/utils/urls.dart';

import '../../model/profile/basic_information/primary-information.dart';
import '../../model/profile/other_information/organization_memberships.dart';

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

  Future<ProfileInformation?> getProfile(String token, int id) async {
    String url = Url.instance.profileInformation();
    String path = url + id.toString();
    ProfileInformation? profileInformation0;
    PrimaryInformation? primaryInformation;
    List<WorkHistory> workExperiences = [];
    List<PersonalReference> references = [];
    List<MainAdress> addresses = [];
    List<Identification> identificationInformation = [];
    List<ContactInfo> contactInformation = [];
    List<EligibityCert> eligibilities = [];
    List<FamilyBackground> families = [];
    List<Citizenship> citizenships = [];
    List<LearningDevelopement> learningsDevelopments = [];
    List<EducationalBackground> educationalBackgrounds = [];
    List<VoluntaryWork> voluntaryWorks = [];
    List<SkillsHobbies> skillsHobbies = [];
    List<OrganizationMembership> orgMemberships = [];
    List<NonAcademicRecognition> nonAcademicRecognitions = [];
    Map<String, String> headers = {
      'Content-Type': 'application/json; charset=UTF-8',
      'Authorization': "Token $token"
    };
    // try{
    http.Response response = await Request.instance
        .getRequest(path: path, param: {}, headers: headers);
    if (response.statusCode == 200) {
      Map data = jsonDecode(response.body);

      // get primary information
      if (data['data']['basic_information']['primary_information'] != null) {
        primaryInformation = PrimaryInformation.fromJson(
            data['data']['basic_information']['primary_information']);
      } else {
        primaryInformation = null;
      }

      // get all contacts
      if (data['data']['basic_information']['contact_information'] != null) {
        data['data']['basic_information']['contact_information']
            .forEach((var contact) {
          ContactInfo contactInfo =
              ContactInfo.fromJson(contact['contact_info']);
          contactInformation.add(contactInfo);
        });
      }

      // get all addresses
      if (data['data']['basic_information']['addresses'] != null) {
        data['data']['basic_information']['addresses'].forEach((var address) {
          MainAdress mainAdress = MainAdress.fromJson(address);
          addresses.add(mainAdress);
        });
      }

      // get all identifications
      if (data['data']['basic_information']['identification_records'] != null) {
        data['data']['basic_information']['identification_records']!
            .forEach((var identity) {
          Identification identification = Identification.fromJson(identity);
          identificationInformation.add(identification);
        });
      }

      // get all family background
      if(data['data']['family_background'] != null){
        data['data']['family_background'].forEach((var family){
          FamilyBackground familyBackground = FamilyBackground.fromJson(family);
          families.add(familyBackground);
        });
      }

      //get all eligibilities
      if (data['data']['eligibilities'] != null) {
        data['data']['eligibilities']!.forEach((var cert) {
          EligibityCert eligibility = EligibityCert.fromJson(cert);
          eligibilities.add(eligibility);
        });
      }

      // get all citizenships
      if (data['data']['citizenship'] != null) {
        data['data']['citizenships']!.forEach((var citizenship) {
          Citizenship person = Citizenship.fromJson(citizenship);
          citizenships.add(person);
        });
      }
      // get all references;
      if (data['data']['personal_references'] != null) {
        data['data']['personal_references'].forEach((var person) {
          PersonalReference reference = PersonalReference.fromJson(person);
          references.add(reference);
        });
      }

      //get all learning and developments
      if (data['data']['learning_development'] != null) {
        data['data']['learning_development'].forEach((var training) {
          LearningDevelopement learnings =
              LearningDevelopement.fromJson(training);
          learningsDevelopments.add(learnings);
        });
      }

      //get all educational background
      if (data['data']['education_background'] != null) {
        data['data']['education_background'].forEach((var education) {
          EducationalBackground educationalBackground =
              EducationalBackground.fromJson(education);
          educationalBackgrounds.add(educationalBackground);
        });
      }

      // get all work history
      if (data['data']['work_experiences'] != null) {
        data['data']['work_experiences'].forEach((var work) {
          WorkHistory experience = WorkHistory.fromJson(work);
          workExperiences.add(experience);
        });
      }

      // get all voluntary works
      if (data['data']['voluntary_works'] != null) {
        data['data']['voluntary_works'].forEach((var work) {
          VoluntaryWork vwork = VoluntaryWork.fromJson(work);
          voluntaryWorks.add(vwork);
        });
      }

      // get all hobbies
      if (data['data']['other_information']['skills_hobbies'] != null) {
        data['data']['other_information']['skills_hobbies']
            .forEach((var skills_hobbies) {
          SkillsHobbies skillsAndHobbies =
              SkillsHobbies.fromJson(skills_hobbies);
          skillsHobbies.add(skillsAndHobbies);
        });
      }

      //get all organization memberships
      if (data['data']['other_information']['organization_memberships'] !=
          null) {
        data['data']['other_information']['organization_memberships']
            .forEach((var org) {
          OrganizationMembership organization =
              OrganizationMembership.fromJson(org);
          orgMemberships.add(organization);
        });
      }

//get all non academic recognition
      if (data['data']['other_information']['non_academic_records'] != null) {
        data['data']['other_information']['non_academic_records']
            .forEach((var recognition) {
          NonAcademicRecognition nonAcademicRecognition =
              NonAcademicRecognition.fromJson(recognition);
          nonAcademicRecognitions.add(nonAcademicRecognition);
        });
      }

      BasicInfo basicInfo = BasicInfo(
          contactInformation: contactInformation,
          primaryInformation: primaryInformation,
          identifications: identificationInformation,
          citizenships: citizenships,
          addresses: addresses);
      OtherInformation otherInformation = OtherInformation(
          skillsAndHobbies: skillsHobbies,
          orgMemberships: orgMemberships,
          nonAcademicRecognition: nonAcademicRecognitions);
      ProfileInformation profileInformation = ProfileInformation(
           families: families,
          otherInformation: otherInformation,
          workExperiences: workExperiences,
          basicInfo: basicInfo,
          eligibilities: eligibilities,
          references: references,
          learningsAndDevelopment: learningsDevelopments,
          educationalBackgrounds: educationalBackgrounds,
          voluntaryWorks: voluntaryWorks);
      profileInformation0 = profileInformation;
    }
    // }catch(e){
    //   throw(e.toString());
    // }
    return profileInformation0;
  }
}
