import * as mc from "@minecraft/server"
import * as racoAPI from "../raco-API"
export function interactWithBlock(entity, slot, block){
    if (entity){
        const container = entity.getComponent("inventory").container
        {
            if (block){
                if (block.typeId == 'minecraft:daylight_detector'){
                    block.dimension.runCommand(`setblock ${block.x} ${block.y} ${block.z} daylight_detector_inverted`)
                } else
                if (block.typeId == 'minecraft:daylight_detector_inverted'){
                    block.dimension.runCommand(`setblock ${block.x} ${block.y} ${block.z} daylight_detector`)
                } else
                if (block.typeId.includes('_repeater')){
                    const delay = block.permutation.getState("repeater_delay")
                    if (delay == 0) racoAPI.setPermutation(block, 'repeater_delay', 1); else
                    if (delay == 1) racoAPI.setPermutation(block, 'repeater_delay', 2); else
                    if (delay == 2) racoAPI.setPermutation(block, 'repeater_delay', 3); else
                    if (delay == 3) racoAPI.setPermutation(block, 'repeater_delay', 0);
                } else
                if (block.typeId == 'minecraft:lever'){
                    const delay = block.permutation.getState("open_bit")
                    const rotation = block.permutation.getState("lever_direction")
                    if (delay == false) block.dimension.runCommand(`setblock ${block.x} ${block.y} ${block.z} lever ["lever_direction"="${rotation}", "open_bit"=true]`); else
                    if (delay == true) block.dimension.runCommand(`setblock ${block.x} ${block.y} ${block.z} lever ["lever_direction"="${rotation}", "open_bit"=false]`)
                } else
                if (block.typeId.includes('_door')){
                    const delay = block.permutation.getState("open_bit")
                    if (delay == false) racoAPI.setPermutation(block, 'open_bit', true); else
                    if (delay == true) racoAPI.setPermutation(block, 'open_bit', false)
                } else
                if (block.typeId.includes('trapdoor')){
                    const delay = block.permutation.getState("open_bit")
                    if (delay == false) racoAPI.setPermutation(block, 'open_bit', true); else
                    if (delay == true) racoAPI.setPermutation(block, 'open_bit', false)
                } else
                if (block.typeId.includes('fence_gate')){
                    const delay = block.permutation.getState("open_bit")
                    if (delay == false) racoAPI.setPermutation(block, 'open_bit', true); else
                    if (delay == true) racoAPI.setPermutation(block, 'open_bit', false)
                } else
                if (block.typeId.includes('candle')){
                    const delay = block.permutation.getState("lit")
                    if (delay == true) racoAPI.setPermutation(block, 'lit', false), block.dimension.playSound('extinguish.candle', block.center())
                }
            }
        }
         if (container && container?.getItem(slot)){
            const item = container?.getItem(slot)
            if (item){ 
                if (item.typeId == 'minecraft:bone_meal'){
                  const plants = [
                    "minecraft:wheat",
                    "minecraft:carrots",
                    "minecraft:potatoes",
                    "minecraft:beetroot"
                  ]
                  if (block && plants.includes(block?.typeId)){
                    if (block.permutation.getState('growth') < 7) {
                        entity.getComponent('inventory')?.container?.setItem(slot, )
                        racoAPI.setPermutation(block, 'growth', block.permutation.getState('growth')+1)
                        entity.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
                    }
                  } else 
                  if (block && block.typeId.includes('sapling')){
                    entity.getComponent('inventory')?.container?.setItem(slot, )
                    entity.runCommand(`replaceitem entity @s slot.weapon.mainhand 0 air`)
                    try {
                      entity.runCommandAsync(`fill ${block.x} ${block.y} ${block.z} ${block.x} ${block.y+5} ${block.z} ${block.getItemStack().typeId.split(":")[1].replace("_sapling", "")}_log [] replace air`)
                      entity.runCommandAsync(`setblock ${block.x} ${block.y} ${block.z} ${block.getItemStack().typeId.split(":")[1].replace("_sapling", "")}_log`)
                    }catch (error) {
                      entity.runCommandAsync(`fill ${block.x} ${block.y} ${block.z} ${block.x} ${block.y+5} ${block.z} ${block.getItemStack().typeId.split(":")[1].replace("_sapling", "")}_wood [] replace air`)
                      entity.runCommandAsync(`setblock ${block.x} ${block.y} ${block.z} ${block.getItemStack().typeId.split(":")[1].replace("_sapling", "")}_wood`)
                    }
                    entity.runCommandAsync(`fill ${block.x-2} ${block.y+3} ${block.z-2} ${block.x+2} ${block.y+4} ${block.z+2} ${block.typeId.split(":")[1].replace("_sapling", "")}_leaves replace air`)
                    entity.runCommandAsync(`fill ${block.x-1} ${block.y+5} ${block.z-1} ${block.x+1} ${block.y+6} ${block.z+1} ${block.typeId.split(":")[1].replace("_sapling", "")}_leaves replace air`)
                    entity.runCommandAsync(`fill ${block.x-1} ${block.y+6} ${block.z-1} ${block.x-1} ${block.y+6} ${block.z-1} air [] replace ${block.typeId.split(":")[1].replace("_sapling", "")}_leaves`)
                    entity.runCommandAsync(`fill ${block.x-1} ${block.y+6} ${block.z+1} ${block.x-1} ${block.y+6} ${block.z+1} air [] replace ${block.typeId.split(":")[1].replace("_sapling", "")}_leaves`)
                    entity.runCommandAsync(`fill ${block.x+1} ${block.y+6} ${block.z-1} ${block.x+1} ${block.y+6} ${block.z-1} air [] replace ${block.typeId.split(":")[1].replace("_sapling", "")}_leaves`)
                    entity.runCommandAsync(`fill ${block.x+1} ${block.y+6} ${block.z+1} ${block.x+1} ${block.y+6} ${block.z+1} air [] replace ${block.typeId.split(":")[1].replace("_sapling", "")}_leaves`)
                    entity.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
                  }
                } else
                if (item.typeId == 'minecraft:flint_and_steel'){
                    if (block.typeId.includes('candle')){
                        const delay = block.permutation.getState("lit")
                        if (delay == false) {
                            racoAPI.setPermutation(block, 'lit', true)
                            block.dimension.playSound('fire.ignite', block.center())
                            container.setItem(slot, racoAPI.applyDurability(item, 1))
                        }
                    }
                    if (block.typeId.includes('campfire')){
                        const delay = block.permutation.getState("extinguished")
                        if (delay == true) {
                            racoAPI.setPermutation(block, 'extinguished', false)
                            block.dimension.playSound('fire.ignite', block.center())
                            container.setItem(slot, racoAPI.applyDurability(item, 1))
                        }
                    }
                } else 
                if (item.typeId.includes('_shovel')){
                    if (block.typeId.includes('campfire')){
                        const delay = block.permutation.getState("extinguished")
                        if (delay == false) {
                            racoAPI.setPermutation(block, 'extinguished', true)
                            container.setItem(slot, racoAPI.applyDurability(item, 1))
                        }
                    } else 
                    if (block.typeId == 'minecraft:dirt' || block.typeId == 'minecraft:grass_block'){
                        block.dimension.runCommand(`setblock ${block.x} ${block.y} ${block.z} grass_path`)
                    }
                } else 
                if (item.typeId.includes('_hoe')){
                    if (block.typeId == 'minecraft:dirt' || block.typeId == 'minecraft:grass_block'){
                        block.dimension.runCommand(`setblock ${block.x} ${block.y} ${block.z} farmland`)
                    }
                } else 
                if (item.typeId.includes('_axe')){
                    if (block.typeId.includes('_log') && !block.typeId.includes('stripped_')){
                        block.dimension.runCommand(`setblock ${block.x} ${block.y} ${block.z} ${block.typeId.replace('minecraft:', 'minecraft:stripped_')} ["pillar_axis"="${block.permutation.getState('pillar_axis')}"]`)
                    }
                } else 
                if (item.typeId == 'minecraft:water_bucket'){
                    if (block?.isAir){
                        block.dimension.runCommand(`setblock ${block.x} ${block.y} ${block.z} flowing_water`)
                    }
                }
            }
        }
    }
}

mc.system.afterEvents.scriptEventReceive.subscribe((data)=>{
    const entity = data.sourceEntity
    const id = data.id
    if (id == 'rc_fb:fds'){
        const block = entity.dimension.getBlock(entity.location)
        //console.warn(block.typeId)
        interactWithBlock(entity, 0, block)
    }
})