// Cokoliv co má něco společnýho s vojsem

const fs = require('fs');

function vypocitatCas(vojsl) {
  let c = new Date();
  let d = new Date();
  let hod = d.getHours();
  let min = d.getMinutes();
  if (min >= 30) {
    min = 0;
    if (hod == 23) {
      hod = 0;
      d.setDate(c.getDate() + 1);
    } else {
      hod++;
    }
  } else {
    min = 30;
  }
  d.setHours(hod);
  d.setMinutes(min);
  d.setSeconds(0);

  timeouty[vojsl.channel.guild.id] = setTimeout(function() {
    rekniCas(vojsl, `${nula(hod)}${nula(min)}`);
  }, d - c + 3000);
}

function rekniCas(vojsl, cas) {
  if (!vojsl) return;
  const d1 = vojsl.play(`./zvuky/intro.mp3`, { volume: 0.8 });
  d1.on('finish', function () {
    const d2 = vojsl.play(`./zvuky/${cas}.mp3`, { volume: 1.5 });
    d2.on('finish', function () {
      vojsl.play(`./zvuky/grg.mp3`, { volume: 0.5 });
    });
  });

  vypocitatCas(vojsl);
}

function nula(a) {
  return a < 10 ? "0" + a : a;
}

const vytahnout = (clen, patro) => {
  const vojs = clen.voice.channel
  if (!vojs) return;

  const aktPatro = Number(vojs.name) || 0;
  let dalsiPatro = aktPatro;
  if (patro < aktPatro) dalsiPatro--; else if (patro > aktPatro) dalsiPatro++; else return;
  if (dalsiPatro === 0) dalsiPatro = 'P';

  const dalsiVojsy = [...clen.guild.channels.cache.values()].filter(channel => channel.type === 'voice' && channel.name === String(dalsiPatro));
  if (!dalsiVojsy) return;

  clen.voice.setChannel(dalsiVojsy[0]);
  setTimeout(() => vytahnout(clen, patro), 1000);
};

const timeouty = {};

module.exports = {
  more_komand: (mes, komand, celArgs) => {
    switch (komand) {

      case 'wojs':
        mes.channel.send(`vojs se pise s normalnim v ti kriple ${mes.author}`);
        break;

      case 'vojs':
        const channel = mes.member.voice.channel;
        if (!channel) {
          mes.channel.send(`di si tam sam ne ty gadzo ${mes.author}`);
          break;
        }

        if (celArgs !== 'potichu') mes.channel.send('<@&591306633196339261> vojs');

        if (mes.guild.voice) {
          if (mes.guild.voice.channel === channel && mes.guild.voice.connection) break;
        }

        channel.join()
          .then(con => {
            if (!timeouty[mes.guild.id]) {
              vypocitatCas(con);
              con.on('disconnect', () => {
                if (timeouty[mes.guild.id]) {
                  clearTimeout(timeouty[mes.guild.id]);
                  delete timeouty[mes.guild.id];
                }
              });
            }
            setTimeout(() => con.play('./zvuky/nazdar.ogg'), 500);
          });
        break;

      case "odejdi":
      case "disconnect":
      case "leave":
      case "odpoj":
      case "votpoj":
      case "vodpoj":
      case "vodprejskni":
      case "tahni":
      case "táhni":
      case "vypadni":
        let vojs = mes.guild.voice;
        if (!vojs) vojs = {};
        if (!vojs.channel) {
          mes.channel.send('nejsem ve vojsu');
          break;
        }
        vojs.channel.leave();
        mes.react('purfieRIP:644609482843881483');
        break;

      case 'vitah':
      case 'vytah':
        if (!mes.member) {
          mes.channel.send('kokot');
        }
        if (!mes.member.voice.channel) {
          mes.channel.send(`nejsi ve vojsu ty gadzo ${mes.author}`);
          break;
        }
        if (!celArgs) {
          mes.channel.send(`napis do jakiho patra ${mes.author}`);
          break;
        }

        vytahnout(mes.member, Number(celArgs) || 0);
        break;

      default:
        return false;
    }
    return true;
  },

  // Neodposlouchávej
  on_voiceStateUpdate: (bef, aft) => {
    if (!aft.channel || !aft.guild.voice?.connection || aft.member.user === aft.guild.client.user) return;

    if (aft.selfMute && !aft.deaf) {
      aft.guild.voice.connection.play('./zvuky/neodposlouchavej.ogg', { volume: 0.38 })
        .on('finish', () => { if (aft.selfMute) aft.setDeaf(true, 'otposlouchávala ta gadza') });
    }

    if (!bef.channel) return;
    if (bef.mute && bef.deaf && !aft.mute && aft.deaf) aft.setDeaf(false, 'us muze poslouchat gadza');
  }
};
