<?php
require_once('_config.php'); 
setlocale(LC_NUMERIC, 'fi_FI');

//Etusivun infoteksti
$text_page = \PStatic::Text_page('find_by_role','INDEX');

if (!empty($_POST['save'])) {
    $notes_text = Text_block::get_by_placement('gh_front_page_notes');
    $notes_text->input_text($_POST['gh_front_page_notes'], 'html');

    $save_success = (bool) $notes_text->save();

    if ($save_success) {
        Message_stack::add('Saved successfully', 'success');
    }
    else {
        Message_stack::add('Save error', 'danger');
    }
    header("Location: index.php?saved");
    exit();
}

// Jos asetuksissa on määritelty etusivun title, käytetään sitä. Muuten käytetään sivuston titleä.
$page_title = 'Home';

$header_meta_data_custom['canonical'] = '<link rel="canonical" href="' . CFG_BASE_URL . '">';
$header_meta_data_custom['og:url'] = '<meta property="og:url" content="' . CFG_BASE_URL . '">';
$header_meta_data_custom['title'] = '<title>Mi Party</title>';

$active_characters  = Character::get_characters('AND retired != 1', 'seat');
$last_play          = Play::get_last_play();
$max_rookie_level   = floor($prosperity_level/2);
$level_total        = 0;
$rookie_characters  = 0;

foreach ($active_characters as $character) {
    $level_total += $character->get_current_level();
    if ($character->get_current_level() <= $max_rookie_level) {
        $rookie_characters++;
    }
}

if ($rookie_characters >= 2) {
    $low_level_text = '2+ low-level characters';
}
else {
    $low_level_text = '0–1 low-level characters';
}

$donatation_sql = "SELECT SUM(`gold_donation`) / -10 AS donations_total FROM `character_to_play`;";
$query = DB::get()->query($donatation_sql, array());
$query->execute();
$data = $query->fetch();

$donations_total = $data['donations_total'];

$dates               = Play::get_first_play_date();
$diff                = strtotime(date('Y-m-d'), 0) - strtotime($dates['first_date'], 0);
$days_since_start    = floor($diff / 86400);
$scenarios_completed = count(Play::get_plays_no_solo());
$scenarios_per_day   = $scenarios_completed/$days_since_start;
$total_scenarios     = 95; // ToDo: laske
$blocked_scenarios   = 10; // ToDo: laske
$extra_scenarios     = 4; // ToDo: laske
$fc_scenarios        = 21; // ToDo: laske
$unblocked_scenarios = $total_scenarios - $blocked_scenarios + $extra_scenarios + $fc_scenarios;
$scenarios_left      = $unblocked_scenarios - $scenarios_completed;
$days_left           = round($scenarios_left / $scenarios_per_day, 0);
$average_level       = $level_total / count($active_characters);
$rounded_avg_level   = ceil($average_level/2);

if ($rookie_characters > 1) {
    $extra_difficulty = 1;
}
else {
    $extra_difficulty = 2;
}

require_once('_header.php');

?>

