import {Md5} from 'ts-md5';
import { Layout } from '../widgets/Layout';

export class Page {
    id: string = "";
    name: string = "";
    slug: string = "";
    path: string = "";
    lang?: string;
    communityId: number = 0;
    showOrder: number = 0;
    visible: boolean = true;
    configuration?: Configuration;
    defaultPage: boolean = false;
    dynamic: boolean = false;
    permissionId?: string;
    showInMenu: boolean= true;

    public constructor(init?: Partial<Page>) {
        Object.assign(this, init);
    }

    getConfiguration() {
        if (Object.prototype.toString.call(this.configuration) === "[object String]")
            return Configuration.fromJson(this.configuration);
        return this.configuration;
    }

    getHash(): string {
        return Md5.hashStr(JSON.stringify(this));
    }
}

export class Configuration {
    layout?: Layout;
    selectedCommunity?: string;
    public constructor(init?: Partial<Configuration>) {
        Object.assign(this, init);
    }
    static empty(): Configuration {
        return new Configuration({ layout: Layout.emptyLayout() });
    }
    static fromJson(json: any) {
        if (!json)
            return null;

        if (typeof json === 'string')
            return Object.assign(new Configuration(), JSON.parse(json));

        return json;
    }
}