﻿@page "/"

@using virus_tortoise.lib

@inject PredictionService predictionService

<section class="h-100">
    <header class="container h-100">
        <div class="d-flex align-items-center justify-content-center h-100">
            <div class="d-flex flex-column">
                <div class="text-center">
                    <img src="/img/logo.png" class="img-fluid" style="max-width: 485px;" alt="virus tortoise" />
                </div>

                @if (_classificationResponseItem != null)
                {
                    @if (!string.IsNullOrEmpty(_classificationResponseItem.ErrorMessage))
                    {
                        <p>@_classificationResponseItem.ErrorMessage</p>
                    }
                    else
                    {
                        <h3 style="margin-top: 20px">File Prediction Results</h3>

                        <table class="table" style="margin: 0 auto;">
                            <tr>
                                <td class="tableLabel">SHA256 Hash</td>
                                <td>@_classificationResponseItem.SHA256</td>
                            </tr>
                            <tr>
                                <td class="tableLabel">SHA1 Hash</td>
                                <td>@_classificationResponseItem.SHA1</td>
                            </tr>
                            <tr>
                                <td class="tableLabel">MD5 Hash</td>
                                <td>@_classificationResponseItem.MD5</td>
                            </tr>
                            <tr>
                                <td class="tableLabel">File Size (bytes)</td>
                                <td>@_classificationResponseItem.FileSize</td>
                            </tr>
                            <tr>
                                <td class="tableLabel">File Type</td>
                                <td>@_classificationResponseItem.FileType</td>
                            </tr>
                            <tr>
                                <td class="tableLabel">Is Valid</td>
                                <td>@_classificationResponseItem.IsValid</td>
                            </tr>
                            <tr>
                                <td class="tableLabel">Analysis Notes</td>
                                <td>
                                    @if (_classificationResponseItem.AnalysisNotes == null)
                                    {
                                        <p>(No notes)</p>
                                    }
                                    else
                                    {
                                        @foreach (var line in _classificationResponseItem.AnalysisNotes)
                                        {
                                            <p>@line</p>
                                        }
                                    }
                                </td>
                            </tr>
                        </table>

                        <button @onclick="ResetForm" class="btn btn-primary">Submit another file</button>
                    }
                }
                else
                {
                    <div style="text-align: center">
                        <InputFile OnChange="HandleSelection" />
                    </div>
                }

                <br/>

                Source code is on <a href="https://github.com/jcapellman/virus-tortoise/" target="_blank">GitHub</a>
                Created by <a href="http://www.jarredcapellman.com/" target="_blank">Jarred Capellman</a>
                This site is for purely educational/research purposes.
            </div>
        </div>
    </header>
</section>

@functions {
    public void ResetForm()
    {
        _classificationResponseItem = null;
    }
}

@code {
    PredictionResponseItem _classificationResponseItem;

    async Task HandleSelection(IEnumerable<IFileListEntry> files)
    {
        var file = files.FirstOrDefault();

        if (file != null)
        {
            var ms = new MemoryStream();
            await file.Data.CopyToAsync(ms);

            _classificationResponseItem = predictionService.PredictFile(ms.GetBuffer());
        }
    }
}