#include <stdint.h>
#include <stdlib.h>
#include "el_runtime.h"

el_val_t on_greet_click(el_val_t widget, el_val_t data);
el_val_t on_counter_click(el_val_t widget, el_val_t data);
el_val_t on_name_change(el_val_t widget, el_val_t data);
el_val_t app_build(el_val_t window);

el_val_t g_window;
el_val_t g_label;
el_val_t g_input;
el_val_t g_button;
el_val_t g_counter;
el_val_t g_counter_lbl;
el_val_t manifest_env;
el_val_t manifest_path;
el_val_t win;
el_val_t g_window;

el_val_t on_greet_click(el_val_t widget, el_val_t data) {
  el_val_t name = widget_get_text(g_input);
  el_val_t greeting = ({ el_val_t _if_result_1 = 0; if ((str_len(name) > 0)) { _if_result_1 = (el_str_concat(el_str_concat(EL_STR("Hello, "), name), EL_STR("!"))); } else { _if_result_1 = (EL_STR("Hello, World!")); } _if_result_1; });
  widget_set_text(g_label, greeting);
  return 0;
}

el_val_t on_counter_click(el_val_t widget, el_val_t data) {
  el_val_t g_counter = (g_counter + 1);
  widget_set_text(g_counter_lbl, el_str_concat(EL_STR("Clicks: "), int_to_str(g_counter)));
  return 0;
}

el_val_t on_name_change(el_val_t widget, el_val_t data) {
  el_val_t has_text = (str_len(data) > 0);
  widget_set_disabled(g_button, !has_text);
  return 0;
}

el_val_t app_build(el_val_t window) {
  el_val_t root = vstack(0);
  widget_set_padding_all(root, 24);
  widget_set_bg_color_hex(root, EL_STR("#0f172a"));
  widget_set_flex(root, 1);
  el_val_t title = label(EL_STR("el-native \xe2\x80\x94 native widget demo"));
  style_label_heading(title);
  widget_add_child(root, title);
  el_val_t gap1 = label(EL_STR(""));
  widget_set_height(gap1, 16);
  widget_add_child(root, gap1);
  el_val_t subtitle = label(EL_STR("AppKit controls from el code, no ObjC in the app layer."));
  style_label_muted(subtitle);
  widget_add_child(root, subtitle);
  el_val_t gap2 = label(EL_STR(""));
  widget_set_height(gap2, 24);
  widget_add_child(root, gap2);
  el_val_t input_row = hstack(8);
  el_val_t name_label = label(EL_STR("Name:"));
  style_label_body(name_label);
  widget_set_width(name_label, 60);
  el_val_t input = text_field(EL_STR("Enter your name\xe2\x80\xa6"));
  style_label_body(input);
  widget_set_flex(input, 1);
  widget_on_change(input, EL_STR("on_name_change"));
  el_val_t g_input = input;
  el_val_t greet_btn = button(EL_STR("Greet"));
  style_button_primary(greet_btn);
  widget_set_disabled(greet_btn, 1);
  widget_on_click(greet_btn, EL_STR("on_greet_click"));
  el_val_t g_button = greet_btn;
  widget_add_child(input_row, name_label);
  widget_add_child(input_row, input);
  widget_add_child(input_row, greet_btn);
  widget_add_child(root, input_row);
  el_val_t gap3 = label(EL_STR(""));
  widget_set_height(gap3, 12);
  widget_add_child(root, gap3);
  el_val_t greeting = label(EL_STR("Waiting for name\xe2\x80\xa6"));
  style_label_body(greeting);
  widget_set_color_hex(greeting, EL_STR("#60a5fa"));
  widget_set_font(greeting, EL_STR("system"), 16, 1);
  el_val_t g_label = greeting;
  widget_add_child(root, greeting);
  el_val_t gap4 = label(EL_STR(""));
  widget_set_height(gap4, 24);
  widget_add_child(root, gap4);
  el_val_t counter_row = hstack(12);
  el_val_t counter_lbl = label(EL_STR("Clicks: 0"));
  style_label_body(counter_lbl);
  el_val_t g_counter_lbl = counter_lbl;
  el_val_t counter_btn = button(EL_STR("Click me"));
  style_button_primary(counter_btn);
  widget_on_click(counter_btn, EL_STR("on_counter_click"));
  widget_add_child(counter_row, counter_lbl);
  widget_add_child(counter_row, counter_btn);
  widget_add_child(root, counter_row);
  widget_add_child(window, root);
  return 0;
}

int el_app_main(int _argc, char** _argv) {
  el_runtime_init_args(_argc, _argv);
  g_window = (-1);
  g_label = (-1);
  g_input = (-1);
  g_button = (-1);
  g_counter = 0;
  g_counter_lbl = (-1);
  native_init();
  manifest_env = env(EL_STR("EL_MANIFEST"));
  manifest_path = ({ el_val_t _if_result_2 = 0; if ((str_len(manifest_env) > 0)) { _if_result_2 = (manifest_env); } else { _if_result_2 = (EL_STR("manifest.el")); } _if_result_2; });
  win = window_from_manifest(manifest_path);
  g_window = win;
  if (win < 0) {
    println(EL_STR("Error: failed to create window. Is EL_TARGET_MACOS defined?"));
    exit_program(1);
  }
  app_build(win);
  window_show(win);
  native_run_loop();
  return 0;
}