<main>

    <div class="container"
         style="max-width: 1000px;"
    >
        <h1 class="mb-4 text-center">Mi Party</h1>

        <div class="row">
            <div class="col-md">
                <div class="card text-bg-dark mb-4 mb-md-0">
                    <div class="card-body">
                        <div class="row gx-0 mb-4">
                        <?php foreach ($active_characters as $character): ?>
                            <div class="col-3 text-center">
                                <div class="px-1 px-sm-2">
                                    <p class="h6 mb-2">
                                        <a href="character.php?id=<?= $character->get_id() ?>"
                                           style="color: <?= $character->get_character_class()->get_color() ?>;"
                                           class="d-block"
                                        >
                                            <?= $character->show_name() ?><br>
                                            <img src="<?= $character->get_character_class()->show_face() ?>"
                                                 width="72"
                                                 height="72"
                                                 class="rounded-circle d-inline-block shadow border border-light mt-2"
                                            />
                                        </a>
                                    </p>

                                    <p class="mb-2 lh-sm g-0 text-center">
                                        <span class="text-muted">Level:</span>
                                        <b><?= $character->get_current_level() ?></b>
                                    </p>

                                    <div class="row g-0 lh-sm">
                                        <div class="col">
                                            <span class="d-inline-block text-muted"
                                                  style="width: 13px; height: 13px; position: relative; top: -2px;"
                                            ><?= file_get_contents(CFG_IMAGE_FOLDER . '/xp.svg') ?></span>
                                            <br>
                                            <b style="position: relative; top: 0px;"><?= $character->get_current_xp() ?></b>
                                        </div>

                                        <div class="col">
                                            <span class="text-muted d-inline-block"
                                                  style="height: 13px;"
                                            >
                                                <i class="fas fa-coin text-muted"
                                                   style="font-size: 0.7em;"
                                                ></i>
                                            </span>
                                            <br>
                                            <b><?= $character->get_current_gold() ?></b>
                                        </div>

                                        <div class="col">
                                            <span class="text-muted d-inline-block"
                                                  style="height: 13px;"
                                            >
                                                <i class="fas fa-check"
                                                   style="font-size: 0.7em;"
                                                ></i>
                                            </span>
                                            <br>
                                            <b><?= $character->get_current_checkmarks() - $character->get_lost_checkmarks() + $character->get_extra_checkmarks() ?></b>
                                        </div>
                                    </div>

                                </div>
                            </div>
                        <?php endforeach ?>
                        </div>

                        <table class="table table-sm border-light-subtle mb-4 fs-6">
                            <tbody>
                                <tr>
                                    <th class="">Scenario level</th>
                                    <td data-bs-toggle="tooltip"
                                        data-bs-html="true"
                                        data-custom-class="text-start"
                                        class="cursor-help"
                                        title="<?= 'Base level: ' . $rounded_avg_level . '<br>Extra difficulty: ' . $extra_difficulty . '<br>(' . $low_level_text . ')' ?>"
                                    >
                                        <?= $rounded_avg_level + $extra_difficulty ?>
                                    </td>
                                </tr>
                                <tr>
                                    <th class="">Prosperity level</th>
                                    <td>9</td>
                                </tr>
                                <tr>
                                    <th class="">Total donations</th>
                                    <td><?= (int) $donations_total ?></td>
                                </tr>
                                <tr>
                                    <th class="">Last play</th>
                                    <td>
                                        <a href="play.php?id=<?= $last_play->get_id() ?>">
                                            <?= $last_play->show_date('date') ?>
                                            <span class="text-muted">(<?= $last_play->get_scenario()->show_identifier() ?>)</span>
                                        </a>
                                    </td>
                                </tr>
<!--                                <tr>-->
<!--                                    <th class="">Scenarios completed</th>-->
<!--                                    <td>--><?php //= $scenarios_completed ?><!--/--><?php //= $unblocked_scenarios ?><!--</td>-->
<!--                                </tr>-->
<!--                                <tr>-->
<!--                                    <th class="">Estimated end date</th>-->
<!--                                    <td>--><?php //= date('j.n.Y', strtotime("now + $days_left days")) ?><!--</td>-->
<!--                                </tr>-->
                            </tbody>
                        </table>

                        <p class="small mb-0 text-muted">
                            Main scenarios: <?= $total_scenarios ?> • Forgotten Circles: <?= $fc_scenarios ?> • Extra: <?= $extra_scenarios ?> • Blocked: <?= $blocked_scenarios ?>
                        </p>

                    </div>
                </div>
            </div>

            <div class="col-md">
                <div class="card text-bg-dark">
                    <div class="card-body">
                        <h2 class="h5 mb-3">
                            Notes
                            <?php if (user_logged_in()): ?>
                                <a href="#" class="js-edit-front-page-notes"><i class="fas fa-sm fa-pencil"></i></a>
                            <?php endif ?>
                        </h2>

                        <?php $notes_text = Text_block::get_by_placement('gh_front_page_notes'); ?>

                        <div class="editor-content front-page-notes js-front-page-notes-html">
                            <?= $notes_text->show_text('html') ?>
                        </div>

                        <?php if (user_logged_in()): ?>
                        <div class="editor-content js-front-page-notes-form"
                             style="display: none;"
                        >
                            <form action="index.php"
                                  method="post"
                            >
                                <textarea name="gh_front_page_notes"
                                          data-input="tinymce"
                                          class="form-control"
                                ><?= $notes_text->show_text('html') ?></textarea>
                                <button type="submit"
                                        name="save"
                                        class="btn btn-primary mt-4"
                                        value="1"
                                >Save</button>
                            </form>
                        </div>
                        <?php endif ?>
                    </div>
                </div>
            </div>
        </div>


    </div>
</main>

<?php include('_footer.php'); ?>
