import {Model} from './Model';

export interface IPageNode {
    id?: number;
    UUID: string;
    Type: string;
    ValueJSON: string;
    PageId: string;
    CreatedAt: string;
    UpdatedAt: string;
    CreatedUserId: string;
    UpdateUserId: string;
    needsServerUpdate?: boolean;
    deleted?: boolean;
    offlineUpdatedAt?: string;
}

export class PageNode extends Model<IPageNode> implements IPageNode {
    id?: number;
    UUID: string;
    Type: string;
    ValueJSON: string;
    PageId: string;
    CreatedAt: string;
    UpdatedAt: string;
    CreatedUserId: string;
    UpdateUserId: string;
    needsServerUpdate?: boolean;
    deleted?: boolean;
    offlineUpdatedAt?: string;

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

    public static getSchema() {
        // tslint:disable-next-line:max-line-length
        return '++id, UUID, Type, ValueJSON, PageId, CreatedAt, UpdatedAt, CreatedUserId, UpdateUserId, needsServerUpdate, deleted, offlineUpdatedAt';
    }

    constructor(
        UUID: string,
        Type: string,
        ValueJSON: string,
        PageId: string,
        CreatedAt: string,
        UpdatedAt: string,
        CreatedUserId: string,
        UpdateUserId: string,
        needsServerUpdate?: boolean,
        deleted?: boolean,
        offlineUpdatedAt?: string,
        id?: number
    ) {
        super();

        this.UUID = UUID;
        this.Type = Type;
        this.ValueJSON = ValueJSON;
        this.PageId = PageId;
        this.CreatedAt = CreatedAt;
        this.UpdatedAt = UpdatedAt;
        this.CreatedUserId = CreatedUserId;
        this.UpdateUserId = UpdateUserId;

        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.Type = record.Type;
        this.ValueJSON = JSON.stringify(record.Value);
        this.PageId = record.PageId;
        this.CreatedAt = String(record.CreatedAt);
        this.UpdatedAt = String(record.UpdatedAt);
        this.CreatedUserId = record.CreatedUserId;
        this.UpdateUserId = record.UpdateUserId;
    }

    public inflateToRecord() {
        const record = this.getSaveRecord();
        record.Value = JSON.parse(record.ValueJSON);
        delete record.ValueJSON;
        return record;
    }

    public getSaveRecord(): any {
        return {
            ...(this.id ? { id: this.id } : {}),
            UUID: this.UUID,
            Type: this.Type,
            ValueJSON: this.ValueJSON,
            PageId: this.PageId,
            CreatedAt: new Date(this.CreatedAt),
            UpdatedAt: new Date(this.UpdatedAt),
            CreatedUserId: this.CreatedUserId,
            UpdateUserId: this.UpdateUserId,
            ...(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<IPageNode, number> {
        return this.staticClass().dbService.table('pageNodes') as Dexie.Table<IPageNode, number>;
    }
}
