import {Model} from './Model';

export interface ICodium {
    id?: number;
    Language: string;
    NodeId: string;
    PageId: string;
    code: string;
    UUID: string;
    needsServerUpdate?: boolean;
    deleted?: boolean;
    offlineUpdatedAt?: string;
}

export class Codium extends Model<ICodium> implements ICodium {
    id?: number;
    Language: string;
    NodeId: string;
    PageId: string;
    code: string;
    UUID: string;
    needsServerUpdate?: boolean;
    deleted?: boolean;
    offlineUpdatedAt?: string;

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

    public static getSchema() {
        return '++id, Language, NodeId, PageId, code, UUID, needsServerUpdate, deleted, offlineUpdatedAt';
    }

    constructor(
        Language: string,
        NodeId: string,
        PageId: string,
        code: string,
        UUID: string,
        needsServerUpdate?: boolean,
        deleted?: boolean,
        offlineUpdatedAt?: string,
        id?: number
    ) {
        super();

        this.Language = Language;
        this.NodeId = NodeId;
        this.PageId = PageId;
        this.code = code;
        this.UUID = UUID;

        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.Language = record.Language;
        this.NodeId = record.NodeId;
        this.PageId = record.PageId;
        this.code = record.code;
        this.UUID = record.UUID;
    }

    public getSaveRecord(): any {
        return {
            ...(this.id ? { id: this.id } : {}),
            Language: this.Language,
            NodeId: this.NodeId,
            PageId: this.PageId,
            code: this.code,
            UUID: this.UUID,
            ...(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<ICodium, number> {
        return this.staticClass().dbService.table('codiums') as Dexie.Table<ICodium, number>;
    }
}
