using System.Collections;
using System.Collections.Generic;
using Godot;
using System;

using System.Threading.Tasks;

namespace Rokojori
{
  public class MultiBakeModeCrossBraces:MultiBakeModeBillboardBase
  {
    public override MultiBaker.BakeMode GetBakeMode()
    {
      return MultiBaker.BakeMode.Cross_Braces;
    }

    public override int GetNumViews()
    {
      var views = multiBaker.crossAngles * 2;

      if ( multiBaker.crossBottom )
      {
        views ++;
      }

      if ( multiBaker.crossTop )
      {
        views ++;
      }

      return views;
    }

    public override void CreateBakes()
    {
      var fov = multiBaker.GetCameraFOV();
      var distance = multiBaker.GetCameraDistance();
      var outputScale = multiBaker.GetOutputScale();

      var _bakers = multiBaker.bakers;
      var mb = multiBaker;

      
      _bakers.ForEach(
        b =>
        {
          b.useCustomFOV = true;
          b.customFOV = fov;

          b.useCustomDistance = true;
          b.customDistance = distance;

          b.rotationMode = Baker.RotationMode.Yaw_Pitch;
        }
      );

      var index = 0;
      var mg = new MeshGeometry();  

      var numTextures = GetNumViews();
      var textureAlignment = TextureMerger.ComputeTextureAlignment( numTextures );
 

      if ( mb.crossTop )
      {
        _bakers[ index ].yaw = 0 + mb.crossAngleOffset;
        _bakers[ index ].pitch = 90f;        

        var uv = TextureMerger.GetUVRectangle( textureAlignment, index, true );

        var q = new MeshGeometry();        
        q.AddQuad( _bakers[ index ].bakingRotation, outputScale, uv.min, uv.max );  

        mg.Add( q );

        index ++;
      }

      if ( mb.crossBottom )
      {
        _bakers[ index ].yaw = 0 + mb.crossAngleOffset;
        _bakers[ index ].pitch = -90f;

        var uv = TextureMerger.GetUVRectangle( textureAlignment, index, true );
        var q = new MeshGeometry();
        q.AddQuad( _bakers[ index ].bakingRotation, outputScale, uv.min, uv.max );  

        mg.Add( q );

        index ++;
      }

      
      for ( int i = 0; i < mb.crossAngles; i++ )
      {
        for ( int side = 0; side < 2; side ++ )
        {
          var angle = ( 180f * i ) / (float) mb.crossAngles;

          _bakers[ index ].yaw = side == 0 ? angle : ( MathX.Repeat( angle + 180f, 360f ) );
          _bakers[ index ].yaw += mb.crossAngleOffset;
          _bakers[ index ].pitch = 0;

          var uv = TextureMerger.GetUVRectangle( textureAlignment, index, true );          

          var q = new MeshGeometry(); 
          q.AddQuad( _bakers[ index ].bakingRotation, outputScale, uv.min, uv.max );  

          if ( mb.crossBraces > 1 )
          {
            var normal = Vector3.Forward * _bakers[ index ].bakingRotation;
            var startOffset = normal * mb.crossSpreadDistance * 0.5f;
            var endOffset = -startOffset;
            
            for ( int brace = 0; brace < mb.crossBraces; brace ++ )
            {
              var bq = q.Clone();
              var lerpAmount = (float)brace / (float)( mb.crossBraces - 1 );
              var offset = startOffset.Lerp( endOffset, lerpAmount );
              bq.ApplyTranslation( offset );
              mg.Add( bq );
            }
          }
          else
          {
            mg.Add( q );
          }


          index ++;
          
        }



      }

      
      mb.X_outputMesh.Mesh = mg.GenerateMesh();  
    }
  }
}