import cfg from "../config.mjs";
import sql from "../sql.mjs";
import lib from "../lib.mjs";
import { getLevel } from "../admin.mjs";
import fetch from "flumm-fetch-cookies";

import fs from "fs";
import { exec as _exec } from "child_process";

const exec = cmd => new Promise((resolve, reject) => {
  _exec(cmd, { maxBuffer: 5e3 * 1024 }, (err, stdout, stderr) => {
    if(err)
      return reject(err);
    if(stderr)
      console.error(stderr);
    resolve({ stdout: stdout });
  });
});

//const regex = /https?:\/\/[\w\S(\.|:|/)]+/gi;
const regex = /https?:\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?/gi;

export default async bot => {

  return [{
    name: "parser",
    call: regex,
    active: true,
    f: e => {
      const links = e.message.match(regex)?.filter(link => !link.includes("f0ck.me")) || [];

      if(e.photo)
        links.push(e.photo);

      if(links.length === 0)
        return false;
      
      if(e.message.match(/(!|-)ignore/))
        return false;

      if(!e.channel.includes("f0ck") && !e.message.match(/(!|-)f0ck/i))
        return false;

      console.log(`parsing ${links.length} link${links.length > 1 ? "s" : ""}...`);

      links.forEach(async link => {
        // check repost (link)
        const q_repost = await sql("items").select("id").where("src", link);
        if(q_repost.length > 0)
          return e.reply(`repost motherf0cker (link): ${cfg.main.url}/${q_repost[0].id}`);

        // generate uuid
        const uuid = (await sql.select(sql.raw("gen_random_uuid() as uuid")))[0].uuid.substring(0, 8);

        const maxfilesize = (getLevel(e.user).level > 50 ? cfg.main.maxfilesize * cfg.main.adminmultiplier : cfg.main.maxfilesize) / 1024;

        let meta;
        // read metadata
        try {
          meta = JSON.parse((await exec(`yt-dlp -f 'bv*[height<=720]+ba/b[height<=720] / wv*+ba/w' --skip-download --dump-json "${link}"`)).stdout);
        }
        catch(err) {
          //e.reply("[error] f0ck has no bock :(");
          //console.error(err);
          return;
        }

        if(!Object.values(cfg.mimes).includes(meta.ext.toLowerCase())) {
          const tmphead = (await fetch(link, { method: "HEAD" })).headers["content-type"];
          if(!Object.keys(cfg.mimes).includes(tmphead))
            return;
          meta.ext = cfg.mimes[tmphead];
        }

        let filename = `${uuid}.${meta.ext}`;

        e.reply(`[charging the f0cker] downloading: ${uuid}`);

        // download data
        const start = new Date();
        let source;
        if(meta.ext === "mp4") {
          source = (await exec(`yt-dlp -f 'bv*[height<=720]+ba/b[height<=720] / wv*+ba/w' "${link}" --max-filesize ${maxfilesize}k --merge-output-format mp4 -o ./tmp/${filename}`)).stdout.trim();
          //change 720 to any other available resolution, higher = better quality but bigger filesize
        }
        else {
          source = (await exec(`yt-dlp -f 'bv*[height<=720]+ba/b[height<=720] / wv*+ba/w' "${link}" --max-filesize ${maxfilesize}k -o ./tmp/${filename}`)).stdout.trim();
          //change 720 to any other available resolution, higher = better quality but bigger filesize
        }

        if(source.match(/larger than/))
          return e.reply("too large lol");
        const end = ~~((new Date() - start) / 1e3);
        
        // generate checksum
        const checksum = (await exec(`sha256sum ./tmp/${filename}`)).stdout.trim().split(" ")[0];
        const size = fs.statSync(`./tmp/${filename}`).size;

        // mime check
        const mime = (await exec(`file --mime-type -b ./tmp/${filename}`)).stdout.trim();
        if(!Object.keys(cfg.mimes).includes(mime))
          return e.reply(`lol, go f0ck yourself (${mime})`);
        
        if(!Object.values(cfg.mimes).includes(meta.ext.toLowerCase())) {
          let tmpext = cfg.mimes[meta.ext.toLowerCase()];
          fs.renameSync(`./tmp/${filename}`, `./tmp/${uuid}.${tmpext}`);
          filename = `${uuid}.${tmpext}`;
        }

        // check repost (checksum)
        const q_repostc = await sql("items").select("id").where("checksum", checksum);
        if(q_repostc.length > 0)
          return e.reply(`repost motherf0cker (checksum): ${cfg.main.url}/${q_repostc[0].id}`);

        await fs.promises.copyFile(`./tmp/${filename}`, `./public/b/${filename}`);
        await fs.promises.unlink(`./tmp/${filename}`).catch(_=>{});

        await sql("items").insert({
          src: e.photo ? "" : link,
          dest: filename,
          mime: mime,
          size: size,
          checksum: checksum,
          username: e.user.nick || e.user.username,
          userchannel: e.channel,
          usernetwork: e.network,
          stamp: ~~(new Date() / 1000),
          active: 1
        });
        const itemid = (await sql('items').where('dest', filename).limit(1))[0].id;

        // generate thumbnail
        try {
          if(mime.startsWith('video/') || mime == 'image/gif')
            await exec(`ffmpegthumbnailer -i./public/b/${filename} -s1024 -o./tmp/${itemid}.png`);
          else if(mime.startsWith('image/') && mime != 'image/gif')
            await exec(`convert ./public/b/${filename} ./tmp/${itemid}.png`);
          else if(mime.startsWith('audio/')) {
            if(link.match(/soundcloud/)) {
              let cover = (await exec(`yt-dlp -f 'bv*[height<=720]+ba/b[height<=720] / wv*+ba/w' --get-thumbnail "${link}"`)).stdout.trim();
              if(!cover.match(/default_avatar/)) {
                cover = cover.replace(/-(large|original)\./, '-t500x500.');
                try {
                  await exec(`wget "${cover}" -O ./tmp/${itemid}.jpg`);
                  const size = (await fs.promises.stat(`./tmp/${itemid}.jpg`)).size;
                  if(size >= 0) {
                    await exec(`convert ./tmp/${itemid}.jpg ./tmp/${itemid}.png`);
                    await exec(`convert ./tmp/${itemid}.jpg ./public/ca/${itemid}.webp`);
                  }
                } catch(err) {}
              }
              else {
                await exec(`ffmpeg -i ./public/b/${filename} -update 1 -map 0:v -map 0:1 -c copy ./tmp/${itemid}.png`);
                await exec(`convert ./tmp/${itemid}.png ./public/ca/${itemid}.webp`);
              }
            }
            else {
              await exec(`ffmpeg -i ./public/b/${filename} -update 1 -map 0:v -map 0:1 -c copy ./tmp/${itemid}.png`);
              await exec(`convert ./tmp/${itemid}.png ./public/ca/${itemid}.webp`);
            }
          }
      
          await exec(`convert "./tmp/${itemid}.png" -resize "128x128^" -gravity center -crop 128x128+0+0 +repage ./public/t/${itemid}.webp`);
          await fs.promises.unlink(`./tmp/${itemid}.png`).catch(err => {});
          await fs.promises.unlink(`./tmp/${itemid}.jpg`).catch(err => {});
        } catch(err) {
          await exec(`convert ./mugge.png ./public/t/${itemid}.webp`);
        }

        let speed = lib.calcSpeed(size, end);
        speed = !Number.isFinite(speed) ? "yes" : `${speed.toFixed(2)} Mbit/s`;

        // autotagger
        let tag;
        try {
          if(mime.startsWith('image') && mime != 'image/gif') {
            const body = { headers: { Authorization: `Basic ${cfg.tagger.btoa}` } };
            const res = await (await fetch(`${cfg.tagger.endpoint}/categories/nsfw_beta?image_url=${cfg.main.url}/b/${filename}`, body)).json();
            if(res)
              tag = (res.result.categories[0].name.en === 'safe') ? 'sfw' : 'nsfw';
          }
          else if(mime.startsWith('audio')) {
            tag = 'sfw';
          }

          if(tag === 'sfw' || tag === 'nsfw') {
            await sql("tags_assign").insert({
              tag_id: tag === "sfw" ? 1 : 2,
              item_id: itemid,
              user_id: 7 // user: autotagger (ID: 7)
            });
          }
        } catch(err) {
          console.error(err);
        }

        e.reply([
          `[f0cked] link: ${cfg.main.url}/${itemid} | size: ${lib.formatSize(size)} | speed: ${speed}` + (tag ? ` | ${tag}` : "")
        ]);

      });
    }
  }];
};
