import fs from "fs";
import { config } from "../config.js";

function changeVoiceStatus(client, status, members) {
  const changeStatus = (type, value, members) => {
    switch (type) {
      case "mute":
        for (let member of members) {
          member[1].voice.setMute(value);
        }
        break;
      case "deaf":
        for (let member of members) {
          member[1].voice.setDeaf(value);
        }
        break;
    }
  };

  var voiceChannel = client.channels.cache.get(config.voiceChannelId);
  var connectedMembers = voiceChannel.members;

  if (members == "all") {
    members = connectedMembers;
  }

  switch (status) {
    case "mute":
      changeStatus("mute", true, members);
      break;
    case "unmute":
      changeStatus("mute", false, members);
      break;
    case "deaf":
      changeStatus("deaf", true, members);
      break;
    case "undeaf":
      changeStatus("deaf", false, members);
      break;
  }
}

function getJSONData(file) {
  const jsonFile = fs.readFileSync(file);
  const data = JSON.parse(jsonFile);

  return data;
}

function getRandomInt(max) {
  return Math.floor(Math.random() * max);
}

function isConnectedToVoiceChannel(client, userId) {
  var voiceChannel = client.channels.cache.get(config.voiceChannelId);
  var connectedMembers = voiceChannel.members;

  var selectedMember = connectedMembers.filter(
    (member) => member.user.id === userId
  );

  if (selectedMember.size === 1) {
    return true;
  }

  return false;
}

export {
  changeVoiceStatus,
  getJSONData,
  getRandomInt,
  isConnectedToVoiceChannel,
};
