import { Request, Response } from "express";
import { SocialNetworkInstagramsMethods } from "../../methods/social-network/social-network-instagram.method";
import { SocialNetworkInstagramsModel } from "../../models/social-network/social-network-instagram.model";
import {
  IgApiClient,
  PostingAlbumOptions,
  PostingPhotoOptions,
  PostingStoryPhotoOptions,
  PostingStoryVideoOptions,
  PostingVideoOptions,
} from "../../utils/plugins/bot-intagram";
import { AbstractController } from "../abstract.controller";

export class SocialNetworkInstagramsController extends AbstractController {
  methods: SocialNetworkInstagramsMethods;
  constructor() {
    super(SocialNetworkInstagramsModel);
    this.methods = new SocialNetworkInstagramsMethods(
      SocialNetworkInstagramsModel
    );
  }

  /**
   * Create record with public source
   * @param {*} req
   * @param {*} res
   */
  async session(req: Request | any, res: Response) {
    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);
    }
  }
  /**
   * Create record with public source
   * @param {*} req
   * @param {*} res
   */
  async connect(req: Request | any, res: Response) {
    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: Request | any, res: Response) {
    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: Request, res: Response) {
    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: Request, res: Response) {
    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: Request, res: Response) {
    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: Request, res: Response) {
    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: Request, res: Response) {
    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: Request | any, res: Response) {
    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: Request | any, res: Response) {
    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: Request | any, res: Response) {
    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: Request | any, res: Response) {
    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);
    }
  }
}
