import {Injectable} from '@angular/core';
import {ICollection, ISchema, Resource, Service} from 'ngx-jsonapi';
import {Type, TypesService} from '../../basic/types/types.service';
import {Role, RolesService} from '../roles/roles.service';
import {Base} from 'ngx-jsonapi/services/base';
import {Doc, DocService} from '../../../classes/doc.service';
import {Center, CentersService} from '../../../classes/center.service';
import {Message} from '../../../classes/message.service';


@Injectable()
export class UsersService extends Service<User> {


  public resource = User;
  public type = 'users';
  public schema: ISchema = {
    relationships: {
      type: {
        hasMany: false
      },
      role: {
        hasMany: false
      },
      messages: {
        hasMany: true
      },
      centers: {
        hasMany: true
      },
      docshenasname: {
        hasMany: false
      },
      doccardmelli: {
        hasMany: false
      },
      docdegree: {
        hasMany: false
      },
      docimage: {
        hasMany: false
      },
      docmsn: {
        hasMany: false
      },

    }
  };
}

export class User extends Resource {
  public static NURSE_TYPE_ID = '2';
  public attributes: {
    id: string,
    name: string,
    mobile: string,
    password: string,
    email: string,
    cloud_token: string,
    gender: string,
    identifyNumber: string,
    birth_day: string,
    father_name: string,
    resume: string,
    address: string,
    postal_code: string,
    expertise: string,
    msns: string,
    lat: string,
    lng: string,
    available: boolean,
  };

// وفتی میخوای از اینا داخل برنامه استفاده کنی بدون پرانتز استفاده کن
  get available(): boolean {
    return this.attributes.available;
  }

