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

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

<div>
    @if (_editing || _model.CompanyId != null)
    {
        <Field>
            <FieldLabel>@L["Company"]</FieldLabel>
            @if (_editing)
            {
                <Check TValue="bool" @bind-Checked="@_unknownCompany">@L["Unknown (company)"]</Check>
            }
            @if (!_editing ||
                 !_unknownCompany)
            {
                <Select Disabled="!_editing" TValue="int?" @bind-SelectedValue="@_model.CompanyId">
                    @foreach (var company in _companies)
                    {
                        <SelectItem TValue="int?" Value="@company.Id">@company.Name</SelectItem>
                    }
                </Select>
            }
        </Field>
    }
    <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>
    @if (_editing || _model.ModelCode != null)
    {
        <Field>
            <FieldLabel>@L["Model code"]</FieldLabel>
            @if (_editing)
            {
                <Check TValue="bool" @bind-Checked="@_unknownModelCode">@L["Unknown (model code)"]</Check>
            }
            @if (!_editing ||
                 !_unknownModelCode)
            {
                <Validation Validator="@ValidateModelCode">
                    <TextEdit Disabled="!_editing" @bind-Text="@_model.ModelCode">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid model code."]</ValidationError>
                        </Feedback>
                    </TextEdit>
                </Validation>
            }
        </Field>
    }
    @if (_editing || _model.Introduced.HasValue)
    {
        <Field>
            <FieldLabel>@L["Introduced"]</FieldLabel>
            @if (_editing)
            {
                <Check TValue="bool" Disabled="_prototype" @bind-Checked="@_unknownIntroduced">@L["Unknown (introduction date)"]</Check>
                <Check TValue="bool" Disabled="_unknownIntroduced" @bind-Checked="@_prototype">@L["Prototype"]</Check>
            }
            @if (!_editing ||
                 (!_prototype && !_unknownIntroduced))
            {
                <Validation Validator="@ValidateIntroduced">
                    <DateEdit Disabled="!_editing" TValue="DateTime?" @bind-Date="@_model.Introduced">
                        <Feedback>
                            <ValidationError>@L["Please enter an introduction date."]</ValidationError>
                        </Feedback>
                    </DateEdit>
                </Validation>
            }
            </Field>
    }
    @if (_editing || _model.Package != null)
    {
        <Field>
            <FieldLabel>@L["Package"]</FieldLabel>
            @if (_editing)
            {
                <Check TValue="bool" @bind-Checked="@_unknownPackage">@L["Unknown (package)"]</Check>
            }
            @if (!_editing ||
                 !_unknownPackage)
            {
                <Validation Validator="@ValidatePackage">
                    <TextEdit ReadOnly="!_editing" @bind-Text="@_model.Package">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid package."]</ValidationError>
                        </Feedback>
                    </TextEdit>
                </Validation>
            }
        </Field>
    }
    @if (_editing || _model.Process != null)
    {
        <Field>
            <FieldLabel>@L["Process"]</FieldLabel>
            @if (_editing)
            {
                <Check TValue="bool" @bind-Checked="@_unknownProcess">@L["Unknown (process)"]</Check>
            }
            @if (!_editing ||
                 !_unknownProcess)
            {
                <Validation Validator="@ValidateProcess">
                    <TextEdit ReadOnly="!_editing" @bind-Text="@_model.Process">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid process."]</ValidationError>
                        </Feedback>
                    </TextEdit>
                </Validation>
            }
        </Field>
    }
    @if (_editing || _model.ProcessNm.HasValue)
    {
        <Field>
            <FieldLabel>@L["Process (nm)"]</FieldLabel>
            @if (_editing)
            {
                <Check TValue="bool" @bind-Checked="@_unknownProcessNm">@L["Unknown (process size)"]</Check>
            }
            @if (!_editing ||
                 !_unknownProcessNm)
            {
                <Validation Validator="@ValidateFloatBiggerThanOne">
                    <NumericEdit Disabled="!_editing" TValue="float?" Decimals="2" @bind-Value="@_model.ProcessNm">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid process size in nanometers."]</ValidationError>
                        </Feedback>
                    </NumericEdit>
                </Validation>
            }
        </Field>
    }
    @if (_editing || _model.DieSize.HasValue)
    {
        <Field>
            <FieldLabel>@L["Die size (mm²)"]</FieldLabel>
            @if (_editing)
            {
                <Check TValue="bool" @bind-Checked="@_unknownDieSize">@L["Unknown (die size)"]</Check>
            }
            @if (!_editing ||
                 !_unknownDieSize)
            {
                <Validation Validator="@ValidateFloatBiggerThanOne">
                    <NumericEdit Disabled="!_editing" TValue="float?" Decimals="2" @bind-Value="@_model.DieSize">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid die size in square millimeters."]</ValidationError>
                        </Feedback>
                    </NumericEdit>
                </Validation>
            }
        </Field>
    }
    @if (_editing || _model.Transistors.HasValue)
    {
        <Field>
            <FieldLabel>@L["Transistors"]</FieldLabel>
            @if (_editing)
            {
                <Check TValue="bool" @bind-Checked="@_unknownTransistors">@L["Unknown (transistors)"]</Check>
            }
            @if (!_editing ||
                 !_unknownTransistors)
            {
                <Validation Validator="@ValidateLongBiggerThanZero">
                    <NumericEdit Disabled="!_editing" TValue="long?" Decimals="0" @bind-Value="@_model.Transistors">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid number of transistors."]</ValidationError>
                        </Feedback>
                    </NumericEdit>
                </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/gpus" class="btn btn-secondary">@L["Back to list"]</a>
</div>
@if (!_editing)
{
    <hr />
    <h3>@L["Resolutions supported by this graphical processing unit"]</h3>
    <Button Color="Color.Success" Clicked="OnAddResolutionClick" Disabled="_addingResolution">@L["Add new (resolution by gpu)"]</Button>
    @if (_addingResolution)
    {
        <div>
            <Field>
                <FieldLabel>@L["Resolutions"]</FieldLabel>
                <Select Disabled="_savingResolution" TValue="int?" @bind-SelectedValue="@_addingResolutionId">
                    @foreach (var resolution in _resolutions)
                    {
                        @if (_gpuResolutions.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 (_gpuResolutions?.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 _gpuResolutions)
                {
                    <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>
}