import { ApiValidationError } from "../../Models/Server.Error.model";
import { Body, Controller, Post, Response, Route, SuccessResponse } from "tsoa";

import {
    AssingAvailableServerParams,
    BotCheckSession,
    BotCheckStatus,
    BotEventParams,
    BotView,
    IWapServer,
    SendBotMessageParams,
} from "../../Models/WapServer/WapServer.Interface";
import { WapServerService } from "./WapServer.Service";
import { ApiVoidResult } from "../../Models/Api.VoidResult.type";

@Route("wapserver/quit")
export class WapServerQuitController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async wapServerQuit(
        @Body() requestBody: AssingAvailableServerParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new WapServerService().quitServer(requestBody);
            this.setStatus(200);
            return {
                success: true,
                message: "Done",
            };
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("wapserver/assign")
export class WapServerAssignController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async wapServerAssign(
        @Body() requestBody: AssingAvailableServerParams
    ): Promise<IWapServer | ApiValidationError> {
        try {
            const server = await new WapServerService().assignAvailableServer(requestBody);
            this.setStatus(200);
            return server;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("wapserver/create")
export class WapServerCreateController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async wapServerCreate(
        @Body() requestBody: BotEventParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new WapServerService().createBot(requestBody);
            this.setStatus(200);
            return {
                success: true,
                message: "Done",
            };
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("wapserver/start")
export class WapServerStartController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async wapServerStart(
        @Body() requestBody: BotEventParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new WapServerService().startBot(requestBody);
            this.setStatus(200);
            return {
                success: true,
                message: "Done",
            };
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("wapserver/stop")
export class WapServerStopController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async wapServerStop(
        @Body() requestBody: BotEventParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new WapServerService().stopBot(requestBody);
            this.setStatus(200);
            return {
                success: true,
                message: "Done",
            };
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("wapserver/delete")
export class WapServerDeleteController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async wapServerDelete(
        @Body() requestBody: BotEventParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new WapServerService().deleteBot(requestBody);
            this.setStatus(200);
            return {
                success: true,
                message: "Done",
            };
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("wapserver/botview")
export class WapServerViewController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async wapServerView(
        @Body() requestBody: BotEventParams
    ): Promise<BotView | ApiValidationError> {
        try {
            const status = await new WapServerService().viewBot(requestBody);
            this.setStatus(200);
            return status;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("wapserver/check")
export class WapServerCheckController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async wapServerCheck(
        @Body() requestBody: BotEventParams
    ): Promise<BotCheckStatus | ApiValidationError> {
        try {
            const status = await new WapServerService().checkBot(requestBody);
            this.setStatus(200);
            return status;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("wapserver/session")
export class WapServerSessionController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async wapServerSession(
        @Body() requestBody: BotEventParams
    ): Promise<BotCheckSession | ApiValidationError> {
        try {
            const status = await new WapServerService().checkSession(requestBody);
            this.setStatus(200);
            return status;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("wapserver/send")
export class WapServerSendMessageController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async wapServerSendMessage(
        @Body() requestBody: SendBotMessageParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new WapServerService().sendMessage(requestBody);
            this.setStatus(200);
            return {
                success: true,
                message: "Done",
            };
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("wapserver/qr")
export class WapServerGetQRController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async wapServerSendMessage(
        @Body() requestBody: BotEventParams
    ): Promise<string | ApiValidationError> {
        try {
            const pixBuf = await new WapServerService().getQr(requestBody);
            // Headers para imagen PNG
            this.setStatus(200);
            return pixBuf;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}
