window.addEventListener('pywebviewready', function() {
  pywebview.api.on_pywebview_ready();
})
var data = "";

function update_header() {
  pywebview.api.get_header().then(function(response) {
    document.getElementById('head').innerHTML = response;
  });
}

function update_main() {
  pywebview.api.get_body().then(function(response) {
    document.getElementById('main').innerHTML = response;
  });
}

var current = ""
function select_user(name) {
  console.log("enter");
  current = "user"
  var items = document.getElementsByName('rGroup');
  for (var i = 0; i < items.length; i ++) {
      items[i].checked = false;
  }
  document.getElementById(name).checked = true;
  document.getElementById('header_name_display_info').innerHTML = name;
  pywebview.api.update_settings_player(name);
}

function minimize_window() {
  pywebview.api.minimize_window();
}

function exit_window() {
  pywebview.api.exit_window()
}

function select_all_users() {
  current = "all"
  var items = document.getElementsByName('rGroup');
  for (var i = 0; i < items.length; i ++) {
      items[i].checked = true;
  }
  document.getElementById('header_name_display_info').innerHTML = "Multi";
  pywebview.api.update_settings_multi(true);
}

function popup_name_appear()
{
  document.getElementsByClassName('popup')[0].addEventListener('click', function(e) {
    e.preventDefault();
    e.stopPropagation();
  });
  promise = pywebview.api.update_popup('name').then(function(response) {
    document.getElementsByClassName('popup')[0].innerHTML = response;
    if (current == "all") {
      var items = document.getElementsByName('rGroup');
      for (var i = 0; i < items.length; i ++) {
          items[i].checked = true;
    }
  }
  });
  document.getElementsByClassName('popup_closer')[0].style.display = 'block';
}

function close_popup() {
  document.getElementsByClassName('popup_closer')[0].style.display = 'none';
}

function set_view_combat() {
  pywebview.api.set_view_combat();
}

function set_view_overview() {
  pywebview.api.set_view_overview();
}

function set_view_fm() {
  pywebview.api.set_view_fm();
}

function set_view_hunt() {
  pywebview.api.set_view_hunt();
}

function update_fm_pool_value() {
  var value = document.getElementById("fm_pool_current_value").value
  pywebview.api.update_fm_pool(value)
}
function update_content(response) {
  document.body.innerHTML = response;
}

function set_combat_player_view(player_name) {
  pywebview.api.set_view_combat_player(player_name)
}