import "luxe: draw" for Draw, PathStyle
import "luxe: world" for Entity, Transform, UI, UIRenderMode
import "luxe: ui/control" for Control
import "luxe: ui/panel" for UIPanel
import "luxe: ui/list" for UIList
import "luxe: ui/button" for UIButton
import "luxe: ui/image" for UIImage
import "luxe: assets" for Assets

import "globals" for Globals
import "blocks/ui/image_button" for ImageButton
import "math/observable" for Observable

class Ui{
  static Planning{"planning"}
  static Info {"info"}

  ui_mode{_ui_mode.value}
  ui_mode=(v){_ui_mode.value = v}

  construct new(app){
    var ui_rect = Globals["UiRect"]

    _ui_mode = Observable.new(Ui.Info)
    System.print(_ui_mode)

    _ui = Entity.create(app.ui, "UI Root")
    
    UI.create(_ui, ui_rect.x, ui_rect.y, ui_rect.width, ui_rect.height, 0, app.ui_camera)
    UI.set_render_mode(_ui, UIRenderMode.world)

    _info = setup_info()
    _planning = setup_planning()

    _ui_mode.on_change(true) {|val| 
      Control.set_visible(_planning, val == Ui.Planning)
      Control.set_visible(_info, val == Ui.Info)
      UI.commit(_ui)
    }
    
    UI.commit(_ui)
  }

  setup_info(){
    var ui_rect = Globals["UiRect"]
    var game = Globals["Game"]

    var root = Control.create(_ui)
    Control.set_size(root, ui_rect.width, ui_rect.height)

    //list
    var list = UIList.create(_ui)
    Control.child_add(root, list)
    //todo: make list horizontal?
    Control.set_pos(list, 0, ui_rect.height-16)
    Control.set_size(list, ui_rect.width, 16)

    var adventureButtons = Assets.image("assets/AdventureButtons")
    var tiles = [10, 1]
    var button

    //plan adventure
    button = list_button(0)
    UIImage.set_image(button, adventureButtons)
    ImageButton.set_tooltip(button, "Adventure!")
    ImageButton.set_tile_uv(button, tiles, [1, 0])
    ImageButton.set_state_change(button) { |data| 
      System.print(data)
      if(data["press"]){
        ui_mode = Ui.Planning
        
      }
    }
    UIList.add(list, button)

    //info
    button = list_button(1)
    UIImage.set_image(button, adventureButtons)
    ImageButton.set_tooltip(button, "Stats")
    ImageButton.set_tile_uv(button, tiles, [2, 0])
    UIList.add(list, button)

    //read diary
    button = list_button(2)
    UIImage.set_image(button, adventureButtons)
    ImageButton.set_tooltip(button, "Diary")
    ImageButton.set_tile_uv(button, tiles, [1, 0])
    UIList.add(list, button)

    //UIList.refresh(list) //uncomment this when list learns not to be vertical
    return root
  }

  setup_planning(){
    var ui_rect = Globals["UiRect"]
    var game = Globals["Game"]

    var root = Control.create(_ui)
    Control.set_size(root, ui_rect.width, ui_rect.height)

    //list
    var list = UIList.create(_ui)
    Control.child_add(root, list)
    //todo: make list horizontal?
    Control.set_pos(list, 0, ui_rect.height-16)
    Control.set_size(list, ui_rect.width, 16)

    var adventureButtons = Assets.image("assets/AdventureButtons")
    var tiles = [10, 1]
    var button

    var list_id = 0

    //back to info
    button = list_button(list_id)
    UIImage.set_image(button, adventureButtons)
    ImageButton.set_tooltip(button, "Info")
    ImageButton.set_tile_uv(button, tiles, [1, 0])
    ImageButton.set_state_change(button) { |data| 
      System.print(data)
      if(data["press"]){
        ui_mode = Ui.Info
      }
    }
    UIList.add(list, button)
    list_id = list_id+1

    //abort
    button = list_button(list_id)
    UIImage.set_image(button, adventureButtons)
    ImageButton.set_tooltip(button, "Abort")
    ImageButton.set_tile_uv(button, tiles, [0, 0])
    UIList.add(list, button)
    list_id = list_id+1

    //people
    button = list_button(list_id)
    UIImage.set_image(button, adventureButtons)
    ImageButton.set_tooltip(button, "Adventurers")
    ImageButton.set_tile_uv(button, tiles, [2, 0])
    UIList.add(list, button)
    list_id = list_id+1
    
    //stuff
    button = list_button(list_id)
    UIImage.set_image(button, adventureButtons)
    ImageButton.set_tooltip(button, "Stuff")
    ImageButton.set_tile_uv(button, tiles, [4, 0])
    UIList.add(list, button)
    list_id = list_id+1
    
    //direction
    button = list_button(list_id)
    UIImage.set_image(button, adventureButtons)
    ImageButton.set_tooltip(button, "Direction")
    ImageButton.set_tile_uv(button, tiles, [3, 0])
    UIList.add(list, button)
    list_id = list_id+1
    
    //go
    button = list_button(list_id)
    UIImage.set_image(button, adventureButtons)
    ImageButton.set_tooltip(button, "Depart")
    ImageButton.set_tile_uv(button, tiles, [5, 0])
    UIList.add(list, button)
    list_id = list_id+1

    //UIList.refresh(list) //uncomment this when list learns not to be vertical

    return root
  }

  list_button(i){
    var button = ImageButton.create(_ui)
    Control.set_size(button, 16, 16)
    Control.set_pos(button, i*16, 0) //let list handle this in future
    return button
  }

  test(){
    ImageButton.get_data(_test)
  }
}