<?php
$page = "string-diff-viewer";
include_once('includes/header.php');

$normal_string = "";
if(isset($_POST['old_text']) AND isset($_POST['new_text'])){
    $old = $_POST['old_text'];
    $new = $_POST['new_text'];
}else{
    $old = "";
    $new = "";
}
include __DIR__ . '/vendor/autoload.php';

use Jfcherng\Diff\Differ;
use Jfcherng\Diff\DiffHelper;
use Jfcherng\Diff\Factory\RendererFactory;
use Jfcherng\Diff\Renderer\RendererConstant;

// renderer class name:
//     Text renderers: Context, JsonText, Unified
//     HTML renderers: Combined, Inline, JsonHtml, SideBySide
$rendererName = 'Unified';

// the Diff class options
$differOptions = [
    // show how many neighbor lines
    // Differ::CONTEXT_ALL can be used to show the whole file
    'context' => 3,
    // ignore case difference
    'ignoreCase' => false,
    // ignore whitespace difference
    'ignoreWhitespace' => false,
];

// the renderer class options
$rendererOptions = [
    // how detailed the rendered HTML in-line diff is? (none, line, word, char)
    'detailLevel' => 'char',
    // renderer language: eng, cht, chs, jpn, ...
    // or an array which has the same keys with a language file
    'language' => 'eng',
    // show line numbers in HTML renderers
    'lineNumbers' => true,
    // show a separator between different diff hunks in HTML renderers
    'separateBlock' => true,
    // show the (table) header
    'showHeader' => true,
    // the frontend HTML could use CSS "white-space: pre;" to visualize consecutive whitespaces
    // but if you want to visualize them in the backend with "&nbsp;", you can set this to true
    'spacesToNbsp' => false,
    // HTML renderer tab width (negative = do not convert into spaces)
    'tabSize' => 4,
    // this option is currently only for the Combined renderer.
    // it determines whether a replace-type block should be merged or not
    // depending on the content changed ratio, which values between 0 and 1.
    'mergeThreshold' => 0.8,
    // this option is currently only for the Unified and the Context renderers.
    // RendererConstant::CLI_COLOR_AUTO = colorize the output if possible (default)
    // RendererConstant::CLI_COLOR_ENABLE = force to colorize the output
    // RendererConstant::CLI_COLOR_DISABLE = force not to colorize the output
    'cliColorization' => RendererConstant::CLI_COLOR_ENABLE ,
    // this option is currently only for the Json renderer.
    // internally, ops (tags) are all int type but this is not good for human reading.
    // set this to "true" to convert them into string form before outputting.
    'outputTagAsString' => false,
    // this option is currently only for the Json renderer.
    // it controls how the output JSON is formatted.
    // see available options on https://www.php.net/manual/en/function.json-encode.php
    'jsonEncodeFlags' => \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE,
    // this option is currently effective when the "detailLevel" is "word"
    // characters listed in this array can be used to make diff segments into a whole
    // for example, making "<del>good</del>-<del>looking</del>" into "<del>good-looking</del>"
    // this should bring better readability but set this to empty array if you do not want it
    'wordGlues' => [' ', '-'],
    // change this value to a string as the returned diff if the two input strings are identical
    'resultForIdenticals' => null,
    // extra HTML classes added to the DOM of the diff container
    'wrapperClasses' => ['diff-wrapper table table-bordered'],
];

$jsonResult = DiffHelper::calculate($old, $new, 'Json'); // may store the JSON result in your database

$htmlRenderer = RendererFactory::make('SideBySide', $rendererOptions);
$result = $htmlRenderer->renderArray(json_decode($jsonResult, true));
?>
<style type="text/css">
    <?= DiffHelper::getStyleSheet(); ?>
</style>
    <div class="container">
        <div class="row">
            <div class="col-md-12 mt-2 mb-2">
                <h1><?php echo $title; ?></h1>
                <form action="" method="POST">
                    <div class="row">
                        <div class="col-md-6">
                            
                            <label for="exampleFormControlTextarea1" class="form-label">Original Text</label>
                            <textarea class="form-control" id="exampleFormControlTextarea1" rows="10" name="old_text"><?php echo $old; ?></textarea>
                        </div>

                        <div class=" col-md-6">
                            <label for="exampleFormControlTextarea1" class="form-label">Updated Text</label>
                            <textarea class="form-control" id="exampleFormControlTextarea1" rows="10" name="new_text"><?php echo $new; ?></textarea>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12 mt-2">
                            <button type="submit" class="btn btn-dark float-end">Submit</button>
                        </div>
                    </div>
                </form>
                <?php if(!empty($result)){ ?>
                    <div class="card">
                        <div class="card-header">Differences</div>
                            <div class="card-body">
                                <div class="mb-3">
                                    <?php echo $result; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php } ?>
            </div>
        </div>
    </div>
<?php
include_once('includes/footer.php');
