import {PeopleModel} from './people.model';
import {BrokerModel} from './broker.model';
import {ContactModel} from './contact.model';

export enum UserRoles {
  Unknown = 'Unknown',
  People = 'People',
  User = 'User',
  Broker = 'Broker',
  Beneficiary = 'Beneficiary',
  Admin = 'admin',
  Accountant = 'accountant',
  Manager = 'manager',
  Super = 'super',
}

export class UserExModel extends PeopleModel{
  Role: UserRoles;
  Avatar: string;  // for save new people's avatar only
  Broker?: BrokerModel;
  Login: string;
  Contacts?: ContactModel[];
  defaultContact: ContactModel;
  constructor(payload?: Partial<UserExModel>) {
    super(payload);
    if ( ! payload ) { payload = {}; }
    this.Role = payload.Role || 'People' as UserRoles;
    this.Avatar = payload.Avatar || '';
    this.Login = payload.Login || '';
    this.Broker = new BrokerModel(payload.Broker);
    this.Contacts = [];
    this.defaultContact = new ContactModel({PeopleId: this.Id, ContactType: 'default'});
    if (Array.isArray(payload.Contacts)) {
      payload.Contacts.forEach( v => {

        if ( v.ContactType.toLowerCase() === 'default' ){
          this.defaultContact = new ContactModel(v);
          this.Contacts.unshift(new ContactModel(v)); // add to head, make sure it's number 1
        }else{
          this.Contacts.push(new ContactModel(v));  // add to tail
        }
      });
    }
    if ( this.Contacts.length === 0 ){
      this.Contacts.push(this.defaultContact);
    }
  }
}
