import { transporterSMTP } from "../../config/email";
import { EmailMarkers } from "../../utils/markers";
import { htmlRenders } from "../../utils/renders/html.renders";
import { AbstractMethods } from "../abstract.method";

const verifications = [];
export class SocialNetworkEmailsMethods extends AbstractMethods {
  constructor(model) {
    super(model);
  }
  async render(text, data) {
    return await htmlRenders(text, data);
  }
  send(options, text, data = {}, email) {
    return new Promise((resolve, reject) => {
      let markers = [];
      markers = Object.keys(data).map((d) => {
        return EmailMarkers.hasOwnProperty(d)
          ? EmailMarkers[d]({ email })
          : new Promise((r) => r(data[d]));
      });
      console.log(markers)
      if (markers.length) {
        Promise.allSettled(markers)
          .then((r) => {
            let dataParsed = r.reduce((prev, next: any, i) => {
              return {
                ...prev,
                [Object.keys(data)[i]]: next.value
                  ? next.value
                  : data[Object.keys(data)[i]],
              };
            }, {});
            this.render(text, dataParsed)
              .then((html) => {
                const mailOptions = {
                  from: "Cupen systems <no-reply@cupensystems.com>",
                  to: email,
                  subject: options.subject,
                  html,
                };
                transporterSMTP.sendMail(mailOptions, (error, info) => {
                  if (error) {
                    reject("ERROR: " + JSON.stringify(error));
                    console.log(error);
                  } else {
                    resolve(info);
                  }
                });
              })
              .catch((error) => {
                reject("ERROR: " + JSON.stringify(error));
              });
          })
          .catch((error) => {
            reject("ERROR: " + JSON.stringify(error));
          });
      } else {
        console.log(text)
        this.render(text, {})
          .then((html) => {
            const mailOptions = {
              from: "Cupen systems <no-reply@cupensystems.com>",
              to: email,
              subject: options.subject,
              html,
            };
            transporterSMTP.sendMail(mailOptions, (err, info) => {
              if (err) {
                reject("ERROR: " + err);
                console.log(err);
              } else {
                resolve(info);
              }
            });
          })
          .catch((error) => {
            reject("ERROR: " + JSON.stringify(error));
          });
      }
    });
  }
  async sendVerification(body: { email: string }) {
    try {
      const code = Math.floor(Math.random() * (9999 - 1000 + 1) + 1000) + "";

      verifications[body.email] = code;
      const from = "Cupen Sys";
      const html = `Code: ${code}.`;
      return await this.send(
        { subject: "Verify" },
        html,
        {},
        body.email.trim() ?? ""
      );
    } catch (error) {
      console.log(error);
      throw error;
    }
  }
  verify(number, code) {
    return verifications[number] + "" === code + "";
  }
}