  set available(available: boolean) {
    this.attributes.available = available;
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  get cloud_token() {
    return this.attributes.cloud_token;
  }

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

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

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

  // in baray relationship type hast ke yek be yek hast
  // todo
  get type2(): Type {
    if (Object.keys(this.relationships.type.data).length === 0) {
      const type = (new TypesService()).new();
      this.type2 = type;
    }
    return <Type>this.relationships.type.data;
  }

  set type2(type: Type) {
    this.relationships.type.data = type;
  }

  set typeId(id: string) {
    let typeService = new TypesService();
    let type = typeService.new();
    type.id = id;
    this.type2 = type;
  }

  // in baray relationship role hast ke yek be yek hast

  get role(): Role {
    if (Object.keys(this.relationships.role.data).length === 0) {
      const role = (new RolesService()).new();
      this.role = role;
    }
    return <Role>this.relationships.role.data;
  }

  set role(role: Role) {
    this.relationships.role.data = role;
  }

  set roleId(id: string) {
    let roleService = new RolesService();
    let role = roleService.new();
    role.id = id;
    this.role = role;
  }

  // in baray relationship centers hast ke chand be chand hast
  //  تمام سنتر هارو بر میگردونه
  get centers(): ICollection<Center> {
    return <ICollection<Center>>this.relationships.centers.data;
  }

  //   برای ست کردن یه سنتر استفاده میشه
  set centers(center: ICollection<Center>) {
    this.relationships.centers.data = center;
  }

// این  برای وقتی که id های سنتر هارو میخوای بگیری این جا روی data های ریلیشن سنتر foreach میرنه
  get centersIds(): string[] {

    let ids: string[] = [];
    Base.forEach(this.centers, (center: Center) => {
      ids.push(center.id);
    });
    return ids;
  }

  //  زمانی  استفاده میشه که مثلا بخوای یه سنتر create کنی
  set centersIds(centersIds: string[]) {
    if (centersIds == null) {
      centersIds = [];
    }
    const selected_centers: ICollection<Center> = Base.newCollection<Center>();
    for (const key in centersIds) {
      if (key in centersIds) {
        const center_id = centersIds[key];
        const center = (new CentersService()).new();
        center.id = center_id;
        selected_centers[center_id] = center;
      }
    }
    this.centers = selected_centers;
  }

// in baray relationship messages hast ke chand be chand hast
  // in tamam datae relationship messages ro bar migardone
  get messages(): ICollection<Message> {
    return <ICollection<Message>>this.relationships.messages.data;
  }

  set messages(messages: ICollection<Message>) {
    this.relationships.Message.data = messages;
  }

// in tamam  center haro migire va foreach mizane idesh ham bar migardone
  get messagesIds(): string[] {
    let ids: string[] = [];
    Base.forEach(this.messages, (messages: Message) => {
      ids.push(messages.id);
    });
    return ids;
  }

  // in zamani estefade mishe ke bekhay message create koni
  set messagesIds(messagesIds: string[]) {
    const selected_messages: ICollection<Message> = Base.newCollection<Message>();
    for (const key in messagesIds) {
      if (key in messagesIds) {
        const message_id = messagesIds[key];
        const message = (new CentersService()).new();
        message.id = message_id;
        selected_messages[message_id] = message;
      }
    }
    this.messages = selected_messages;
  }

  // // in baray docshenasname hast ke yek be yek hast
  get docshenasname(): Doc {
    if (Object.keys(this.relationships.docshenasname.data).length === 0) {
      const doc = (new DocService()).new();
      this.docshenasname = doc;
    }
    return <Doc>this.relationships.docshenasname.data;
  }

  set docshenasname(docshenasname: Doc) {
    this.relationships.docshenasname.data = docshenasname;
  }

  set docshenasnameId(id: string) {
    let docService = new DocService();
    let doc = docService.new();
    doc.id = id;
    this.docshenasname = doc;
  }

  // in baray docshenasname hast ke yek be yek hast
  get doccardmelli(): Doc {
    if (Object.keys(this.relationships.doccardmelli.data).length === 0) {
      const doc = (new DocService()).new();
      this.doccardmelli = doc;
    }
    return <Doc>this.relationships.doccardmelli.data;
  }

  set doccardmelli(doccardmelli: Doc) {
    this.relationships.doccardmelli.data = doccardmelli;
  }

  set doccardmelliId(id: string) {
    let docService = new DocService();
    let doc = docService.new();
    doc.id = id;
    this.doccardmelli = doc;
  }

  // in baray degree hast  ke yek be yek hast
  get docdegree(): Doc {
    if (Object.keys(this.relationships.docdegree.data).length === 0) {
      const doc = (new DocService()).new();
      this.docdegree = doc;
    }
    return <Doc>this.relationships.docdegree.data;
  }

  set docdegree(docdegree: Doc) {
    this.relationships.docdegree.data = docdegree;
  }

  set docdegreeId(id: string) {
    let docService = new DocService();
    let doc = docService.new();
    doc.id = id;
    this.docdegree = doc;
  }

  // in baray relation image hast ke yek be yek hast
  get docimage(): Doc {
    if (Object.keys(this.relationships.docimage.data).length === 0) {
      const doc = (new DocService()).new();
      this.docimage = doc;
    }
    return <Doc>this.relationships.docimage.data;
  }

  set docimage(docimage: Doc) {
    this.relationships.docimage.data = docimage;
  }

  set docimageId(id: string) {
    let docService = new DocService();
    let doc = docService.new();
    doc.id = id;
    this.docimage = doc;
  }

  // in baray relation docmsn hast ke yek be yek hast , msn hamon parvane nezam pezeshki hast
  get docmsn(): Doc {
    if (Object.keys(this.relationships.docmsn.data).length === 0) {
      const doc = (new DocService()).new();
      this.docmsn = doc;
    }
    return <Doc>this.relationships.docmsn.data;
  }

  set docmsn(docmsn: Doc) {
    this.relationships.docmsn.data = docmsn;
  }

  set docmsnId(id: string) {
    let docService = new DocService();
    let doc = docService.new();
    doc.id = id;
    this.docmsn = doc;
  }

}
