@{
/******************************************************************************
// 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/screens/details/{Id:int}"
@page "/admin/screens/edit/{Id:int}"
@page "/admin/screens/create"
@using Marechai.Database.Models
@inherits OwningComponentBase<ScreensService>
@inject IStringLocalizer<ScreensService> L
@inject ResolutionsService ResolutionsService
@inject NavigationManager NavigationManager
@inject ResolutionsByScreenService ResolutionsByScreenService
@inject Microsoft.AspNetCore.Identity.UserManager<ApplicationUser> UserManager
@inject AuthenticationStateProvider AuthenticationStateProvider
@attribute [Authorize(Roles = "UberAdmin, Admin")]
<h3>@L["Screen details"]</h3>
<hr />

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

    return;
}

<div>
    @if (_editing || _model.Width.HasValue)
    {
        <Field>
            <FieldLabel>@L["Width (mm)"]</FieldLabel>
            @if (_editing)
            {
                <Check TValue="bool" @bind-Checked="@_unknownWidth">@L["Unknown (width)"]</Check>
            }
            @if (!_editing ||
                 !_unknownWidth)
            {
                <Validation Validator="@ValidateDoubleBiggerThanZero">
                    <NumericEdit Disabled="!_editing" TValue="double?" Decimals="2" @bind-Value="@_model.Width" >
                        <Feedback>
                            <ValidationError>@L["Please enter a valid width in millimeters."]</ValidationError>
                        </Feedback>
                    </NumericEdit>
                </Validation>
            }
        </Field>
    }
    @if (_editing || _model.Height.HasValue)
    {
        <Field>
            <FieldLabel>@L["Height (mm)"]</FieldLabel>
            @if (_editing)
            {
                <Check TValue="bool" @bind-Checked="@_unknownHeight">@L["Unknown (height)"]</Check>
            }
            @if (!_editing ||
                 !_unknownHeight)
            {
                <Validation Validator="@ValidateDoubleBiggerThanZero">
                    <NumericEdit Disabled="!_editing" TValue="double?" Decimals="2" @bind-Value="@_model.Height" >
                        <Feedback>
                            <ValidationError>@L["Please enter a valid height in millimeters."]</ValidationError>
                        </Feedback>
                    </NumericEdit>
                </Validation>
            }
        </Field>
    }
    <Field>
        <FieldLabel>@L["Diagonal (inches)"]</FieldLabel>
        <Validation Validator="@ValidateDoubleBiggerThanZero">
            <NumericEdit Disabled="!_editing" TValue="double" Decimals="2" @bind-Value="@_model.Diagonal" >
                <Feedback>
                    <ValidationError>@L["Please enter a correct diagonal size in inches."]</ValidationError>
                </Feedback>
            </NumericEdit>
        </Validation>
    </Field>
    @if (_editing || _model.EffectiveColors.HasValue)
    {
        <Field>
            <FieldLabel>@L["Effective colors"]</FieldLabel>
            @if (_editing)
            {
                <Check TValue="bool" @bind-Checked="@_unknownColors">@L["Unknown (effective colors)"]</Check>
            }
            @if (!_editing ||
                 !_unknownColors)
            {
                <Validation Validator="@ValidateLongBiggerThanZero">
                    <NumericEdit Disabled="!_editing" TValue="long?" Decimals="0" @bind-Value="@_model.EffectiveColors" >
                        <Feedback>
                            <ValidationError>@L["Please enter a number of effective colors."]</ValidationError>
                        </Feedback>
                    </NumericEdit>
                </Validation>
            }
        </Field>
    }
    @if (_editing || _model.Type != null)
    {
        <Field>
            <FieldLabel>@L["Type"]</FieldLabel>
            @if (_editing)
            {
                <Check TValue="bool" @bind-Checked="@_unknownType">@L["Unknown (type)"]</Check>
            }
            @if (!_editing ||
                 !_unknownType)
            {
                <Validation Validator="@ValidateType">
                    <TextEdit ReadOnly="!_editing" @bind-Text="@_model.Type">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid screen type."]</ValidationError>
                        </Feedback>
                    </TextEdit>
                </Validation>
            }
        </Field>
    }
    <Field>
        <FieldLabel>@L["Native resolution"]</FieldLabel>
        <Select Disabled="!_editing" TValue="int" @bind-SelectedValue="@_model.NativeResolutionId">
            @foreach (var resolution in _resolutions)
            {
                <SelectItem TValue="int" Value="@resolution.Id">@resolution.ToString()</SelectItem>
            }
        </Select>
    </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/screens" class="btn btn-secondary">@L["Back to list"]</a>
</div>
@if (!_editing)
{
    <hr />
    <h3>@L["Resolutions supported by this screen"]</h3>
    <Button Color="Color.Success" Clicked="OnAddResolutionClick" Disabled="_addingResolution">@L["Add new (resolution by screen)"]</Button>
    @if (_addingResolution)
    {
        <div>
            <Field>
                <FieldLabel>@L["Resolutions"]</FieldLabel>
                <Select Disabled="_savingResolution" TValue="int?" @bind-SelectedValue="@_addingResolutionId">
                    @foreach (var resolution in _resolutions)
                    {
                        @if (resolution.Id != _model.NativeResolutionId &&
                             _screenResolutions.All(r => r.ResolutionId != resolution.Id))
                        {
                            <SelectItem TValue="int?" Value="@resolution.Id">@resolution</SelectItem>
                        }
                    }
                </Select>
                <Button Color="Color.Primary" Clicked="@CancelAddResolution" Disabled="@_savingResolution">@L["Cancel"]</Button>
                <Button Color="Color.Success" Clicked="@ConfirmAddResolution" Disabled="@_savingResolution">@L["Add"]</Button>
            </Field>
        </div>
    }
    @if (_screenResolutions?.Count > 0)
    {
        <div>
            <table class="table table-striped">
                <thead>
                <tr>
                    <th>
                        @L["Width"]
                    </th>
                    <th>
                        @L["Height"]
                    </th>
                    <th>
                        @L["Colors"]
                    </th>
                    <th>
                        @L["Palette"]
                    </th>
                    <th>
                        @L["Chars"]
                    </th>
                    <th>
                        @L["Grayscale"]
                    </th>
                    <th></th>
                </tr>
                </thead>
                <tbody>
                @foreach (var item in _screenResolutions)
                {
                    <tr>
                        <td>
                            @item.Resolution.Width
                        </td>
                        <td>
                            @item.Resolution.Height
                        </td>
                        <td>
                            @item.Resolution.Colors
                        </td>
                        <td>
                            @item.Resolution.Palette
                        </td>
                        <td>
                            @item.Resolution.Chars
                        </td>
                        <td>
                            @item.Resolution.Grayscale
                        </td>
                        <td>
                            <Button Color="Color.Danger" Clicked="() => {ShowResolutionDeleteModal(item.Id);}" Disabled="@_addingResolution">@L["Delete"]</Button>
                        </td>
                    </tr>
                }
                </tbody>
            </table>
        </div>
    }

    <Modal @ref="_frmDelete" IsCentered="true" Closing="@ModalClosing">
        <ModalBackdrop />
        <ModalContent Centered="true">
            <ModalHeader>
                <ModalTitle>@_deleteTitle</ModalTitle>
                <CloseButton Clicked="@HideModal" />
            </ModalHeader>
            <ModalBody>
                <Text>@_deleteText</Text>
            </ModalBody>
            <ModalFooter>
                <Button Color="Color.Primary" Clicked="@HideModal" Disabled="@_deleteInProgress">@L["Cancel"]</Button>
                <Button Color="Color.Danger" Clicked="@ConfirmDelete" Disabled="@_deleteInProgress">@L["Delete"]</Button>
            </ModalFooter>
        </ModalContent>
    </Modal>
}