"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SocialNetworkWhatsappsController = void 0;
const abstract_controller_1 = require("../abstract.controller");
const social_network_whatsapp_model_1 = require("../../models/social-network/social-network-whatsapp.model");
const social_network_whatsapp_method_1 = require("../../methods/social-network/social-network-whatsapp.method");
class SocialNetworkWhatsappsController extends abstract_controller_1.AbstractController {
    constructor() {
        super(social_network_whatsapp_model_1.SocialNetworkWhatsappsModel);
        this.methods = new social_network_whatsapp_method_1.SocialNetworkWhatsappsMethods(social_network_whatsapp_model_1.SocialNetworkWhatsappsModel);
    }
    async session(req, res) {
        try {
            const matchedData = this.params.matchedData(req);
            const id = matchedData.id;
            const session = await this.methods.loadSession(id);
            if (session) {
                return res.send('SESSION_FOUND');
            }
            else {
                this.params.handleHttpError(res, { message: 'SESSION_NOT_FOUND' });
            }
        }
        catch (e) {
            this.params.handleHttpError(res, e);
        }
    }
    async connect(req, res) {
        try {
            const matchedData = this.params.matchedData(req);
            const name = req.user_id + matchedData.username;
            return this.methods.createSession(name, res);
        }
        catch (e) {
            this.params.handleHttpError(res, e);
        }
    }
    async disconnect(req, res) {
        try {
            const matchedData = this.params.matchedData(req);
            const name = req.user_id + matchedData.username;
            try {
                this.methods.removeSession(name);
                return res.send('SESSION_REMOVED');
            }
            catch (error) {
                this.params.handleHttpError(res, error);
            }
        }
        catch (e) {
            this.params.handleHttpError(res, e);
        }
    }
    async sendMessages(req, res) {
        try {
            const matchedData = this.params.matchedData(req);
            const name = req.user_id + matchedData.username;
            this.methods
                .sendMessages(name, matchedData.list)
                .then((response) => {
                this.methods
                    .saveDocument({ ...matchedData, response }, req)
                    .then((r) => {
                    res.send(r);
                })
                    .catch((error) => {
                    this.params.handleHttpError(res, error);
                });
            })
                .catch((error) => {
                this.params.handleHttpError(res, error);
            });
        }
        catch (e) {
            console.log(e);
            this.params.handleHttpError(res, e);
        }
    }
    async sendPhotos(req, res) {
        try {
            const matchedData = this.params.matchedData(req);
            const name = req.user_id + matchedData.username;
            this.methods
                .sendPhotos(name, matchedData.list)
                .then((response) => {
                this.methods
                    .saveDocument({ ...matchedData, response }, req)
                    .then((r) => {
                    res.send(r);
                })
                    .catch((error) => {
                    this.params.handleHttpError(res, error);
                });
            })
                .catch((error) => {
                this.params.handleHttpError(res, error);
            });
        }
        catch (e) {
            console.log(e);
            this.params.handleHttpError(res, e);
        }
    }
    async sendVideos(req, res) {
        try {
            const matchedData = this.params.matchedData(req);
            const name = req.user_id + matchedData.username;
            this.methods
                .sendVideos(name, matchedData.list)
                .then((response) => {
                this.methods
                    .saveDocument({ ...matchedData, response }, req)
                    .then((r) => {
                    res.send(r);
                })
                    .catch((error) => {
                    this.params.handleHttpError(res, error);
                });
            })
                .catch((error) => {
                this.params.handleHttpError(res, error);
            });
        }
        catch (e) {
            console.log(e);
            this.params.handleHttpError(res, e);
        }
    }
    async sendAudios(req, res) {
        try {
            const matchedData = this.params.matchedData(req);
            const name = req.user_id + matchedData.username;
            this.methods
                .sendAudios(name, matchedData.list)
                .then((response) => {
                this.methods
                    .saveDocument({ ...matchedData, response }, req)
                    .then((r) => {
                    res.send(r);
                })
                    .catch((error) => {
                    this.params.handleHttpError(res, error);
                });
            })
                .catch((error) => {
                this.params.handleHttpError(res, error);
            });
        }
        catch (e) {
            console.log(e);
            this.params.handleHttpError(res, e);
        }
    }
}
exports.SocialNetworkWhatsappsController = SocialNetworkWhatsappsController;
