import * as mc from "@minecraft/server"
import * as racoAPI from "../raco-API"
import { hasRedstonePower } from "../hasRedstonePower";

export function precisionHopperTick(block){
    const on = hasRedstonePower(block, ['north', 'south', 'west', 'east', 'below'], true) ?? false;
    const entity = block.dimension.getEntities({type:'vtng_rt:precision_hopper_entity', location:block.center(), maxDistance:0.5, closest:1})[0]
    if (entity){
        const container = entity?.getComponent("inventory")?.container
        if (container){
            let filterItems = []
            for (let i = 0; i < container.size; i++){
                const thisItem = container.getItem(i)
                if (thisItem){
                    filterItems.push(thisItem?.typeId)
                }
            }
            {
                const face = {
                    'up':'below',
                    'down':'below',
                    'north':'south',
                    'south':'north',
                    'west':'east',
                    'east':'west'
                }
                const direction = block.permutation.getState('minecraft:block_face')
                const containerConnected = block[face[direction]]()?.getComponent("inventory")?.container
                if (containerConnected){
                    let isAvailable = false
                    const containerAbove = block.above()?.getComponent("inventory")?.container
                    if (containerAbove){
                        for (let o = 0; o < containerAbove.size; o++){
                            const theItem = containerAbove.getItem(o)
                            if (theItem && ((!on && (filterItems.includes(theItem?.typeId) || filterItems.length == 0))||(on && (!filterItems.includes(theItem?.typeId) && filterItems.length > 0)))){
                                
                                if (containerConnected.emptySlotsCount > 0) isAvailable = true
                                if (!isAvailable){
                                    for (let i = 0; i < containerConnected.size; i++){
                                        const thisItem = containerConnected.getItem(i)
                                        if (thisItem?.typeId == theItem?.typeId && thisItem.amount + theItem?.amount <= thisItem?.maxAmount){
                                            isAvailable = true
                                        }
                                    }
                                }
                                if (isAvailable){
                                    racoAPI.transferItem(containerAbove, containerConnected, o, 1)
                                }
                            }
                        }
                    } else {
                        const itemsAbove = block.dimension.getEntities({type:"minecraft:item",  location:block.above().center(), maxDistance:0.6})
                        for (const theItem of itemsAbove){
                            const itemStack = theItem.getComponent("minecraft:item").itemStack
                            if (theItem && itemStack && ((!on && (filterItems.includes(itemStack?.typeId) || filterItems.length == 0))||(on && (!filterItems.includes(itemStack?.typeId) && filterItems.length > 0)))){
                                if (containerConnected.emptySlotsCount > 0) isAvailable = true
                                if (!isAvailable){
                                    for (let i = 0; i < containerConnected.size; i++){
                                        const thisItem = containerConnected.getItem(i)
                                        if (thisItem?.typeId == itemStack?.typeId && thisItem.amount + itemStack?.amount <= thisItem?.maxAmount){
                                            isAvailable = true
                                        }
                                    }
                                }
                                if (isAvailable){
                                    if (itemStack.amount > 1){
                                        const newItem = itemStack
                                        itemStack.amount--
                                        const summonItem = theItem.dimension.spawnItem(itemStack, theItem.location)
                                        summonItem.teleport(theItem.location)
                                        theItem.remove()
                                        newItem.amount = 1
                                        containerConnected?.addItem(newItem)
                                    } else if (itemStack.amount == 1) {
                                        containerConnected?.addItem(itemStack)
                                        theItem.remove()
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (on){
    }
    if (on != block.permutation.getState('vtng_rt:on')) racoAPI.setPermutation(block, 'vtng_rt:on', on)
}
mc.world.afterEvents.playerPlaceBlock.subscribe((data)=>{
    const player = data.player
    const block = data.block
    if (block?.typeId == 'vtng_rt:precision_hopper'){
        block.dimension.spawnEntity('vtng_rt:precision_hopper_entity', block.center()).nameTag = `tile.vtng_rt:precision_hopper.name`
    }
})