import { IsBoolean, IsInt, IsNotEmpty, IsPositive } from "class-validator";
import { Scan } from '../entities/Scan';
import { TrackScan } from '../entities/TrackScan';
import { ResponseObjectType } from '../enums/ResponseObjectType';
import { IResponse } from './IResponse';

/**
 * Defines the scan selfservice response.
*/
export class ResponseSelfServiceScan implements IResponse {
    /**
    * The responseType.
    * This contains the type of class/entity this response contains.
    */
    responseType: ResponseObjectType = ResponseObjectType.SELFSERVICESCAN;

    /**
     * The scans's id (for sorting).
     */
    @IsInt()
    id: number;

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

    /**
     * The scans's length/distance in meters.
     */
    @IsInt()
    @IsPositive()
    distance: number;

    /**
     * The scans's lap time (0 if non is availdable).
     */
    @IsInt()
    @IsNotEmpty()
    lapTime: number = 0;

    /**
     * Creates a ResponseScan object from a scan.
     * @param scan The scan the response shall be build for.
     */
    public constructor(scan: Scan | TrackScan) {
        this.id = scan.id;
        this.distance = scan.distance;
        this.valid = scan.valid;
        if (scan instanceof TrackScan) {
            this.lapTime = scan.lapTime;
            this.responseType = ResponseObjectType.SELFSERVICETRACKSCAN;
        }
    }
}
