import { Router } from 'express';
import { Main } from '../Main';
import { join } from 'path';
import { pathExistsSync, readdirSync, readFile, writeFile } from 'fs-extra';

export class CalibrationRouter {
	private _Main: Main;
	Router: Router;

	calibrationImagePath: string;
	hasCalibrationImage: boolean = false;

	constructor(Main: Main) {
		this._Main = Main;
		this.Router = Router();

		this.calibrationImagePath = join(
			this._Main.dataPath,
			'calibrationImage.png'
		);

		this.registerRoutes();
	}

	async registerRoutes() {
		this.Router.get('/calibrationImage', (req, res) => {
			if (!this.hasCalibrationImage)
				return res.redirect('/img/noCalibrationImage.png');

			res.sendFile(this.calibrationImagePath);
		});
	}

	async writeCalibrationImage(base64: string) {
		await writeFile(
			this.calibrationImagePath,
			Buffer.from(base64, 'base64')
		);
		this.hasCalibrationImage = true;
	}
}
