@{
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Author(s)      : Natalia Portillo <claunia@claunia.com>
//
// --[ License ] --------------------------------------------------------------
//
//     This program is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as
//     published by the Free Software Foundation, either version 3 of the
//     License, or (at your option) any later version.
//
//     This program is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
//
//     You should have received a copy of the GNU General Public License
//     along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/
}

@page "/admin/licenses/details/{Id:int}"
@page "/admin/licenses/edit/{Id:int}"
@page "/admin/licenses/create"
@using Marechai.Database.Models
@inherits OwningComponentBase<LicensesService>
@inject IStringLocalizer<LicensesService> L
@inject NavigationManager NavigationManager
@inject Microsoft.AspNetCore.Identity.UserManager<ApplicationUser> UserManager
@inject AuthenticationStateProvider AuthenticationStateProvider
@attribute [Authorize(Roles = "UberAdmin, Admin")]
<h3>@L["License details"]</h3>
<hr />

@if (!_loaded)
{
    <p align="center">@L["Loading..."]</p>

    return;
}

<div>
    <Field>
        <FieldLabel>@L["Name"]</FieldLabel>
        <Validation Validator="@ValidateName">
            <TextEdit Disabled="!_editing" @bind-Text="@_model.Name">
                <Feedback>
                    <ValidationError>@L["Please enter a valid name."]</ValidationError>
                </Feedback>
            </TextEdit>
        </Validation>
    </Field>
    <Field>
        <FieldLabel>@L["SPDX identifier"]</FieldLabel>
        <Validation Validator="@ValidateSpdx">
            <TextEdit ReadOnly="!_editing" @bind-Text="@_model.SPDX">
                <Feedback>
                    <ValidationError>@L["Please enter a valid SPDX identifier."]</ValidationError>
                </Feedback>
            </TextEdit>
        </Validation>
    </Field>
    <Field>
        <FieldLabel>@L["FSF approved"]</FieldLabel>
        <Check TValue="bool" Disabled="!_editing" @bind-Checked="@_model.FsfApproved"/>
    </Field>
    <Field>
        <FieldLabel>@L["OSI approved"]</FieldLabel>
        <Check TValue="bool" Disabled="!_editing" @bind-Checked="@_model.OsiApproved"/>
    </Field>
    @if (_editing || _model.Link != null)
    {
        <Field>
            <FieldLabel>@L["License text link"]</FieldLabel>
            @if (_editing)
            {
                <Check TValue="bool" @bind-Checked="@_unknownLink">@L["Unknown or none (text link)"]</Check>
            }
            @if (!_editing ||
                 !_unknownLink)
            {
                <Validation Validator="@ValidateLink">
                    <TextEdit ReadOnly="!_editing" @bind-Text="@_model.Link">
                        <Feedback>
                            <ValidationError>@L["Please enter a license text link."]</ValidationError>
                        </Feedback>
                    </TextEdit>
                </Validation>
            }
        </Field>
    }
    @if (_editing || _model.Text != null)
    {
        <Field>
            <FieldLabel>@L["License text"]</FieldLabel>
            @if (_editing)
            {
                <Check TValue="bool" @bind-Checked="@_unknownText">@L["Unknown (license text)"]</Check>
            }
            @if (!_editing ||
                 !_unknownText)
            {
                <Validation Validator="@ValidateText">
                    <MemoEdit Rows="200" Plaintext="true" ReadOnly="!_editing" @bind-Text="@_model.Text">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid license text."]</ValidationError>
                        </Feedback>
                    </MemoEdit>
                </Validation>
            }
        </Field>
    }
</div>
<div>
    @if (!_editing)
    {
        <Button Color="Color.Primary" Clicked="@OnEditClicked">@L["Edit"]</Button>
    }
    else
    {
        <Button Color="Color.Success" Clicked="@OnSaveClicked">@L["Save"]</Button>
        <Button Color="Color.Danger" Clicked="@OnCancelClicked">@L["Cancel"]</Button>
    }
    <a href="/admin/licenses" class="btn btn-secondary">@L["Back to list"]</a>
</div>