// priority: 0

EntityEvents.spawned(event => {
    let entity = event.entity
    if (!entity.type) return
    let type = entity.type
    let eData = entity.persistentData
    // 初始化检查
    if (eData.getBoolean("initialize")) return
    eData.putBoolean("initialize", true)
    // 数据库构建
    let soldierData = eData.getCompound("soldier")
    let rideData = eData.getCompound("ride")
    // 百年战争士兵
    if (type.toString().startsWith("hundred_years_war") && !type.toString().startsWith("hundred_years_war:mounted")) {
        if (entity.tags.contains("snk_minion")) return
        // 移速配平
        entity.setAttributeBaseValue("minecraft:generic.movement_speed", 0.30)

        // 随机分配性别 
        let gender = Math.random() < 0.66 ? "male" : "female"
        soldierData.putString("Gender", gender)
        if (gender === "male") {
            entity.mergeNbt({ Skin: Math.floor(Math.random() * 20) + 1 })
        } else {
            entity.mergeNbt({ Skin: Math.floor(Math.random() * 10) + 21 })
        }
        if (
            type === "hundred_years_war:militia" ||
            type === "hundred_years_war:spear_man"
        ) {
            rideData.putBoolean("replace", true)
            rideData.putString("rideID", "hundred_years_war:mounted_light_lancer_rider")
            let ChargeWeaponData = rideData.getCompound("ChargeWeapon")
            ChargeWeaponData.putBoolean("Count", true)
            ChargeWeaponData.putString("id", "hundred_years_war:iron_pike_charge")
            rideData.put("ChargeWeapon", ChargeWeaponData)
        }
        if (
            type === "hundred_years_war:warrior" ||
            type === "hundred_years_war:shieldman"
        ) {
            rideData.putBoolean("replace", true)
            rideData.putString("rideID", "hundred_years_war:mounted_lancer_rider")
            let ChargeWeaponData = rideData.getCompound("ChargeWeapon")
            ChargeWeaponData.putBoolean("Count", true)
            ChargeWeaponData.putString("id", "hundred_years_war:iron_lance")
            rideData.put("ChargeWeapon", ChargeWeaponData)
        }
        if (
            type === "hundred_years_war:archer" ||
            type === "hundred_years_war:crossbowman"
        ) {
            rideData.putBoolean("replace", true)
            rideData.putString("rideID", "hundred_years_war:mounted_archer_rider")
        }
        if (
            type === "hundred_years_war:handgonne_man" ||
            type === "hundred_years_war:matchlock_man"
        ) {
            rideData.putBoolean("replace", true)
            rideData.putString("rideID", "hundred_years_war:mounted_matchlock_man")
        }
        if (type === "hundred_years_war:priest") {
            rideData.putString("riding", "hundred_years_war:hyw_horse")
            if (gender === "female") {
                entity.mergeNbt({ Skin: Math.floor(Math.random() * 3) + 1 })
            } else {
                entity.mergeNbt({ Skin: Math.floor(Math.random() * 3) + 4 })
            }
        }
    }
    // 警卫村民
    if (type.toString().startsWith("guardvillagers")) {
        // 移速配平
        entity.setAttributeBaseValue("minecraft:generic.movement_speed", 0.62)
        // 设置固定性别和职业
        soldierData.putString("Gender", "male")
        soldierData.putString("Profession", "guard")
        soldierData.putString("ProfessionDisplay", "警卫")
        // 生成随机姓名
        let fullName = generateRandomName("male")
        soldierData.putString("FullName", fullName)
        // 设置显示名称
        let displayName = `[警卫]${fullName}`
        entity.setCustomName(Component.of(displayName))
        // 士兵数据库
        soldierData.putInt("HomePosX", entity.x)
        soldierData.putInt("HomePosY", entity.y)
        soldierData.putInt("HomePosZ", entity.z)
        soldierData.putString("HomePosDim", entity.level.dimension.path)
        soldierData.putInt("Occupation", 2)
        soldierData.putString("Type", "guard")
        rideData.putString("riding", "minecraft:donkey")

        // 随机装备分配
        let leftHand = leftHandItems[Math.floor(Math.random() * leftHandItems.length)]
        let rightHand = rightHandItems[Math.floor(Math.random() * rightHandItems.length)]
        let armorSet = armorSets[Math.floor(Math.random() * armorSets.length)]
        entity.setMainHandItem(Item.of(rightHand))
        entity.setOffHandItem(Item.of(leftHand))
        entity.setHeadArmorItem(Item.of(armorSet.helmet))
        entity.setChestArmorItem(Item.of(armorSet.chestplate))
        entity.setLegsArmorItem(Item.of(armorSet.leggings))
        entity.setFeetArmorItem(Item.of(armorSet.boots))
    }
    // 数据储存
    eData.put("soldier", soldierData)
    eData.put("ride", rideData)
})

// 随机姓名生成函数（仅用于警卫村民）
function generateRandomName(gender) {
    let randomLastName = familyName[Math.floor(Math.random() * familyName.length)]
    let randomFirstName

    if (gender === "male") {
        randomFirstName = maleName[Math.floor(Math.random() * maleName.length)]
    } else {
        randomFirstName = femaleName[Math.floor(Math.random() * femaleName.length)]
    }

    if (familyNameFront) {
        return `${randomLastName}${randomFirstName}`
    } else {
        return `${randomFirstName} ${randomLastName}`
    }
}