import ../scene/[scene_object
                        , scene_utils
                        , scene_record
                        , main_scenes
                        ]

, ../vk/[ vulkan
        , vkTypes
        , vulkan_record 
        ]

, ../drawable/[ shape_object
              , text
              ]


, ../wain/keyboard
, glm

import ../drawable/text as mText

import ../drawable/shape_object

type
 MenuState = enum Begin, Exit

var 
 ms = MenuState.Begin

#TODO proper dynamic resolution scaling of UI elements
proc load*( rec: var Vulkan_Record
          , title: var Scene
          , scene_record: var Scene_Record
          ) =

 #[ title.add rec.anSDFText(title, "title", str = "Shapes", size = 18) 
 title.add rec.anSDFText(title, "begin", str = "begin", size = 14) 
 title.add rec.anSDFText(title, "exit", str = "exit", size = 14) 
 title.add rec.a_shape( title.render_pass
                     , title.current_entity_id
                     , 4
                     , "settingsFrame"
                     , hollow = true
                     , theSize = 100
                     )
 
 title.the_text("title").move_to( 150
                              , 40
                              )
 
 title.the_text("begin").move_to( 900
                              , 70
                              )
 
 title.the_text("exit").move_to( 1600
                             , 70
                             )
 
 title.the_text("title").updateFS( outlined = 0.0)
  
 echo rec.swapchain.current_extent.width
 title.the_shape("settingsFrame").move_to( rec.swapchain.current_extent.width.float32 / 2
                                       ,  ((rec.swapchain.current_extent.height.int / 2) + 100 ).float32
                                       
                                       )
 
 title.the_shape("settingsFrame").scale( 8
                                      , 4
                                      ) ]#

 #$title.the_shape("settingsFrame").ubo.model.scale(50)
 scene_record.add_scene( addr title
                       , "title"
                       )
 
#TODO: switching states is kinda buggy
proc input*( rec: var Vulkan_Record
          , sr: var Scene_Record
          ) = 
 
 if WAIN_Key.Up.is_down or
    WAIN_Key.Right.is_down:
  case ms
  of MenuState.Begin: inc ms
  of MenuState.Exit: ms = MenuState.Begin

 if WAIN_Key.Down.is_down or
    WAIN_Key.Left.is_down:
  case ms
  of MenuState.Begin: ms = MenuState.Exit
  of MenuState.Exit: dec ms 
 
 if WAIN_Key.Enter.is_down:
  case ms
  #of MenuState.Begin: sr.the_main_scene = Main_Scene_Enum.Intro
  of MenuState.Exit: quit()
  else: discard 
  
proc update*( rec: var Vulkan_Record
            , menu: var Scene
            ) = discard
 #[ case ms 
 of MenuState.Begin: 
  menu.the_text("begin").updateFS( outlined = 0.1
                                )
  menu.the_text("exit").updateFS( outlined = 0.0)
 of MenuState.Exit: 
  menu.the_text("begin").updateFS( outlined = 0.0)
  menu.the_text("exit").updateFS( outlined = 0.1) ]#
  
proc build_scene*( rec: var Vulkan_Record
          , scene: var Scene
          ) = 

 var
  cmdBufInfo: VkCommandBufferBeginInfo
  scissor: VkRect2D
  
  clearValues: array[2,VkClearValue]
  renderPassBeginInfo: VkRenderPassBeginInfo
  
 scissor.extent.width = uint32 rec.swapchain.current_extent.width
 scissor.extent.height = uint32 rec.swapchain.current_extent.height
 scissor.offset.x = 0
 scissor.offset.y = 0
 cmdBufInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO
 cmdBufInfo.pNext = nil

 clearValues[0].color = VkClearColorValue(float32: [0f, 0f, 0f, 1f])
 clearValues[1].depth_stencil = VkClearDepthStencilValue(depth: 1.0f, stencil: 0 )
 
 renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
 renderPassBeginInfo.pNext = nil
 renderPassBeginInfo.renderArea.offset.x = 0
 renderPassBeginInfo.renderArea.offset.y = 0
 renderPassBeginInfo.renderArea.extent.width = uint32 rec.swapchain.current_extent.width
 renderPassBeginInfo.renderArea.extent.height = uint32 rec.swapchain.current_extent.height
 renderPassBeginInfo.clearValueCount = 2
 renderPassBeginInfo.pClearValues = addr clearValues[0]
 #renderPassBeginInfo.render_pass = scene.render_pass
 
 renderPassBeginInfo.framebuffer = rec.frame_buffers[rec.currentFrameBuffer]
 
 discard vkBeginCommandBuffer(rec.draw_command_buffers[rec.currentFrameBuffer], addr cmdBufInfo)
 
 vkCmdBeginRenderPass(rec.draw_command_buffers[rec.currentFrameBuffer], addr renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE)
 vkCmdSetViewport(rec.draw_command_buffers[rec.currentFrameBuffer], 0, 1, addr rec.current_viewport)
 vkCmdSetScissor(rec.draw_command_buffers[rec.currentFrameBuffer], 0, 1, addr scissor)
 
 #[ for shape in scene.shapes.mitems:
  rec.buildCommandBuffers shape, scene.render_pass, int rec.currentFrameBuffer
 
 for text in scene.texts.mitems: 
  mtext.buildCommandBuffers rec, text, scene.render_pass, int rec.currentFrameBuffer ]#
 
 vkCmdEndRenderPass(rec.draw_command_buffers[rec.currentFrameBuffer])
 discard vkEndCommandBuffer(rec.draw_command_buffers[rec.currentFrameBuffer])
 
 rec.readyFrame = true