using System;
using System.Reflection;

using DataLoader;
using Harmony;
using RobocraftX.Blocks;
using RobocraftX.Blocks.Ghost;
using RobocraftX.Blocks.Scaling;
using RobocraftX.Character;
using RobocraftX.CommandLine.Custom;
using RobocraftX.Common;
using RobocraftX.Common.Input;
using RobocraftX.Common.Utilities;
using RobocraftX.CR.MachineEditing;
using RobocraftX.StateSync;
using Svelto.ECS;
using Svelto.ECS.EntityStructs;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;
using uREPL;

using GamecraftModdingAPI.Utility;

namespace GamecraftModdingAPI.Blocks
{
    /// <summary>
    /// Engine which executes block placement actions
    /// </summary>
    public class PlacementEngine : IApiEngine
    {
        public bool IsInGame = false;

        public void Dispose()
        {
            IsInGame = false;
        }

        public void Ready()
        {
            IsInGame = true;
        }

        public IEntitiesDB entitiesDB { get; set; }
        internal static BlockEntityFactory _blockEntityFactory; //Injected from PlaceBlockEngine

        public void PlaceBlock(BlockIDs block, BlockColors color, byte darkness, float3 position, int uscale,
            float3 scale, uint playerId, float3 rotation)
        { //It appears that only the non-uniform scale has any visible effect, but if that's not given here it will be set to the uniform one
            if (darkness > 9)
                throw new Exception("That is too dark. Make sure to use 0-9 as darkness. (0 is default.)");
            BuildBlock((ushort) block, (byte) (color + darkness * 10), position, uscale, scale, rotation).Init(
                new BlockPlacementInfoStruct()
                {
                    loadedFromDisk = false,
                    placedBy = playerId
                });
        }

        private EntityStructInitializer BuildBlock(ushort block, byte color, float3 position, int uscale, float3 scale, float3 rot)
        {
            if (_blockEntityFactory == null)
                throw new Exception("The factory is null.");
            if (uscale < 1)
                throw new Exception("Scale needs to be at least 1");
            if (scale.x < 4e-5) scale.x = uscale;
            if (scale.y < 4e-5) scale.y = uscale;
            if (scale.z < 4e-5) scale.z = uscale;
            uint dbid = block;
            if (!PrefabsID.DBIDMAP.ContainsKey(dbid))
                throw new Exception("Block with ID " + dbid + " not found!");
            //RobocraftX.CR.MachineEditing.PlaceBlockEngine
            ScalingEntityStruct scaling = new ScalingEntityStruct {scale = scale};
            Quaternion rotQ = Quaternion.Euler(rot);
            RotationEntityStruct rotation = new RotationEntityStruct {rotation = rotQ};
            GridRotationStruct gridRotation = new GridRotationStruct
                {position = position, rotation = rotQ};
            CubeCategoryStruct category = new CubeCategoryStruct
                {category = CubeCategory.General, type = CubeType.Block};
            DBEntityStruct dbEntity = new DBEntityStruct {DBID = dbid};
            uint num = PrefabsID.DBIDMAP[dbid];
            GFXPrefabEntityStructGO gfx = new GFXPrefabEntityStructGO {prefabID = num};
            BlockPlacementScaleEntityStruct placementScale = new BlockPlacementScaleEntityStruct
            {
                blockPlacementHeight = uscale, blockPlacementWidth = uscale, desiredScaleFactor = uscale,
                snapGridScale = uscale,
                unitSnapOffset = 0, isUsingUnitSize = true
            };
            EquippedColourStruct colour = new EquippedColourStruct {indexInPalette = color};
            EGID egid2;
            switch (category.category)
            {
                case CubeCategory.SpawnPoint:
                case CubeCategory.BuildingSpawnPoint:
                    egid2 = MachineEditingGroups.NewSpawnPointBlockID;
                    break;
                default:
                    egid2 = MachineEditingGroups.NewBlockID;
                    break;
            }

            int cubeId = PrefabsID.GenerateDBID((ushort) category.category, block);
            EntityStructInitializer
                structInitializer =
                    _blockEntityFactory.Build(egid2, (uint) cubeId); //The ghost block index is only used for triggers
            if (colour.indexInPalette != byte.MaxValue)
                structInitializer.Init(new ColourParameterEntityStruct
                {
                    indexInPalette = colour.indexInPalette
                });
            structInitializer.Init(new GFXPrefabEntityStructGPUI(gfx.prefabID));
            structInitializer.Init(new PhysicsPrefabEntityStruct(gfx.prefabID));
            structInitializer.Init(dbEntity);
            structInitializer.Init(new PositionEntityStruct {position = position});
            structInitializer.Init(rotation);
            structInitializer.Init(scaling);
            structInitializer.Init(gridRotation);
            structInitializer.Init(new UniformBlockScaleEntityStruct
            {
                scaleFactor = placementScale.desiredScaleFactor
            });
            return structInitializer;
        }

        public string Name { get; } = "GamecraftModdingAPIPlacementGameEngine";

        [HarmonyPatch]
        public class FactoryObtainerPatch
        {
            static void Postfix(BlockEntityFactory blockEntityFactory)
            {
                _blockEntityFactory = blockEntityFactory;
                Logging.MetaDebugLog("Block entity factory injected.");
            }

            static MethodBase TargetMethod(HarmonyInstance instance)
            {
                return typeof(PlaceBlockEngine).GetConstructors()[0];
            }
        }
    }
}