import {Model} from './Model';

export interface IPage {
    id?: number;
    UUID: string;
    Name: string;
    OrgUserId: string;
    IsPublic: boolean;
    IsVisibleInMenu: boolean;
    ParentId: string;
    NodeIds: string[];
    CreatedAt: string;
    UpdatedAt: string;
    Active: boolean;
    CreatedUserId: string;
    UpdateUserId: string;
    ChildPageIds: string[];
    noDelete: boolean;
    virtual: boolean;
    needsServerUpdate?: 0 | 1;
    deleted?: boolean;
    offlineUpdatedAt?: string;
}

export class Page extends Model<IPage> implements IPage {
    id?: number;
    UUID: string;
    Name: string;
    OrgUserId: string;
    IsPublic: boolean;
    IsVisibleInMenu: boolean;
    ParentId: string;
    NodeIds: string[];
    CreatedAt: string;
    UpdatedAt: string;
    Active: boolean;
    CreatedUserId: string;
    UpdateUserId: string;
    ChildPageIds: string[];
    noDelete: boolean;
    virtual: boolean;
    needsServerUpdate?: 0 | 1;
    deleted?: boolean;
    offlineUpdatedAt?: string;

    public static getTableName() {
        return 'pages';
    }

    public static getSchema() {
        // tslint:disable-next-line:max-line-length
        return '++id, UUID, Name, OrgUserId, IsPublic, IsVisibleInMenu, ParentId, NodeIds, CreatedAt, UpdatedAt, Active, CreatedUserId, UpdateUserId, ChildPageIds, noDelete, virtual, needsServerUpdate, deleted, offlineUpdatedAt';
    }

    constructor(
        UUID: string,
        Name: string,
        OrgUserId: string,
        IsPublic: boolean,
        IsVisibleInMenu: boolean,
        ParentId: string,
        NodeIds: string[],
        CreatedAt: string,
        UpdatedAt: string,
        Active: boolean,
        CreatedUserId: string,
        UpdateUserId: string,
        ChildPageIds: string[],
        noDelete: boolean,
        virtual: boolean,
        needsServerUpdate?: 0 | 1,
        deleted?: boolean,
        offlineUpdatedAt?: string,
        id?: number
    ) {
        super();

        this.UUID = UUID;
        this.Name = Name;
        this.OrgUserId = OrgUserId;
        this.IsPublic = IsPublic;
        this.IsVisibleInMenu = IsVisibleInMenu;
        this.ParentId = ParentId;
        this.NodeIds = NodeIds;
        this.CreatedAt = CreatedAt;
        this.UpdatedAt = UpdatedAt;
        this.Active = Active;
        this.CreatedUserId = CreatedUserId;
        this.UpdateUserId = UpdateUserId;
        this.ChildPageIds = ChildPageIds;
        this.noDelete = noDelete;
        this.virtual = virtual;

        if ( typeof needsServerUpdate !== 'undefined' ) {
            this.needsServerUpdate = needsServerUpdate;
        }

        if ( typeof deleted !== 'undefined' ) {
            this.deleted = deleted;
        }

        if ( typeof offlineUpdatedAt !== 'undefined' ) {
            this.offlineUpdatedAt = offlineUpdatedAt;
        }

        if ( id ) {
            this.id = id;
        }
    }

    public fillFromRecord(record: any) {
        this.UUID = record.UUID;
        this.Name = record.Name;
        this.OrgUserId = record.OrgUserId;
        this.IsPublic = record.IsPublic;
        this.IsVisibleInMenu = record.IsVisibleInMenu;
        this.ParentId = record.ParentId;
        this.NodeIds = record.NodeIds;
        this.CreatedAt = String(record.CreatedAt);
        this.UpdatedAt = String(record.UpdatedAt);
        this.Active = record.Active;
        this.CreatedUserId = record.CreatedUserId;
        this.UpdateUserId = record.UpdateUserId;
        this.ChildPageIds = record.ChildPageIds;
        this.noDelete = record.noDelete;
        this.virtual = record.virtual;
    }

    public getSaveRecord(): any {
        return {
            ...(this.id ? { id: this.id } : {}),
            UUID: this.UUID,
            Name: this.Name,
            OrgUserId: this.OrgUserId,
            IsPublic: this.IsPublic,
            IsVisibleInMenu: this.IsVisibleInMenu,
            ParentId: this.ParentId,
            NodeIds: this.NodeIds,
            CreatedAt: new Date(this.CreatedAt),
            UpdatedAt: new Date(this.UpdatedAt),
            Active: this.Active,
            CreatedUserId: this.CreatedUserId,
            UpdateUserId: this.UpdateUserId,
            ChildPageIds: this.ChildPageIds,
            noDelete: this.noDelete,
            virtual: this.virtual,
            ...(typeof this.needsServerUpdate === 'undefined' ? {} : { needsServerUpdate: this.needsServerUpdate }),
            ...(typeof this.deleted === 'undefined' ? {} : { deleted: this.deleted }),
            ...(typeof this.offlineUpdatedAt === 'undefined' ? {} : { offlineUpdatedAt: this.offlineUpdatedAt }),
        };
    }

    public getDatabase(): Dexie.Table<IPage, number> {
        return this.staticClass().dbService.table('pages') as Dexie.Table<IPage, number>;
    }
}
