import {
    IsArray,

    IsBase64,

    IsBoolean,

    IsObject,
    IsOptional,
    IsString
} from "class-validator";
import { Address } from '../entities/Address';
import { RunnerOrganization } from '../entities/RunnerOrganization';
import { ResponseObjectType } from '../enums/ResponseObjectType';
import { IResponse } from './IResponse';
import { ResponseRunnerGroup } from './ResponseRunnerGroup';
import type { ResponseRunnerTeam } from './ResponseRunnerTeam';

/**
 * Defines the runnerOrganization response.
*/
export class ResponseRunnerOrganization extends ResponseRunnerGroup implements IResponse {
    /**
    * The responseType.
    * This contains the type of class/entity this response contains.
    */
    responseType: ResponseObjectType = ResponseObjectType.RUNNERORGANIZATION;

    /**
     * The runnerOrganization's address.
     */
    @IsObject()
    @IsOptional()
    address?: Address;

    /**
     * The runnerOrganization associated teams.
     */
    @IsArray()
    teams?: ResponseRunnerTeam[];

    /**
     * The organization's registration key.
     * If registration is disabled this is null.
     */
    @IsString()
    @IsOptional()
    @IsBase64()
    registrationKey?: string;

    /**
     * Is registration enabled for the organization?
     */
    @IsOptional()
    @IsBoolean()
    registrationEnabled?: boolean = true;

    /**
     * Creates a ResponseRunnerOrganization object from a runnerOrganization.
     * @param org The runnerOrganization the response shall be build for.
     */
    public constructor(org: RunnerOrganization) {
        super(org);
        this.address = org.address;
        const ResponseRunnerTeam = require('./ResponseRunnerTeam').ResponseRunnerTeam;
        this.teams = new Array<ResponseRunnerTeam>();
        if (org.teams) {
            for (let team of org.teams) {
                this.teams.push(team.toResponse());
            }
            for (const team of this.teams) {
                this.total_distance += team.total_distance;
            }
        }

        if (!org.key) { this.registrationEnabled = false; }
        else { this.registrationKey = Buffer.from(org.key).toString('base64'); }
    }
}
