// header includes
#include "base/base_inc.h"
#include "os/os_inc.h"
#include "render/render_inc.h"
#include "font/font_inc.h"
#include "draw/draw_inc.h"
#include "ui/ui_inc.h"

// .c includes
#include "base/base_inc.c"
#include "os/os_inc.c"
#include "os/os_entry_point.c"
#include "render/render_inc.c"
#include "font/font_inc.c"
#include "draw/draw_inc.c"
#include "ui/ui_inc.c"

#define APP_WINDOW_WIDTH 1280
#define APP_WINDOW_HEIGHT 720


#define OS_REFRESH_RATE 60.0f
#define FRAME_DT 1.0f/OS_REFRESH_RATE

void EntryPoint()
{
  
  OS_InitReceipt os_receipt = OS_init();
  OS_InitGfxReceipt os_gfx_receipt = OS_gfx_init(os_receipt);
  //unused_variable(os_gfx_receipt);
  R_InitReceipt r_init_receipt = R_init(os_receipt, os_gfx_receipt);
  F_InitReceipt f_init_receipt = F_init();
  D_InitReceipt d_init_receipt = D_init(r_init_receipt, f_init_receipt);
  unused_variable(d_init_receipt);
  
  UI_State *ui = UI_state_alloc();
  UI_state_set(ui);
  
  OS_Handle window_handle = OS_window_open(0, vec2_S64(APP_WINDOW_WIDTH, APP_WINDOW_HEIGHT), str8_lit("ello"));
  R_Handle window_r = R_window_equip(window_handle);
  
  //break_debugger();
  //~ Main loop
  MSG msg = {0};
  U64 frame_idx = 0;
  for (B32 quit = 0; quit == 0;)
  {
    ArenaTemp scratch = scratch_get(0, 0);
    OS_EventList events = OS_get_events(scratch.arena);
    
    if(window_handle.u64[0] == 0)
    {
      break_debugger();
    }
    
    F32 dt = FRAME_DT;
    Rng2_F32 client_rect = OS_client_rect_from_window(window_handle);
    Vec2_F32 client_rect_dim = dim2_F32(client_rect);
    Vec2_S64 resolution = vec2_S64_from_vec(client_rect_dim);
    
    //- Begin frame 
    R_frame_begin();
    D_frame_begin();
    UI_frame_begin(dt);
    R_window_start(window_r, resolution);
    D_Bucket *bucket = D_bucket_make(scratch.arena);
    // Anthing within this bucket scope will be pushed onto this the bucket.
    D_BucketScope(scratch.arena, bucket)
    {
      
      
      //~ Build UI 
      UI_build_begin(window_handle, &events); // TODO(anton): events
      
      U64 top_pane_height = 25;
      Vec2_F32 top_bar_p0 = vec2_F32(client_rect.x0, client_rect.y0);
      Vec2_F32 top_bar_p1 = vec2_F32(client_rect.x0 + client_rect_dim.x, client_rect.y0 + top_pane_height);
      Rng2_F32 top_bar_rect = rng2_F32(top_bar_p0, top_bar_p1);
      unused_variable(top_bar_rect);
      
      U64 left_pane_width = 0.2f*APP_WINDOW_WIDTH;
      
      ///////////////////////////
      //- Top bar 
      {  
        UI_push_fixed_rect(top_bar_rect);
        UI_Box *top_bar_box = UI_box_make(UI_BoxFlag_DrawDropShadow|UI_BoxFlag_DrawBackground|UI_BoxFlag_DrawBorder, str8_lit("top_bar_pane"));
        UI_pop_fixed_rect();
        UI_push_parent(top_bar_box);
        
        // Top pane
        //UI_pane(top_bar_rect, str8_lit("top_bar_pane"))
          //UI_width_fill
        {
          UI_Key file_menu_key = UI_key_from_string(UI_key_zero(), str8_lit("_file_menu_key_"));
          UI_ctx_menu(file_menu_key) 
          {
            UI_pref_width(UI_pixels(100, 0.0))
              UI_pref_height(UI_pixels(25, 0.0f)) 
            {
              String8 buttons[] = {str8_lit("Open"), str8_lit("Exit")};
              
              for(U64 i = 0; i < ArrayCount(buttons); i++)
              {
                UI_Signal sig = UI_button(buttons[i]);
                if(UI_pressed(sig))
                {
                  UI_ctx_menu_close();
                }
              }
              
            }
          }
          
          //  Buttons
          {
            B32 menu_open = 0;
            if(ui_state->ctx_menu_open && UI_key_match(file_menu_key, ui_state->ctx_menu_key))
            {
              menu_open = 1;
            }
            
            UI_set_next_pref_width(UI_pixels(100, 0));
            UI_set_next_pref_height(UI_pixels(25, 0));
            UI_Signal file_button = UI_button(str8_lit("File"));
            F32 button_size_y_px = file_button.box->rect.y1-file_button.box->rect.y0;
            Vec2_F32 menu_offset = vec2_F32(0, button_size_y_px);
            
            if(menu_open) 
            {
              if(UI_hovering(file_button) && !UI_ctx_menu_is_open(file_menu_key)) 
              {
                UI_ctx_menu_open(file_menu_key, file_button.box->key, menu_offset);
              }
            }
            else if(UI_pressed(file_button))
            {
              if(UI_ctx_menu_is_open(file_menu_key))
              {
                UI_ctx_menu_close();
              }
              else
              {
                UI_ctx_menu_open(file_menu_key, file_button.box->key, menu_offset);
              }
            }

            //UI_ctx_menu_close();
            
          }
          
        }
        UI_pop_parent();
        
      }
      
      
      
      UI_build_end();

      
      
      
      
      //~  Submit and end frame 
      UI_draw();
      
      //
      //Rng2_F32 range = rng2_F32(vec2_F32(10,10), vec2_F32(100, 35));
      //D_rect2D(range, .color  = vec4_F32(0, 0, 0, 1.f),
               //.corner_radius = 0,
               //.softness = 0,
               //.omit_texture = 1);
      //D_text2D(vec2_F32(range.p0.x, range.p1.y), str8_lit("Testing text"));
      //
      D_submit(window_r, bucket);
      R_window_finish(window_r);
      UI_frame_end();
    }
    
    //~ Handle Events 
    for(OS_Event *event = events.first; event != 0; event = event->next)
    {
      if(event->kind == OS_EventKind_WindowClose)
      {
        quit = 1;
        break;
      }
      if(event->kind == OS_EventKind_Press && OS_handle_match(event->window, window_handle) &&
         event->key == OS_Key_Esc)
      {
        quit = 1;
        break;
      }
    }
    
    // TODO(anton): Understand this? What is going on?
    if(frame_idx == 0) 
    {
      OS_window_first_paint(window_handle);
    }                  
    
    frame_idx += 1;
    if(frame_idx == U64Max)
    {
      frame_idx = 0;
    }
    
    scratch_release(scratch);
  }
  
  //~ Shutdown
  R_window_unequip(window_handle, window_r);
  R_shutdown();
}



//
//
//UI_padding(UI_pixels(10, 0)) 
//UI_width_fill 
//UI_row
//{
  //UI_spacer(UI_pixels(10, 0));
  //
  //UI_set_next_pref_size(Axis2_X, UI_pixels(100, 1));
  //UI_set_next_pref_size(Axis2_Y, UI_pixels(30, 1));
  //UI_Signal button1 = UI_button(str8_lit("Button1"));
  //unused_variable(button1);
  //
  //UI_spacer(UI_pixels(10, 0));
  //UI_set_next_pref_size(Axis2_X, UI_pixels(100, 0));
  //UI_set_next_pref_size(Axis2_Y, UI_pixels(30, 1));
  //UI_Signal button2 = UI_button(str8_lit("Button2"));
  //unused_variable(button2);
  //
  //UI_spacer(UI_pixels(10, 0));
  //UI_set_next_pref_size(Axis2_X, UI_pixels(100, 0));
  //UI_set_next_pref_size(Axis2_Y, UI_pixels(30, 1));
  //UI_Signal button3 = UI_button(str8_lit("Button3"));
  //unused_variable(button3);
  //
//}
//
