import { IsBoolean, IsEAN, IsInt, IsNotEmpty, IsObject, IsPositive, IsString } from "class-validator";
import { RunnerCard } from '../entities/RunnerCard';
import { ResponseObjectType } from '../enums/ResponseObjectType';
import { IResponse } from './IResponse';
import { ResponseRunner } from './ResponseRunner';

/**
 * Defines the runner card response.
*/
export class ResponseRunnerCard implements IResponse {
    /**
    * The responseType.
    * This contains the type of class/entity this response contains.
    */
    responseType: ResponseObjectType = ResponseObjectType.RUNNERCARD;

    /**
     * The card's id.
     */
    @IsInt()
    id: number;;

    /**
     * The card's associated runner.
     * This is important to link scans to runners.
     */
    @IsObject()
    runner: ResponseRunner | null;

    /**
     * The card's code.
     */
    @IsEAN()
    @IsString()
    @IsNotEmpty()
    code: string;

    /**
     * Is the enabled valid (for fraud reasons).
     * The determination of validity will work differently for every child class.
     */
    @IsBoolean()
    enabled: boolean = true;

    @IsInt()
    @IsPositive()
    created_at: number;

    @IsInt()
    @IsPositive()
    updated_at: number;

    /**
     * Creates a ResponseRunnerCard object from a runner card.
     * @param card The card the response shall be build for.
     */
    public constructor(card: RunnerCard) {
        this.id = card.id;
        if (!card.runner) { this.runner = null }
        else { this.runner = card.runner.toResponse(); }
        try {
            this.code = card.code;
        } catch (error) {
            this.code = "0000000000000"
        }

        this.enabled = card.enabled;
        this.created_at = card.created_at;
        this.updated_at = card.updated_at;
    }
}
