import { Request, Response } from "express";
import { AbstractController } from "../abstract.controller";
import { SocialNetworkWhatsappsModel } from "../../models/social-network/social-network-whatsapp.model";
import { SocialNetworkWhatsappsMethods } from "../../methods/social-network/social-network-whatsapp.method";

export class SocialNetworkWhatsappsController extends AbstractController {
  methods: SocialNetworkWhatsappsMethods;
  constructor() {
    super(SocialNetworkWhatsappsModel);
    this.methods = new SocialNetworkWhatsappsMethods(
      SocialNetworkWhatsappsModel
    );
  }

  /**
   * Session
   * @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) {
      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 name = req.user_id + matchedData.username;
      return this.methods.createSession(name, res);
    } catch (e) {
      this.params.handleHttpError(res, e);
    }
  }
  async disconnect(req: Request | any, res: Response) {
    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: Request | any, res: Response) {
    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: Request | any, res: Response) {
    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: Request | any, res: Response) {
    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: Request | any, res: Response) {
    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);
    }
  }
  // async sendStory(req: any, res: Response) {
  //   try {
  //     // const body = req.body;
  //     // const { file } = req;
  //     // console.log({ file, body });
  //     // if (body.name && bot.hasOwnProperty(body.name)) {
  //     //   bot[body.name]
  //     //     .sendMessage("status@broadcast", "test", {
  //     //       options: {
  //     //         media:
  //     //           "https://fastly.picsum.photos/id/496/1000/1000.jpg?hmac=ywTDTNfBOA6neOtR1skTT7ITBiKA97CxRjxCsC5lRb0",
  //     //       },
  //     //     })
  //     //     .then((r) => {
  //     //       console.log(r);
  //     //       res.send({ msg: "SENT" });
  //     //     })
  //     //     .catch((error) => {
  //     //       res.send({ msg: "ERROR", error });
  //     //     });
  //     // } else {
  //     //   res.send({ msg: "ERROR_NOT_HAVE_SESSION" });
  //     // }
  //   } catch (e) {
  //     console.log(e);
  //     this.params.handleHttpError(res, e);
  //   }
  // }
}
