text: const User = require("../../database/Schemas/User");
  const Command = require("../../structures/Command");
  const { loadImage, registerFont, createCanvas } = require("canvas");
  registerFont("src/assets/fonts/Montserrat-Black.ttf", { family: "Montserrat" });
  registerFont("src/assets/fonts/Segoe Print.ttf", { family: "Segoe Print" });
  registerFont("src/assets/fonts/Segoe UI.ttf", { family: "Segoe UI" });
  registerFont("src/assets/fonts/Segoe UI Black.ttf", {
    family: "Segoe UI Black",
  });
  const Utils = require("../../utils/Util");
  
  const { MessageAttachment, Util } = require("discord.js");
  
  module.exports = class Profile extends Command {
    constructor(client) {
      super(client);
      this.client = client;
  
      this.name = "profile";
      this.category = "Miscellaneous";
      this.description = "Veja seu perfil com este comando";
      this.usage = "profile <@user>";
      this.aliases = ["perfil", "p"];
  
      this.enabled = true;
      this.guildOnly = true;
    }
  
    async run({ message, args, prefix, author }, t) {
  
      const USER =
        this.client.users.cache.get(args[0]) ||
        message.mentions.users.first() ||
        message.author;
  
      User.findOne({ idU: USER.id }, async (err, user) => {
  
        const canvas = createCanvas(1280, 720);
        const ctx = canvas.getContext("2d");
        let nextLevel = user.Exp.nextLevel * user.Exp.level;
  
        //========================// Import Background //========================//
  
        const background = await loadImage(
          "./src/assets/img/jpeg/background.jpg"
        );
        ctx.drawImage(background, 0, 0, 1280, 720);
  
  			//========================// Import badgade //========================//
  
  			
        if(user.badges.includes("sthk")) {
          const badgadehfs = await loadImage("./src/assets/img/png/staffhk.png");
          ctx.drawImage(badgadehfs, 227, 650, 50, 50);
        } if(user.badges.includes("developer")) {
          const badgadehfs = await loadImage("./src/assets/img/png/bgstaff.png");
          ctx.drawImage(badgadehfs, 227, 650, 50, 50);
        } if(user.badges.includes("helperhf")) {
          const badgadehfs = await loadImage("./src/assets/img/png/helperhf.png");
          ctx.drawImage(badgadehfs, 227, 650, 50, 50);
  			} if(user.badges.includes("hfbg")) {
          const badgadehfs = await loadImage("./src/assets/img/png/hfbg.png");
          ctx.drawImage(badgadehfs, 270, 650, 50, 60);
        } if(user.badges.includes("droid-tech")) {
          const badgadehfs = await loadImage("./src/assets/img/png/tdbg.png");
          ctx.drawImage(badgadehfs, 320, 650, 50, 60);
  			} if(user.badges.includes("rehash")) {
          const badgadehfs = await loadImage("./src/assets/img/png/rhbg.png");
          ctx.drawImage(badgadehfs, 370, 645, 70, 70);
  			} if(user.badges.includes('suportint')) {
          const badgadehfs = await loadImage("./src/assets/img/png/suportint.png");
          ctx.drawImage(badgadehfs, 450, 645, 70, 70);
  			} if(user.badges.includes('phf')) {
          const badgadehfs = await loadImage("./src/assets/img/png/phf.png");
          ctx.drawImage(badgadehfs, 515, 636, 95, 95);
  			} if(user.badges.includes('hkbg')) {
          const badgadehfs = await loadImage("./src/assets/img/png/hkbg.png");
          ctx.drawImage(badgadehfs, 830, 25, 400, 200);
  			} if(user.badges.includes('verify')) {
          const badgadehfs = await loadImage("./verify.png");
          ctx.drawImage(badgadehfs, 570, 650, 106, 60);
  			} if(user.badges.includes('progv')) {
          const badgadehfs = await loadImage("./progv.png");
          ctx.drawImage(badgadehfs, 640, 630, 110, 106);
  			} if(user.badges.includes('cb')) {
          const badgadehfs = await loadImage("./cb.png");
          ctx.drawImage(badgadehfs, 730, 630, 110, 100);
  			} 
  
  
  
  
        //========================// Import BreakLines //========================//
  
        function addBreakLines(str, max) {
          max = max + 1;
          for (let i = 0; i < str.length / max; i++) {
            str =
              str.substring(0, max * i) +
              `\n` +
              str.substring(max * i, str.length);
          }
          return str;
        }
  
        //========================// Texts //========================//
  
        // Username
  
        ctx.textAlign = "left";
        ctx.font = '50px "Segoe UI Black"';
        ctx.fillStyle = "rgb(253, 255, 252)";
        await Utils.renderEmoji(
          ctx,
          USER.username.length > 20
            ? USER.username.slice(0, 20) + "..."
            : USER.username,
          180,
          60
        );
  
        // Titles
  
        ctx.textAlign = "left";
        ctx.font = '30px "Segoe UI Black"';
        ctx.fillStyle = "rgb(253, 255, 252)";
        ctx.fillText("Frames", 190, 90);
        ctx.fillText("Xp", 190, 155);
        // badgade oficial
  
        ctx.textAlign = "left";
        ctx.font = '30px "Segoe UI Black"';
        ctx.fillStyle = "rgb(253, 255, 252)";
        ctx.fillText("badgade hf:", 40,  687);
      //\n
  
        // Coins/XP
  
        ctx.textAlign = "left";
        ctx.font = '30px "Segoe UI"';
        ctx.fillStyle = "rgb(253, 255, 252)";
        ctx.fillText(`${Utils.toAbbrev(user.coins)}`, 190, 120);
        ctx.fillText(
          `#${user.Exp.level} | ${Utils.toAbbrev(user.Exp.xp)}/${Utils.toAbbrev(
            nextLevel
          )}`,
          190,
          185
        );
  
        // Sobre
  
        ctx.font = '25px "Montserrat"';
        ctx.fillText(
          addBreakLines(
            String(
              user.about == "null"
                ? `Use ${prefix}sobremim <msg> para alterar essa mensagem`
                : user.about
            ),
            77
          ),
          30,
          500
        );
  
        //========================// Import Avatar //========================//
  //.            x    y   t   
        ctx.arc(100, 110, 80, 0, Math.PI * 2, true);
        ctx.lineWidth = 6;
        ctx.strokeStyle = "#faf5f5";
        ctx.stroke();
        ctx.closePath();
        ctx.clip();
  
        const avatar = await loadImage(USER.displayAvatarURL({ format: "jpeg" }));    //  x   y
        ctx.drawImage(avatar, 17, 30, 165, 175);//15, 15, 175, 175
  
        //========================// Create Image //========================//
  
        const attach = new MessageAttachment(
          canvas.toBuffer(),
          `Profile_${USER.tag}_.png`
        );
  
        message.quote(attach);
      });
    }
  };
  