using System.Collections.Generic;
using Intrepid.Gameplay.Journey;
using Intrepid.Gameplay.Prototyping.SaveStations;
using Intrepid.Utilities;
using Sirenix.OdinInspector;
using UnityEngine;

namespace Intrepid.Gameplay.Prototyping
{
    public class InteractableSaveStationModelPrototype : InteractableModelPrototype
    {
        [GroupInjections, SerializeField] private SaveStationDeploymentPoint saveStationDeploymentPoint;
        [GroupConfigurations, SerializeField, ReadOnly]  private int sizeX = 1;
        [GroupConfigurations, SerializeField, ReadOnly] private int sizeY = 1;
        [GroupConfigurations, SerializeField, ReadOnly] private int sizeZ = 1;
        [GroupConfigurations, SerializeField, ReadOnly] private float offset = 1f;
        
        [GroupDebug, SerializeField, Min(1)] private int x = 1;
        [GroupDebug, SerializeField, Min(1)] private int y = 1;
        [GroupDebug, SerializeField, Min(1)] private int z = 1;
        [GroupDebug, SerializeField, Range(0f, 1f)] private float contact_offset = 1f;
        [GroupDebug, SerializeField, ReadOnly] private StageManager stageManager;
        
        public Vector3 DeploymentPoint => saveStationDeploymentPoint.DeploymentPoint;
        public StageTag StageTag => stageManager?.StageTag;
        
        public override void SetStageManager(StageManager sm)
        {
            stageManager = sm;
        }

        protected override void ConfigElements()
        {
        }

        protected override void DebugResize()
        {
            sizeX = Mathf.Max(1, x);
            sizeY = Mathf.Max(1, y);
            sizeZ = Mathf.Max(1, z);
            offset = Mathf.Clamp01(contact_offset);
        }

        protected override void SetupVisibility()
        {
        }

        protected override void Generate()
        {
            var vertices = new List<Vector3>();
            var triangles = new List<int>();
            var uvs = new List<Vector2>();
            
            MeshUtils.AddCubeFaces(vertices, triangles, uvs,
                sizeX, sizeY, sizeZ,
                sizeX * 0.5f, sizeY * 0.5f, sizeZ * 0.5f);
            
            PrototypeModel.BuildMesh(new MeshData(
                vertices.ToArray(),
                triangles.ToArray(),
                uvs.ToArray()));
            
            PrototypeModel.BoxCollider.size = new Vector3(sizeX + offset, sizeY, sizeZ + offset);
            PrototypeModel.BoxCollider.center = Vector3.zero;
            PrototypeModel.transform.localPosition = new Vector3(0f, sizeY / 2f, 0f);
        }
    }
}