/**
 * @overview Life of German Reallife - Voice Main main.ts
 * @author Zeuto
 * @copyright (c) 2008 - 2019 Life of German
 */

export default function voice(globalData: GlobalData) {

    let dictLoaded = false;
    const Use3d = true;
    const UseAutoVolume = false;
    const MaxRange = 30.0;

    mp.voiceChat.muted = true; // initialize muted

    var listeners: Array<PlayerMp> = new Array<PlayerMp>();

    mp.keys.bind(0x73, false, _ => {
        mp.voiceChat.muted = !mp.voiceChat.muted;
    });

    let gX = 0.5;
    let gY = 0.5;

    mp.events.add('render', () => {

        if (globalData.LoggedIn) {
            if (!dictLoaded) {
                mp.game.graphics.requestStreamedTextureDict("voiceimages", true);
                dictLoaded = true;
            }

            var { x, y } = mp.game.graphics.getScreenActiveResolution(0, 0);

            if (!mp.voiceChat.muted) {
                mp.game.graphics.drawSprite("voiceimages", "microphone-black-shape", 0.165, 0.952, 0.015, 0.015 * (x / y), 0, 0, 102, 0, 255);
            } else {
                mp.game.graphics.drawSprite("voiceimages", "microphone-off", 0.165, 0.952, 0.015, 0.015 * (x / y), 0, 255, 51, 51, 255);
            }
        }
    })

    function addListener(target: PlayerMp) {

        listeners.push(target);
        mp.events.callRemote("CLIENT:AddVoiceListener", target);

        if (UseAutoVolume) {
            target.voiceAutoVolume = true;
        } else {
            target.voiceVolume = 1.0;
        }
        if (Use3d) {
            target.voice3d = true;
        }
    }

    function removeListener(target: PlayerMp) {
        let idx = listeners.indexOf(target)

        if (idx !== -1) {
            listeners.splice(idx, 1);
        }

        mp.events.callRemote("CLIENT:RemoveVoiceListener");
    }

    setInterval(() => {
        let localPlayer = mp.players.local;
        let localPos = localPlayer.position;

        mp.players.forEachInStreamRange(player => {
            if (player != localPlayer) {
                if (!listeners.includes(player)) {
                    const playerPos = player.position;
                    let dist = mp.game.system.vdist(playerPos.x, playerPos.y, playerPos.z, localPos.x, localPos.y, localPos.z);

                    if (dist <= MaxRange) {
                        addListener(player);
                    }
                }
            }
        });

        listeners.forEach((player: PlayerMp) => {
            if (player) {
                if (player.handle !== 0) {
                    const playerPos = player.position;
                    let dist = mp.game.system.vdist(playerPos.x, playerPos.y, playerPos.z, localPos.x, localPos.y, localPos.z);

                    if (dist > MaxRange) {
                        removeListener(player);
                    } else if (!UseAutoVolume) {
                        player.voiceVolume = 1 - (dist / MaxRange);
                    }
                }
                else {
                    removeListener(player);
                }
            }
        })
    }, 500)
}
