import { Request } from "express";
import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam, Req, UseBefore } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm';
import { RunnerNotFoundError } from '../errors/RunnerErrors';
import { ScanIdsNotMatchingError, ScanNotFoundError } from '../errors/ScanErrors';
import { ScanStationNotFoundError } from '../errors/ScanStationErrors';
import ScanAuth from '../middlewares/ScanAuth';
import { CreateScan } from '../models/actions/create/CreateScan';
import { CreateTrackScan } from '../models/actions/create/CreateTrackScan';
import { UpdateScan } from '../models/actions/update/UpdateScan';
import { UpdateTrackScan } from '../models/actions/update/UpdateTrackScan';
import { Scan } from '../models/entities/Scan';
import { TrackScan } from '../models/entities/TrackScan';
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
import { ResponseScan } from '../models/responses/ResponseScan';
import { ResponseTrackScan } from '../models/responses/ResponseTrackScan';
@JsonController('/scans')
@OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] })
export class ScanController {
	private scanRepository: Repository<Scan>;
	private trackScanRepository: Repository<TrackScan>;

	/**
	 * Gets the repository of this controller's model/entity.
	 */
	constructor() {
		this.scanRepository = getConnectionManager().get().getRepository(Scan);
		this.trackScanRepository = getConnectionManager().get().getRepository(TrackScan);
	}

	@Get()
	@Authorized("SCAN:GET")
	@ResponseSchema(ResponseScan, { isArray: true })
	@ResponseSchema(ResponseTrackScan, { isArray: true })
	@OpenAPI({ description: 'Lists all scans (normal or track) from all runners. <br> This includes the scan\'s runner\'s distance ran.' })
	async getAll() {
		let responseScans: ResponseScan[] = new Array<ResponseScan>();
		const scans = await this.scanRepository.find({ relations: ['runner', 'track', 'runner.scans', 'runner.group', 'runner.scans.track', 'card', 'station'] });
		scans.forEach(scan => {
			responseScans.push(scan.toResponse());
		});
		return responseScans;
	}

	@Get('/:id')
	@Authorized("SCAN:GET")
	@ResponseSchema(ResponseScan)
	@ResponseSchema(ResponseTrackScan)
	@ResponseSchema(ScanNotFoundError, { statusCode: 404 })
	@OnUndefined(ScanNotFoundError)
	@OpenAPI({ description: 'Lists all information about the scan whose id got provided. This includes the scan\'s runner\'s distance ran.' })
	async getOne(@Param('id') id: number) {
		let scan = await this.scanRepository.findOne({ id: id }, { relations: ['runner', 'track', 'runner.scans', 'runner.group', 'runner.scans.track', 'card', 'station'] })
		if (!scan) { throw new ScanNotFoundError(); }
		return scan.toResponse();
	}

	@Post()
	@UseBefore(ScanAuth)
	@ResponseSchema(ResponseScan)
	@ResponseSchema(RunnerNotFoundError, { statusCode: 404 })
	@OpenAPI({ description: 'Create a new scan (not track scan - use /scans/trackscans instead). <br> Please rmemember to provide the scan\'s runner\'s id and distance.', security: [{ "StationApiToken": [] }, { "AuthToken": [] }, { "RefreshTokenCookie": [] }] })
	async post(@Body({ validate: true }) createScan: CreateScan) {
		let scan = await createScan.toEntity();
		scan = await this.scanRepository.save(scan);
		return (await this.scanRepository.findOne({ id: scan.id }, { relations: ['runner', 'track', 'runner.scans', 'runner.group', 'runner.scans.track', 'card', 'station'] })).toResponse();
	}

