import NetInterface, { INetInterface } from './NetInterface';

// This file was autogenerated. Please do not change.
// All changes will be overwrited on commit.
export interface IAddressesInfo {
    dns_port: number;
    interfaces: { [key: string]: INetInterface };
    web_port: number;
}

export default class AddressesInfo {
    readonly _dns_port: number;

    /**
     * Description: undefined
     * Example: 53
     */
    get dnsPort(): number {
        return this._dns_port;
    }

    static dnsPortValidate(dnsPort: number): boolean {
        return typeof dnsPort === 'number';
    }

    readonly _interfaces: { [key: string]: NetInterface };

    /** */
    get interfaces(): { [key: string]: NetInterface } {
        return this._interfaces;
    }

    readonly _web_port: number;

    /**
     * Description: undefined
     * Example: 80
     */
    get webPort(): number {
        return this._web_port;
    }

    static webPortValidate(webPort: number): boolean {
        return typeof webPort === 'number';
    }

    constructor(props: IAddressesInfo) {
        this._dns_port = props.dns_port;
        this._interfaces = Object.keys(props.interfaces).reduce((prev, key) => {
                                    return { ...prev, [key]: new NetInterface(props.interfaces[key])};
                                },{})
        this._web_port = props.web_port;
    }

    serialize(): IAddressesInfo {
        const data: IAddressesInfo = {
            dns_port: this._dns_port,
            interfaces: Object.keys(this._interfaces).reduce<Record<string, any>>((prev, key) => ({ ...prev, [key]: this._interfaces[key].serialize() }), {}),
            web_port: this._web_port,
        };
        return data;
    }

    validate(): string[] {
        const validate = {
            dns_port: typeof this._dns_port === 'number',
            web_port: typeof this._web_port === 'number',
        };
        const isError: string[] = [];
        Object.keys(validate).forEach((key) => {
            if (!(validate as any)[key]) {
                isError.push(key);
            }
        });
        return isError;
    }

    update(props: Partial<IAddressesInfo>): AddressesInfo {
        return new AddressesInfo({ ...this.serialize(), ...props });
    }
}
