import {Injectable} from '@angular/core';
import {ICollection, ISchema, Resource, Service} from 'ngx-jsonapi';
import {Image, ImagesService} from './images.service';
import {Type, TypesService} from './types.service';
import * as moment from 'jalali-moment';
import {Base} from 'ngx-jsonapi/services/base';
import {Cities, CitiesService} from './cities.service';
import {Majore, MajoreService} from './majore.service';


@Injectable()
export class RequestsService extends Service<Requests> {
  public resource = Requests;
  public type = 'pishkhan-requests';
  public schema: ISchema = {
    relationships: {
      docIdentify : { // axe carte meli
        hasMany: false
      },
      docNational : { // axe shenasname
        hasMany: false
      },
      docScores : { // copy rize nomarat
        hasMany: false
      },
      docFinance  : { // copy tasviye hesabe mali
        hasMany: false
      },
      docWorkflow   : { // copy govahiye anjam kar
        hasMany: false
      },
      docImage    : { // axe personali
        hasMany: false
      },
      city : {
        hasMany: true
      } ,
      major : {
        hasMany : true
      }
    }
  };
}

export class Requests extends Resource {
  public attributes: {
    name: string,
    last_name: string,
    student_number: string, //شماره دانشجویی
    birth_date: string,
    address: string,
    birth_certificate_id: string, // شماره شناسنامه
    identify_id: string, // شماره ملی
    father_name: string,
    phone: string, // ثابت
    mobile: string,
    office_address: string, // address mahale kar
    office_phone: string,  // telephone mahale kar
    master_name: string,  // name ostad marbote
    defense_date: string,  // tarikhe defae payan name
    Graduation_year: string,  // sale faregh o tahsili
    temporary_certification_date: string,  //tarikhe sodoor govahiye movaghat
    average: string,  //moadel
    service_commitment_canceled_date: string,  //tarikhe taahod khedmat
    service_commitment_canceled_number: string,  //shomare name
    finance_date : string , // tarikhe taviye hesab
    finance_number : string , // shomare name
    service_commitment_number : string , // shomare carte payad khedmat

  };


  get temporary_certification_date(): string {
    return this.attributes.office_address;
  }

  set temporary_certification_date(temporary_certification_date: string) {
    this.attributes.temporary_certification_date = temporary_certification_date;
  }

  get average(): string {
    return this.attributes.average;
  }

  set average(average: string) {
    this.attributes.average = average;
  }

  get finance_date(): string {
    return this.attributes.finance_date;
  }

  set finance_date(finance_date: string) {
    this.attributes.finance_date = finance_date;
  }

  get finance_number(): string {
    return this.attributes.finance_number;
  }

  set finance_number(finance_number: string) {
    this.attributes.finance_number = finance_number;
  }

  get service_commitment_number(): string {
    return this.attributes.service_commitment_number;
  }

  set service_commitment_number(service_commitment_number: string) {
    this.attributes.service_commitment_number = service_commitment_number;
  }
  get service_commitment_canceled_number(): string {
    return this.attributes.service_commitment_canceled_number;
  }

  set service_commitment_canceled_number(service_commitment_canceled_number: string) {
    this.attributes.service_commitment_canceled_number = service_commitment_canceled_number;
  }

  get service_commitment_canceled_date(): string {
    return this.attributes.service_commitment_canceled_date;
  }

  set service_commitment_canceled_date(service_commitment_canceled_date: string) {
    this.attributes.service_commitment_canceled_date = service_commitment_canceled_date;
  }

  get office_address(): string {
    return this.attributes.office_address;
  }

  set office_address(office_address: string) {
    this.attributes.office_address = office_address;
  }

  get office_phone(): string {
    return this.attributes.office_phone;
  }

  set office_phone(office_phone: string) {
    this.attributes.office_phone = office_phone;
  }
  get master_name(): string {
    return this.attributes.master_name;
  }

  set master_name(master_name: string) {
   this.attributes.master_name = master_name;
  }

  get defense_date(): string {
    return this.attributes.defense_date;
  }

  set defense_date(defense_date: string) {
    this.attributes.defense_date = defense_date;
  }

  get Graduation_year(): string {
    return this.attributes.Graduation_year;
  }

  set Graduation_year(Graduation_year: string) {
    this.attributes.Graduation_year = Graduation_year;
  }

  get father_name(): string {
    return this.attributes.father_name;
  }

  set father_name(father_name: string) {
    this.attributes.father_name = father_name;
  }

  get phone(): string {
    return this.attributes.phone;
  }

  set phone(phone: string) {
    this.attributes.phone = phone;
  }


  get mobile(): string {
    return this.attributes.mobile;
  }

  set mobile(mobile: string) {
    this.attributes.mobile = mobile;
  }

  get name(): string {
    return this.attributes.name;
  }

  set name(name: string) {
    this.attributes.name = name;
  }

  get last_name(): string {
    return this.attributes.last_name;
  }

  set last_name(last_name: string) {
    this.attributes.last_name = last_name;
  }

  get student_number(): string {
    return this.attributes.student_number;
  }

  set student_number(student_number: string) {
    this.attributes.student_number = student_number;
  }

// todo test
  get birth_date(): any {
    if (this.attributes.birth_date) {
      const m = moment.from(this.attributes.birth_date, 'fa', 'YYYY/MM/DD');
      return m.format('YYYY-MM-DD');
    }
  }

