import { isInAnyNoDMPolygon, listNoDMZones } from "../Player/polygons";
import polygons from "../polygons";
import { getWantedCount } from "../Gui/wanteds";
import { spawnschutzEnabled } from "../Player/spawnschutz";


function isTargetInPolygon(pos): boolean {
    let found = false;

    listNoDMZones.forEach(zone => {
        if (polygons.isPositionWithinPolygon(pos, zone, zone.dimension)) {
            found = true;
            return;
        }
    });
    if (found) return true;
    
    return false;
}

export default function weapondamageUtil(globalData: IGlobalData) {
    let blockInput = false;

    mp.players.local.setSuffersCriticalHits(false);

    mp.players.forEachInStreamRange((player) => {
        player.setSuffersCriticalHits(false);
    });

    mp.events.add(RageEnums.EventKey.INCOMING_DAMAGE, (sourceEntity: EntityMp, sourcePlayer: PlayerMp, targetEntity: EntityMp, weapon: number, boneIndex: number, damage: number) => {
        if ((isTargetInPolygon(sourceEntity.position) || isInAnyNoDMPolygon) && getWantedCount() === 0) {
            return true;
        }

        if (globalData.IsAfk == true && getWantedCount() === 0) {
            return true;
        }

        if (spawnschutzEnabled && getWantedCount() === 0) {
            return true;
        }
    });

    mp.events.add(RageEnums.EventKey.OUTGOING_DAMAGE, (sourceEntity, targetEntity, sourcePlayer, weapon, boneIndex, damage) => {
        if (spawnschutzEnabled && getWantedCount() === 0) {
            return true;
        }
    });

    mp.events.add("render", () => {
        if (blockInput) {
            mp.game.controls.disableControlAction(32, 142, true);
            mp.game.controls.disableControlAction(32, 140, true);
            mp.game.controls.disableControlAction(32, 263, true);
        }
    });

    mp.events.add('SERVER:WeaponModifier', (player) => {
        //mp.gui.chat.push("Modifier steht jetzt auf" + modifier);
        //mp.players.local.setWeaponDamageModifier(modifier);
        let modifier = 1;
        let meelemodifier = 1;

        blockInput = (mp.game.weapon.getWeapontypeGroup(player.weapon) != 2685387236);

        switch (player.weapon) {
            case 0x1B06D571: //Pistol
                modifier = 0.4;
                meelemodifier = 1;
                //mp.gui.chat.push("PistolModifier: " + modifier);
                break;
            case 0xBFE256D4: //Pistol_MK2
                modifier = 0.4;
                meelemodifier = 1;
                break;
            case 0x5EF9FEC4: //Combatpistol
                modifier = 0.5;
                meelemodifier = 1;
                break;
            case 0x99AEEB3B: //Pistol50
                modifier = 0.35;
                meelemodifier = 1;
                break;
            case 0x2BE6766B: //SMG
                modifier = 0.25;
                meelemodifier = 1;
                break;
            case 0x0A3D4D34: //Combat PDW
                modifier = 0.185;
                meelemodifier = 1;
                break;
            case 0xBFEFFF6D: //assaultrifle
                modifier = 0.225;
                meelemodifier = 1;
                break;
            case 0x1D073A89: //Pumpshotgun
                modifier = 0.2;
                meelemodifier = 1;
                break;
            case 0x83BF0278: //Cabinerifle
                modifier = 0.2;
                meelemodifier = 1;
                break;
            case 0x624FE830: //Compactrifle
                modifier = 0.2;
                meelemodifier = 0.1;
                break;
            case 0x05FC3C11: //Sniperrifle
                modifier = 0.5;
                meelemodifier = 1;
                break;
            case 0xA2719263: //Meele Umarmed Fist
                meelemodifier = 0.1;
                modifier = 1;
                //mp.gui.chat.push("FistModifier: " + meelemodifier);
                break;
            case 0x958A4A8F: //Meele Baseball Bat
                meelemodifier = 0.15;
                modifier = 1;
                break;
            case 0x8BB05FD7: //Meele Flashlight
                meelemodifier = 0.1;
                modifier = 1;

                break;
            case 0xD8DF3C3C: //Meele Knuckle
                meelemodifier = 0.1;
                modifier = 1;

                //mp.gui.chat.push("KnuckleModifier: " + meelemodifier);
                break;
            case 0x678B81B1: //Meele Nightstick
                meelemodifier = 0.1;
                modifier = 1;

                break;
            case 0xEFE7E2DF: // Assault SMG
                meelemodifier = 1;
                modifier = 0.259

                break;
            default:
                modifier = 1;
                meelemodifier = 1;

                break;
        }
        mp.game.invoke("0xCE07B9F7817AADA3", player, modifier);
        mp.game.invoke("0x4A3DC7ECCC321032", player, meelemodifier);
        //mp.gui.chat.push("Modifier: " + modifier + "MeeleModifier: " +meelemodifier);
    });
    mp.events.add('SERVER:WeaponModifier2', (player, modifier, meelemodifier) => {
        //mp.gui.chat.push("Modifier steht jetzt auf" + modifier);
        //mp.players.local.setWeaponDamageModifier(modifier);
        mp.game.invoke("0xCE07B9F7817AADA3", player, modifier);
        mp.game.invoke("0x4A3DC7ECCC321032", player, meelemodifier);
        //mp.gui.chat.push("Modifier: " + modifier + "MeeleModifier: " + meelemodifier);
    });

    mp.events.add("entityStreamIn", (entity) => {
        if (entity.type === "player") {
            entity.setSuffersCriticalHits(false);
        }
    });

    /*mp.events.add('playerWeaponShot', (targetPosition, targetEntity) => {
for (var x in this.weaponAmmo) {
    if (this.weaponAmmo[x].id != this.currentWeapon) {
        continue;
    }
    this.weaponAmmo[x].ammo = this.weaponAmmo[x].ammo - 1;
}*/
}