<?php declare(strict_types=1);

// needs global $qrbApi

// included by index.php

// Target for POST with action 'selectProfile'.
if (array_key_exists('selectProfile', $_POST))
{
    $profile = $_POST['profile'];
    
    execute($qrbApi, function() use ($qrbApi, $profile)
    {
        $qrbApi->writeLog("Attempt to change to profile '{$profile}'");
        if ($qrbApi->selectCurrentProfile($profile))
        {
            reloadPage();
        }
        else
        {
            print '<div class="highlight"><p>';
            print $qrbApi->xl('exception.profile.selectFailed'); 
            print '</p></div>';
        }
    });
}

// Target for POST with action 'createProfile'.
if (array_key_exists('createProfile', $_POST))
{
    $profile = $_POST['newProfile'];

    if ($profile !== "") // just ignore empty string
    {
        execute($qrbApi, function() use ($qrbApi, $profile)
        {
            $qrbApi->writeLog("Attempt to create new profile '{$profile}'");
            if ($qrbApi->createProfile($profile))
            {
                reloadPage();
            }
            else
            {
                print '<div class="highlight"><p>';
                print $qrbApi->xl('exception.profile.createFailed'); 
                print '</p></div>';
            }
        });
    }
} 

// Target for POST with action 'deleteProfile'.
if (array_key_exists('deleteProfile', $_POST))
{
    $profile = $_POST['profileToDelete'];
    
    if ($profile !== "") // just ignore empty string (see below in form)
    {
        execute($qrbApi, function() use ($qrbApi, $profile)
        {
            $qrbApi->writeLog("Attempt to delete {$profile}");
            if ($qrbApi->deleteProfile($profile))
            {
                reloadPage();
            }
            else
            {
                print '<div class="highlight"><p>';
                print $qrbApi->xl('exception.profile.deleteFailed'); 
                print '</p></div>';
            }
        });
    }
}

function execute(QrbApi $qrbApi, Closure $functionToCall) : void
{
    try
    {
        $functionToCall();
    }
    catch (QrbException $e)
    {
        $msg = $qrbApi->xl($e->getLocalizeKey());
        print '<div class="highlight"><p>';
        print "<strong>{$msg}</strong> {$e->getAdditionalInformation()}";
        print '</p></div>';
        $qrbApi->writeLog("{$msg}: {$e}");
    }
    catch (\Throwable $th)
    {
        $msg = $qrbApi->xl("exception.unexpectedError1") . $th->getMessage() . $qrbApi->xl("exception.unexpectedError2"); 
        print '<div class="highlight"><p>';
        print $msg;
        print '</p></div>';
        $qrbApi->writeLog("{$msg}: {$th}");
    }
}

$profiles = $qrbApi->getProfiles();
?>

  <div class="profiles highlight">
  
    <div class="select-profile">
      <form method="post">
<?php
print $qrbApi->xl('profile.selection.title'); 
?>
        <br />
        <select class="form-select" name="profile" id="profile" size="1">
<?php 
$currentProfile = $qrbApi->getCurrentProfile(); 
$qrbApi->writeLog("Current profile is '{$currentProfile}'");

foreach ($profiles as $profile)
{
  print '<option value="' . $profile . '"'; 
  if ($profile == $currentProfile) print ' selected';
  print '>';
  print $profile;
  print '</option>';
}
?>
        </select>
        <button class="btn btn-dark" type="submit" name="selectProfile">
<?php
print $qrbApi->xl('profile.selection.submit'); 
?>
        </button>
      </form>
    </div>
    
    <div class="create-profile">
      <form method="post">
<?php
print $qrbApi->xl('profile.creation.title'); 
?>
        <br />
        <input class="form-control" type="text" name="newProfile" value="" />
        <button class="btn btn-dark" type="submit" name="createProfile">
<?php
print $qrbApi->xl('profile.creation.submit'); 
?>
        </button>
      </form>
    </div>

    <div class="delete-profile">
      <form method="post">
<?php
print $qrbApi->xl('profile.deletion.title'); 
?>
        <br />
        <select class="form-select" name="profileToDelete" id="profile" size="1">
<?php 
// add empty string as first option to prohibit accidental deletion of first profile by some random click
print '<option value=""> </option>'; 

foreach ($profiles as $profile)
{
  // do not allow to delete default profile
  if ($profile == QrbEnvironment::DEFAULT_PROFILE) continue;
  
  print '<option value="' . $profile . '">'; 
  print $profile;
  print '</option>';
}
?>
        </select>
        <button class="btn btn-danger" type="submit" name="deleteProfile">
<?php
print $qrbApi->xl('profile.deletion.submit'); 
?>
        </button>
      </form>
    </div>

  </div>