  set birth_date(momentDate: any) {
    this.attributes.birth_date = momentDate.locale('fa').format('YYYY/MM/DD');

  }

  get address(): string {
    return this.attributes.address;
  }

  set address(address: string) {
    this.attributes.address = address;
  }

  get birth_certificate_id(): string {
    return this.attributes.birth_certificate_id;
  }

  set birth_certificate_id(birth_certificate_id: string) {
    this.attributes.birth_certificate_id = birth_certificate_id;
  }


  get identify_id(): string {
    return this.attributes.identify_id;
  }

  set identify_id(identify_id: string) {
    this.attributes.identify_id = identify_id;
  }


  // docIdentify
  get docIdentify(): Image {
    if (Object.keys(this.relationships.docIdentify.data).length === 0) {
      const image = (new ImagesService()).new();
      this.docIdentify = image;
    }
    return <Image>this.relationships.docIdentify.data;
  }

  set docIdentify(docIdentify: Image) {
    this.relationships.docIdentify.data = docIdentify;
  }

  set docIdentifyId(id: string) {
    let imageService = new ImagesService();
    let image = imageService.new();
    image.id = id;
    this.docIdentify = image;
  }

  // docNational
  get docNational(): Image {
    if (Object.keys(this.relationships.docNational.data).length === 0) {
      const image = (new ImagesService()).new();
      this.docNational = image;
    }
    return <Image>this.relationships.docNational.data;
  }

  set docNational(docNational: Image) {
    this.relationships.docNational.data = docNational;
  }

  set docNational_Id(id: string) {
    let imageService = new ImagesService();
    let image = imageService.new();
    image.id = id;
    this.docNational = image;
  }

  // docscores

  get docScores(): Image {
    if (Object.keys(this.relationships.docScores.data).length === 0) {
      const image = (new ImagesService()).new();
      this.docScores = image;
    }
    return <Image>this.relationships.docScores.data;
  }

  set docScores(docScores: Image) {
    this.relationships.docScores.data = docScores;
  }

  set docScores_Id(id: string) {
    let imageService = new ImagesService();
    let image = imageService.new();
    image.id = id;
    this.docScores = image;
  }
  //  docFinance
  get docFinance(): Image {
    if (Object.keys(this.relationships.docFinance.data).length === 0) {
      const image = (new ImagesService()).new();
      this.docFinance = image;
    }
    return <Image>this.relationships.docFinance.data;
  }

  set docFinance(docFinance: Image) {
    this.relationships.docFinance.data = docFinance;
  }

  set docFinance_Id(id: string) {
    let imageService = new ImagesService();
    let image = imageService.new();
    image.id = id;
    this.docFinance = image;
  }
  // docWorkflow
  get docWorkflow(): Image {
    if (Object.keys(this.relationships.docWorkflow.data).length === 0) {
      const image = (new ImagesService()).new();
      this.docWorkflow = image;
    }
    return <Image>this.relationships.docWorkflow.data;
  }

  set docWorkflow(docWorkflow: Image) {
    this.relationships.docWorkflow.data = docWorkflow;
  }

  set docWorkflow_Id(id: string) {
    let imageService = new ImagesService();
    let image = imageService.new();
    image.id = id;
    this.docWorkflow = image;
  }
  // docImage
  get docImage(): Image {
    if (Object.keys(this.relationships.docImage.data).length === 0) {
      const image = (new ImagesService()).new();
      this.docImage = image;
    }
    return <Image>this.relationships.docImage.data;
  }

  set docImage(docImage: Image) {
    this.relationships.docImage.data = docImage;
  }

  set docImage_Id(id: string) {
    let imageService = new ImagesService();
    let image = imageService.new();
    image.id = id;
    this.docImage = image;
  }

  // relation city
  get city(): ICollection<Cities> {
    return <ICollection<Cities>>this.relationships.city.data;
  }

  set city(city: ICollection<Cities>) {
    this.relationships.city.data = city;
  }

  get cityIds(): string[] {
    let ids: string[] = [];
    Base.forEach(this.city, (city: Cities) => {
      ids.push(city.id);
    });
    return ids;
  }

  set cityIds(cityIds: string[]) {
    if (cityIds == null) {
      cityIds = [];
    }
    const selected_cities: ICollection<Cities> = Base.newCollection<Cities>();
    for (const key in cityIds) {
      if (key in cityIds) {
        const city_id = cityIds[key];
        const city = (new CitiesService()).new();
        city.id = city_id;
        selected_cities[city_id] = city;
      }
    }
    this.city = selected_cities;
  }
  // relation major
  get major(): ICollection<Majore> {
    return <ICollection<Majore>>this.relationships.majore.data;
  }

  set major(city: ICollection<Majore>) {
    this.relationships.major.data = city;
  }

  get majorIds(): string[] {
    let ids: string[] = [];
    Base.forEach(this.city, (city: Majore) => {
      ids.push(city.id);
    });
    return ids;
  }

  set majorIds(cityIds: string[]) {
    if (cityIds == null) {
      cityIds = [];
    }
    const selected_majore: ICollection<Majore> = Base.newCollection<Majore>();
    for (const key in cityIds) {
      if (key in cityIds) {
        const majore_id = cityIds[key];
        const majore = (new MajoreService()).new();
        majore.id = majore_id;
        selected_majore[majore_id] = majore;
      }
    }
    this.major = selected_majore;
  }
}
