package net.pietru.omni_power.blockevents;

import com.badlogic.gdx.utils.Array;
import finalforeach.cosmicreach.blockevents.BlockEventArgs;
import finalforeach.cosmicreach.blockevents.BlockEventTrigger;
import finalforeach.cosmicreach.blockevents.actions.ActionId;
import finalforeach.cosmicreach.blockevents.actions.IBlockAction;
import finalforeach.cosmicreach.blocks.BlockPosition;
import finalforeach.cosmicreach.blocks.BlockState;
import finalforeach.cosmicreach.world.BlockSetter;
import finalforeach.cosmicreach.world.Zone;
import net.pietru.omni_power.OmniPower;

import java.util.HashMap;

@ActionId(id = OmniPower.MOD_ID+":connect")
public class Connect implements IBlockAction {

    public String update_triggerId;
    public String cable_group;
    public boolean self_only = true;
    public boolean self_run = true;
    String center_name = "center";
    String xp_name = "xp_power";
    String xm_name = "xm_power";
    String yp_name = "yp_power";
    String ym_name = "ym_power";
    String zp_name = "zp_power";
    String zm_name = "zm_power";

    @Override
    public void act(BlockEventArgs args) {
        Zone zone = args.zone;
        BlockPosition sourcePos = args.blockPos;
        String faces_arg = "";

        Array<BlockPosition> arr = new Array<>();
        BlockPosition side_pos;
        side_pos =  sourcePos.getOffsetBlockPos(zone, 1, 0, 0);
        if (block_has_dir_or_all(side_pos)) {
            arr.add(side_pos);
            faces_arg+=xp_name;
        }
        side_pos =  sourcePos.getOffsetBlockPos(zone, -1, 0, 0);
        if (block_has_dir_or_all(side_pos)) {
            arr.add(side_pos);
            faces_arg+=xm_name;
        }

        side_pos =  sourcePos.getOffsetBlockPos(zone, 0, 1, 0);
        if (block_has_dir_or_all(side_pos)) {
            arr.add(side_pos);
            faces_arg+=yp_name;
        }
        side_pos =  sourcePos.getOffsetBlockPos(zone, 0, -1, 0);
        if (block_has_dir_or_all(side_pos)) {
            arr.add(side_pos);
            faces_arg+=ym_name;
        }

        side_pos =  sourcePos.getOffsetBlockPos(zone, 0, 0, 1);
        if (block_has_dir_or_all(side_pos)) {
            arr.add(side_pos);
            faces_arg+=zp_name;
        }
        side_pos =  sourcePos.getOffsetBlockPos(zone, 0, 0, -1);
        if (block_has_dir_or_all(side_pos)) {
            arr.add(side_pos);
            faces_arg+=zm_name;
        }

        if (faces_arg.isEmpty())
            faces_arg=center_name;

        if (self_run) {
            HashMap<String, String> params = new HashMap<>();
            params.put("faces", faces_arg);
            BlockState blockState = sourcePos.getBlockState();
            blockState = blockState.getVariantWithParams(params);
            BlockSetter.get().replaceBlock(zone, blockState, sourcePos);
        }

        if (!self_only)
            arr.forEach(pos -> runTrigger(update_triggerId,pos,args));
    }

    private void runTrigger(String triggerId, BlockPosition triggerPos, BlockEventArgs args) {

        Zone zone = args.zone;
        if (triggerPos != null) {
            BlockState targetBlockState = triggerPos.getBlockState();
            if (targetBlockState == null) {
                return;
            }

            BlockEventTrigger[] customTrigger = targetBlockState.getTrigger(triggerId);
            if (customTrigger == null) {
                return;
            }

            BlockEventArgs newArgs = new BlockEventArgs();
            newArgs.blockPos = triggerPos;
            newArgs.zone = zone;
            newArgs.srcBlockState = targetBlockState;
            newArgs.setSrcIdentity(args.getSrcIdentity());

            for (BlockEventTrigger t : customTrigger) {
                t.act(newArgs);
            }
        }
    }

    public Boolean block_has_dir_or_all(BlockPosition pos){
        if (pos==null)
            return false;
        BlockState state = pos.getBlockState();
        if (state==null)
            return false;
        if (state.stringId==null)
            return false;
        return state.stringId.contains(cable_group);
    }
}
