import { world, system, EntityDamageCause, EquipmentSlot} from "@minecraft/server";
import { launchPlayer } from "../../util/movements.js";
import { weaponDamageIndicator, getAmmoItem, getAmmoCount} from "../../util/utils.js";
import { spawnProjectile, attackPlayerMeikyu } from "../../util/attackMethods.js";
import {JsonDatabase} from "../../util/con-database.js";

let AmmoBody = new JsonDatabase("ammo_pistol")


function reloaded(player,ticks,ammo,item) {
    let al = system.runTimeout(()=>{
        let count = 0
        let enough = getAmmoCount(player,ammo)
        if(enough > 0) {
            let itemAmmo = getAmmoItem(player,ammo)
            let ammoTypers = itemAmmo.getDynamicProperty("ammo")
            item.setDynamicProperty("ammo", ammoTypers)
            player.runCommand(`clear @s ${ammo} 0 1`)
            player.getComponent("minecraft:equippable").setEquipment('Mainhand', item)
        } else {
            player.onScreenDisplay.setActionBar(`No Container Found`)
        }
        player.setDynamicProperty("attacking",0)
        world.scoreboard.getObjective("combatCooldown").setScore(player, 10)
        world.scoreboard.getObjective("combatCheck").setScore(player, 0)
        player.runCommandAsync("tag @s remove attack")
    },ticks)
}

const animations =[
    [
        {
            animation: "minced.hand_cannon.attack_sprint",
            knockback: [2,0],
            Player_Knockback: [2,0],
            type: "slash",
            attack_speed: 10,
            hit_number: 1,
            stun_type: "long",
            range: 3,
            distance: 2,
            targetNum: 1,
            damage_multiplier: 1,
            stun_damage_multiplier: 1,
            next_attack_time: 5,
            Unbreakable: false
        },
        {
            animation: "minced.pistol.attack0",
            knockback: [0,0.25],
            Player_Knockback: [1,0.25],
            type: "blunt",
            attack_speed: 6,
            hit_number: 1,
            stun_type: "long",
            range: 3,
            distance: 2,
            targetNum: 1,
            damage_multiplier: 1,
            stun_damage_multiplier: 1,
            next_attack_time: 5,
            Unbreakable: false
        },
        {
            animation: "minced.pistol.attack1",
            knockback: [3,0.25],
            Player_Knockback: [1,0.25],
            type: "blunt",
            attack_speed: 6,
            hit_number: 1,
            stun_type: "long",
            range: 3,
            distance: 2,
            targetNum: 1,
            damage_multiplier: 1,
            stun_damage_multiplier: 1,
            next_attack_time: 5,
            Unbreakable: false
        }
    ]
]

const weapon = {
    name: 'pistol',
    display_name: "Pistol",
    ammount: 20,
    dual_wield: ["pistol","sword","longsword","rapier","shield","greatsword","hand_cannon","katana","wakizashi","odachi"],
    type: "shooter",
    can_block: false,
    bulletTypes: ["bn","bf","bp"],
    block_damage: 33,
    front_dodge: [3, 0.25],
    block_time: 6,
    back_dodge:[-3, 0.25],
    left_dodge:[3, 0.2],
    right_dodge:[3, 0.2],
    clash_power: 10,
    stylePrefixes: ["minced.pistol"],
    weaponSize: "small",
    available_moves: ["front_dodge","back_dodge"],
    cd: 20,
    execute(player, weapon, effect, mode, burn) {
        const blckdmg = this.block_damage
        const cd = this.cd
        let atkspd
        let animChecker
        for(let i = (animations[0].length - 1); i > 0; i--) {
            if(player.getDynamicProperty("combo") % i == 0) {
                animChecker = i
                break;
            }
        }
        let attackEffect = []
        attackEffect.push(effect)
        let amm = JSON.parse(weapon.getDynamicProperty("ammo"))
        let AmmCoun = amm?.length
        console.warn(AmmCoun)
        let lavander = weaponDamageIndicator(weapon)
        
        switch(mode) {
            default:
                switch(burn) {
                    case "": {
                        console.warn("player")
                        if(AmmCoun > 0) {
                            console.warn("play")
                            player.playAnimation(`animation.minced.pistol.attack_shoot_onehand`, 
                            {
                                blendOutTime: 0.1,
                                stopExpression: "query.all_animations_finished",
                                nextState: `animation.minced.pistol.combat_stance`
                            })
                            system.runTimeout(() =>{
                                let AmmoType = amm[AmmCoun - 1]
                                amm.pop()
                                weapon.setDynamicProperty("ammo", JSON.stringify(amm))
                                player.getComponent("minecraft:equippable").setEquipment('Mainhand', weapon)
                                spawnProjectile(player, `minced:bullet<${AmmoType}>`, 1, 1, 10, "normal", "")
                                launchPlayer(player, -1, 0)
                            },3);
                            system.runTimeout(() =>{
                                player.setDynamicProperty("attacking",0)
                                world.scoreboard.getObjective("combatCooldown").setScore(player, 10)
                                world.scoreboard.getObjective("combatCheck").setScore(player, 0)
                                player.runCommandAsync("tag @s remove attack")
                            },6);
                            
                        } else if(AmmCoun == 0){
                            reloaded(player,10,"pixel:pellet_container", weapon)
                        }
                        
                    } break;
                    default: {
                        let stundamage = 10
                        let propCheck = animations[0][animChecker]
                        let knockback = propCheck.knockback
                        player.playAnimation(`animation.${propCheck.animation}`)
                        atkspd = propCheck.attack_speed
                        let Pknockback = propCheck.Player_Knockback
                        launchPlayer(player, Pknockback[0], Pknockback[1])
                        let effects = propCheck.type
                        let stun = propCheck.stun_type
                        let ranger = propCheck.range
                        let distil = propCheck.distance
                        let tgtnm = propCheck.targetNum
                        let NAT = propCheck.next_attack_time
                        let breakable = propCheck.Unbreakable
                        let hitCount = propCheck.hit_number
                        if(animChecker == 3) {
                            player.addEffect("slow_falling", 16, { amplifier: 3, showParticles: false })
                        }
                        attackPlayerMeikyu(player, ranger, (lavander * propCheck.damage_multiplier), blckdmg, cd, atkspd, distil, tgtnm, "line_target", attackEffect, knockback, stun, "", effects, (animations[0].length - 1), stundamage, NAT, breakable, "normal",weapon, hitCount, false)
                    }
                }
            
            break;
        }
        console.warn(AmmCoun)
    },
    setCore(player, item) {
        let Abilities = [
            {
              Id: "S2",
            },
            {
              Id: "A2",
            },
            {
              Id: "P3",
            },
            {
              Id: "R1",
            },
            {
              Id: "O0",
            }
        ]
        item.setDynamicProperty("abilities", JSON.stringify(Abilities))
        item.setDynamicProperty("ammo", JSON.stringify([]))
        item.setDynamicProperty("fightStyle", 1)
        player.getComponent('equippable')?.setEquipment(EquipmentSlot.Mainhand, item)
    },
    getCurrentAtkspd(player) {
        let animChecker
        for(let i = (animations[0].length - 1); i > 0; i--) {
            if(player.getDynamicProperty("combo") % i == 0) {
                animChecker = i
                break;
            }
        }
        return animations[0][animChecker].attack_speed
    } 
}

export default weapon


