"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SocialNetworkInstagramsController = void 0;
const social_network_instagram_method_1 = require("../../methods/social-network/social-network-instagram.method");
const social_network_instagram_model_1 = require("../../models/social-network/social-network-instagram.model");
const abstract_controller_1 = require("../abstract.controller");
class SocialNetworkInstagramsController extends abstract_controller_1.AbstractController {
    constructor() {
        super(social_network_instagram_model_1.SocialNetworkInstagramsModel);
        this.methods = new social_network_instagram_method_1.SocialNetworkInstagramsMethods(social_network_instagram_model_1.SocialNetworkInstagramsModel);
    }
    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) {
            return this.params.handleHttpError(res, e);
        }
    }
    async connect(req, res) {
        try {
            const matchedData = this.params.matchedData(req);
            const context = await this.methods.startProvider(matchedData);
            return res.send(context);
        }
        catch (e) {
            return this.params.handleHttpError(res, e);
        }
    }
    async disconnect(req, res) {
        try {
            const matchedData = this.params.matchedData(req);
            this.methods
                .deleteSession(matchedData.username)
                .then((context) => {
                return res.send('SESSION_REMOVED');
            })
                .catch((error) => {
                this.params.handleHttpError(res, error);
            });
        }
        catch (e) {
            return this.params.handleHttpError(res, e);
        }
    }
    async sendMessages(req, res) {
        const matchedData = this.params.matchedData(req);
        try {
            let ig = await this.methods.loadSession(matchedData.username);
            return this.methods.sendMessages(ig, matchedData.list)
                .then((response) => {
                res.send(response);
            }).catch((error) => {
                this.params.handleHttpError(res, error);
            });
        }
        catch (error) {
            return this.params.handleHttpError(res, error);
        }
    }
    async sendPhotos(req, res) {
        const matchedData = this.params.matchedData(req);
        try {
            let ig = await this.methods.loadSession(matchedData.username);
            return this.methods.sendPhotos(ig, matchedData.list)
                .then((response) => {
                res.send(response);
            }).catch((error) => {
                this.params.handleHttpError(res, error);
            });
        }
        catch (error) {
            return this.params.handleHttpError(res, error);
        }
    }
    async sendVideos(req, res) {
        const matchedData = this.params.matchedData(req);
        try {
            let ig = await this.methods.loadSession(matchedData.username);
            return this.methods.sendVideos(ig, matchedData.list)
                .then((response) => {
                res.send(response);
            }).catch((error) => {
                this.params.handleHttpError(res, error);
            });
        }
        catch (error) {
            return this.params.handleHttpError(res, error);
        }
    }
    async sendAudios(req, res) {
        const matchedData = this.params.matchedData(req);
        try {
            let ig = await this.methods.loadSession(matchedData.username);
            return this.methods.sendVoices(ig, matchedData.list)
                .then((response) => {
                res.send(response);
            }).catch((error) => {
                this.params.handleHttpError(res, error);
            });
        }
        catch (error) {
            return this.params.handleHttpError(res, error);
        }
    }
    async sendStories(req, res) {
        const matchedData = this.params.matchedData(req);
        try {
            let ig = await this.methods.loadSession(matchedData.username);
            return this.methods.sendStories(ig, matchedData.list)
                .then((response) => {
                res.send(response);
            }).catch((error) => {
                this.params.handleHttpError(res, error);
            });
        }
        catch (error) {
            return this.params.handleHttpError(res, error);
        }
    }
    async publishPhoto(req, res) {
        const matchedData = this.params.matchedData(req);
        try {
            let ig = await this.methods.loadSession(matchedData.username);
            return this.methods.publishPhoto(ig, matchedData.list)
                .then((response) => {
                res.send(response);
            }).catch((error) => {
                this.params.handleHttpError(res, error);
            });
        }
        catch (error) {
            console.error(error);
            return this.params.handleHttpError(res, error);
        }
    }
    async publishVideo(req, res) {
        const matchedData = this.params.matchedData(req);
        try {
            let ig = await this.methods.loadSession(matchedData.username);
            return this.methods.publishVideo(ig, matchedData.list)
                .then((response) => {
                res.send(response);
            }).catch((error) => {
                this.params.handleHttpError(res, error);
            });
        }
        catch (error) {
            console.error(error);
            return this.params.handleHttpError(res, error);
        }
    }
    async publishAlbum(req, res) {
        const matchedData = this.params.matchedData(req);
        try {
            let ig = await this.methods.loadSession(matchedData.username);
            return this.methods.publishAlbum(ig, matchedData.list)
                .then((response) => {
                res.send(response);
            }).catch((error) => {
                this.params.handleHttpError(res, error);
            });
        }
        catch (error) {
            console.error(error);
            return this.params.handleHttpError(res, error);
        }
    }
    async publishStory(req, res) {
        const matchedData = this.params.matchedData(req);
        try {
            let ig = await this.methods.loadSession(matchedData.username);
            return this.methods.publishStory(ig, matchedData.list)
                .then((response) => {
                res.send(response);
            }).catch((error) => {
                this.params.handleHttpError(res, error);
            });
        }
        catch (error) {
            console.error(error);
            return this.params.handleHttpError(res, error);
        }
    }
}
exports.SocialNetworkInstagramsController = SocialNetworkInstagramsController;