	@Post("/trackscans")
	@UseBefore(ScanAuth)
	@ResponseSchema(ResponseTrackScan)
	@ResponseSchema(RunnerNotFoundError, { statusCode: 404 })
	@OpenAPI({ description: 'Create a new track scan (for "normal" scans use /scans instead). <br> Please remember that to provide the scan\'s card\'s station\'s id.', security: [{ "StationApiToken": [] }, { "AuthToken": [] }, { "RefreshTokenCookie": [] }] })
	async postTrackScans(@Body({ validate: true }) createScan: CreateTrackScan, @Req() req: Request) {
		const station_id = req.headers["station_id"];
		if (station_id) {
			createScan.station = parseInt(station_id.toString());
		}
		let scan = await createScan.toEntity();
		scan = await this.trackScanRepository.save(scan);
		return (await this.scanRepository.findOne({ id: scan.id }, { relations: ['runner', 'track', 'runner.scans', 'runner.group', 'runner.scans.track', 'card', 'station'] })).toResponse();
	}

	@Put('/:id')
	@Authorized("SCAN:UPDATE")
	@ResponseSchema(ResponseScan)
	@ResponseSchema(ScanNotFoundError, { statusCode: 404 })
	@ResponseSchema(RunnerNotFoundError, { statusCode: 404 })
	@ResponseSchema(ScanIdsNotMatchingError, { statusCode: 406 })
	@OpenAPI({ description: "Update the scan (not track scan use /scans/trackscans/:id instead) whose id you provided. <br> Please remember that ids can't be changed and distances must be positive." })
	async put(@Param('id') id: number, @Body({ validate: true }) scan: UpdateScan) {
		let oldScan = await this.scanRepository.findOne({ id: id });

		if (!oldScan) {
			throw new ScanNotFoundError();
		}

		if (oldScan.id != scan.id) {
			throw new ScanIdsNotMatchingError();
		}

		await this.scanRepository.save(await scan.update(oldScan));
		return (await this.scanRepository.findOne({ id: id }, { relations: ['runner', 'track', 'runner.scans', 'runner.group', 'runner.scans.track', 'card', 'station'] })).toResponse();
	}

	@Put('/trackscans/:id')
	@Authorized("SCAN:UPDATE")
	@ResponseSchema(ResponseTrackScan)
	@ResponseSchema(ScanNotFoundError, { statusCode: 404 })
	@ResponseSchema(RunnerNotFoundError, { statusCode: 404 })
	@ResponseSchema(ScanStationNotFoundError, { statusCode: 404 })
	@ResponseSchema(ScanIdsNotMatchingError, { statusCode: 406 })
	@OpenAPI({ description: 'Update the track scan (not "normal" scan use /scans/trackscans/:id instead) whose id you provided. <br> Please remember that only the validity, runner and track can be changed.' })
	async putTrackScan(@Param('id') id: number, @Body({ validate: true }) scan: UpdateTrackScan) {
		let oldScan = await this.trackScanRepository.findOne({ id: id });

		if (!oldScan) {
			throw new ScanNotFoundError();
		}

		if (oldScan.id != scan.id) {
			throw new ScanIdsNotMatchingError();
		}

		await this.trackScanRepository.save(await scan.update(oldScan));
		return (await this.scanRepository.findOne({ id: id }, { relations: ['runner', 'track', 'runner.scans', 'runner.group', 'runner.scans.track', 'card', 'station'] })).toResponse();
	}

	@Delete('/:id')
	@Authorized("SCAN:DELETE")
	@ResponseSchema(ResponseScan)
	@ResponseSchema(ResponseEmpty, { statusCode: 204 })
	@OnUndefined(204)
	@OpenAPI({ description: 'Delete the scan whose id you provided. <br> If no scan with this id exists it will just return 204(no content).' })
	async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
		let scan = await this.scanRepository.findOne({ id: id });
		if (!scan) { return null; }
		const responseScan = await this.scanRepository.findOne({ id: scan.id }, { relations: ['runner', 'track', 'runner.scans', 'runner.group', 'runner.scans.track', 'card', 'station'] });

		await this.scanRepository.delete(scan);
		return responseScan.toResponse();
	}
}
