import { Response } from "express";
import * as mime from "mime-types";
import { SocialNetworkConnectsModel } from "../../models/social-network/social-network-connect.model";
import {
  Session,
  createSession,
  deleteSession,
  getSession,
} from "../../utils/plugins/bot-whatsapp/wa";
import { WebWhatsappProvider } from "../../utils/plugins/whatsapp.plugin";
import { AbstractMethods } from "../abstract.method";
export class SocialNetworkWhatsappsMethods extends AbstractMethods {
  constructor(model) {
    super(model);
  }
  createSession(sessionId: string, res: Response): void {
    createSession({ sessionId, res });
    return;
  }
  removeSession(sessionId: string) {
    deleteSession(sessionId);
  }
  async loadSession(sessionId: string): Promise<Session | undefined> {
    return getSession(sessionId);
  }
  sendMessages(
    sessionId: string,
    list: { number: string; message: string; options: any }[]
  ) {
    return new Promise(async (resolve, reject) => {
      try {
        const listPromises = list.map((item) => {
          const provider: WebWhatsappProvider = new WebWhatsappProvider(
            sessionId
          );
          return provider.sendMessage(
            item.number,
            { text: item.message },
            item.options
          );
        });
        const responses = await Promise.allSettled(listPromises);
        resolve(responses);
      } catch (error) {
        reject(error);
      }
    });
  }
  sendPhotos(
    sessionId: string,
    list: { number: string; message: string; options: any }[]
  ) {
    return new Promise(async (resolve, reject) => {
      try {
        const listPromises = list.map((item) => {
          const provider: WebWhatsappProvider = new WebWhatsappProvider(
            sessionId
          );
          if (item.options?.media) {
            const mimeType = mime.lookup(item.options?.media);
            if (mimeType.includes("image")) {
              return provider.sendMessage(
                item.number,
                { text: item.message },
                item.options
              );
            } else {
              return new Promise((rs, rj) => {
                rs(false)
              })
            }
          } else {
            return new Promise((rs, rj) => {
              rs(false)
            })
          }
        });
        const responses = await Promise.allSettled(listPromises);
        resolve(responses);
      } catch (error) {
        reject(error);
      }
    });
  }
  sendVideos(
    sessionId: string,
    list: { number: string; message: string; options: any }[]
  ) {
    return new Promise(async (resolve, reject) => {
      try {
        const listPromises = list.map((item) => {
          const provider: WebWhatsappProvider = new WebWhatsappProvider(
            sessionId
          );
          if (item.options?.media) {
            const mimeType = mime.lookup(item.options?.media);
            if (mimeType.includes("video")) {
              return provider.sendMessage(
                item.number,
                { text: item.message },
                item.options
              );
            } else {
              return new Promise((rs, rj) => {
                rs(false)
              })
            }
          } else {
            return new Promise((rs, rj) => {
              rs(false)
            })
          }
        });
        const responses = await Promise.allSettled(listPromises);
        resolve(responses);
      } catch (error) {
        reject(error);
      }
    });
  }
  sendAudios(
    sessionId: string,
    list: { number: string; message: string; options: any }[]
  ) {
    return new Promise(async (resolve, reject) => {
      try {
        const listPromises = list.map((item) => {
          const provider: WebWhatsappProvider = new WebWhatsappProvider(
            sessionId
          );
          if (item.options?.media) {
            const mimeType = mime.lookup(item.options?.media);
            if (mimeType.includes("audio")) {
              return provider.sendMessage(
                item.number,
                { text: item.message },
                item.options
              );
            } else {
              return new Promise((rs, rj) => {
                rs(false)
              })
            }
          } else {
            return new Promise((rs, rj) => {
              rs(false)
            })
          }
        });
        const responses = await Promise.allSettled(listPromises);
        resolve(responses);
      } catch (error) {
        reject(error);
      }
    });
  }

  refreshSession(status, req, matchedData) {
    try {
      const query = {
        store_id: req.headers.store,
        user_id: req.user_id,
        username: matchedData.username,
        provider_id: matchedData.provider_id,
      };
      const update = {
        $set: {
          store_id: req.headers.store,
          user_id: req.user_id,
          username: matchedData.username,
          provider_id: matchedData.provider_id,
          status,
        },
      };
      const options = { upsert: true };
      return SocialNetworkConnectsModel.updateOne(query, update, options);
    } catch (error) {
      throw error;
    }
  }
}
