@{
    /******************************************************************************
// 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-2026 Natalia Portillo
*******************************************************************************/
}

@page "/admin/machines/details/{Id:int}"
@page "/admin/machines/edit/{Id:int}"
@page "/admin/machines/create"
@using Marechai.Data
@using Marechai.Data.Dtos
@using Marechai.Database.Models
@inherits OwningComponentBase<MachinesService>
@inject IStringLocalizer<MachinesService> L
@inject CompaniesService                  CompaniesService
@inject MachineFamiliesService            MachineFamiliesService
@inject NavigationManager                 NavigationManager
@inject GpusByMachineService              GpusByMachineService
@inject GpusService                       GpusService
@inject SoundSynthsByMachineService       SoundSynthsByMachineService
@inject SoundSynthsService                SoundSynthsService
@inject ProcessorsByMachineService        ProcessorsByMachineService
@inject ProcessorsService                 ProcessorsService
@inject MemoriesByMachineService          MemoriesByMachineService
@inject StorageByMachineService           StorageByMachineService
@inject ScreensByMachineService           ScreensByMachineService
@inject ScreensService                    ScreensService
@inject MachinePhotosService              MachinePhotosService
@inject UserManager<ApplicationUser>      UserManager
@inject AuthenticationStateProvider       AuthenticationStateProvider
@attribute [Authorize(Roles = "UberAdmin, Admin")]
<h3>@L["Machine details"]</h3>
<hr/>
@if(!_loaded)
{
    <p align="center">@L["Loading..."]</p>

    return;
}
<div>
    <Field>
        <FieldLabel>@L["Company"]</FieldLabel>
        <Select @bind-SelectedValue="@_model.CompanyId" Disabled="!_editing" @TValue="int">
            @foreach(CompanyDto company in _companies)
            {
                <SelectItem @TValue="int" Value="@company.Id">@company.Name</SelectItem>
            }
        </Select>
    </Field>
    <Field>
        <FieldLabel>@L["Name"]</FieldLabel>
        <Validation Validator="@ValidateName">
            <TextEdit @bind-Text="@_model.Name" Disabled="!_editing">
                <Feedback>
                    <ValidationError>@L["Please enter a valid name."]</ValidationError>
                </Feedback>
            </TextEdit>
        </Validation>
    </Field>
    <Field>
        <FieldLabel>@L["Type"]</FieldLabel>
        <Select @bind-SelectedValue="@Type" Disabled="!_editing" @TValue="int">
            @foreach(int type in Enum.GetValues(typeof(MachineType)))
            {
                <SelectItem @TValue="int" Value="@type">@(((MachineType)type).ToString())</SelectItem>
            }
        </Select>
    </Field>
    @if(_editing || _model.Model != null)
    {
        <Field>
            <FieldLabel>@L["Model code"]</FieldLabel>
            @if(_editing)
            {
                <Check @bind-Checked="@_unknownModel" @TValue="bool">@L["Unknown (model)"]</Check>
            }
            @if(!_editing || !_unknownModel)
            {
                <Validation Validator="@ValidateModel">
                    <TextEdit @bind-Text="@_model.Model" Disabled="!_editing">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid model."]</ValidationError>
                        </Feedback>
                    </TextEdit>
                </Validation>
            }
        </Field>
    }
    @if(_editing || _model.Introduced.HasValue)
    {
        <Field>
            <FieldLabel>@L["Introduced (public release)"]</FieldLabel>
            @if(_editing)
            {
                <Check @bind-Checked="@_unknownIntroduced" Disabled="_prototype"
                       @TValue="bool">@L["Unknown (introduction date)"]</Check>

                <Check @bind-Checked="@_prototype" Disabled="_unknownIntroduced" @TValue="bool">@L["Prototype"]</Check>
            }
            @if(!_editing || !_prototype && !_unknownIntroduced)
            {
                <Validation Validator="@ValidateIntroduced">
                    <DateEdit @bind-Date="@_model.Introduced" Disabled="!_editing" @TValue="DateTime?">
                        <Feedback>
                            <ValidationError>@L["Please enter an introduction date."]</ValidationError>
                        </Feedback>
                    </DateEdit>
                </Validation>
            }
        </Field>
    }
    @if(_editing || _model.FamilyId != null)
    {
        <Field>
            <FieldLabel>@L["Family"]</FieldLabel>
            @if(_editing)
            {
                <Check @bind-Checked="@_noFamily" @TValue="bool">@L["No family"]</Check>
            }
            @if(!_editing || !_noFamily)
            {
                <Select @bind-SelectedValue="@_model.FamilyId" Disabled="!_editing" @TValue="int?">
                    @foreach(MachineFamilyDto family in _families)
                    {
                        <SelectItem @TValue="int?" Value="@family.Id">@family.Name</SelectItem>
                    }
                </Select>
            }
        </Field>
    }
</div>
<div>
    @if(!_editing)
    {
        <Button Clicked="@OnEditClicked" Color="Color.Primary">@L["Edit"]</Button>
    }
    else
    {
        <Button Clicked="@OnSaveClicked" Color="Color.Success">@L["Save"]</Button>

        <Button Clicked="@OnCancelClicked" Color="Color.Danger">@L["Cancel"]</Button>
    }
    <a class="btn btn-secondary" href="/admin/machines">@L["Back to list"]</a>
</div>
@if(!_editing)
{
    <hr/>
    <h3>@L["Graphical processing units belonging to this machine"]</h3>

    <Button Clicked="OnAddGpuClick" Color="Color.Success" Disabled="_addingGpu">@L["Add new (gpu by machine)"]</Button>

    @if(_addingGpu)
    {
        <div>
            <Field>
                <FieldLabel>@L["Graphical processing units"]</FieldLabel>
                <Select @bind-SelectedValue="@_addingGpuId" Disabled="_savingGpu" @TValue="int?">
                    @foreach(GpuDto gpu in _gpus)
                    {
                        <SelectItem @TValue="int?" Value="@gpu.Id">@gpu.Company - @gpu.Name</SelectItem>
                    }
                </Select>
                <Button Clicked="@CancelAddGpu" Color="Color.Primary" Disabled="@_savingGpu">@L["Cancel"]</Button>
                <Button Clicked="@ConfirmAddGpu" Color="Color.Success" Disabled="@_savingGpu">@L["Add"]</Button>
            </Field>
        </div>
    }

    @if(_machineGpus?.Count > 0)
    {
        <div>
            <table class="table table-striped">
                <thead>
                <tr>
                    <th>
                        @L["Company"]
                    </th>
                    <th>
                        @L["Name"]
                    </th>
                    <th></th>
                </tr>
                </thead>
                <tbody>
                @foreach(GpuByMachineDto item in _machineGpus)
                {
                    <tr>
                        <td>
                            @item.CompanyName
                        </td>
                        <td>
                            @item.Name
                        </td>
                        <td>
                            <Button Clicked="() => {ShowGpuDeleteModal(item.Id);}" Color="Color.Danger"
                                    Disabled="@_addingGpu">@L["Delete"]</Button>
                        </td>
                    </tr>
                }
                </tbody>
            </table>
        </div>
    }

    <hr/>
    <h3>@L["Sound synthesizers belonging to this machine"]</h3>

    <Button Clicked="OnAddSoundClick" Color="Color.Success"
            Disabled="_addingSound">@L["Add new (sound by machine)"]</Button>

    @if(_addingSound)
    {
        <div>
            <Field>
                <FieldLabel>@L["Sound synthesizers"]</FieldLabel>
                <Select @bind-SelectedValue="@_addingSoundId" Disabled="_savingSound" @TValue="int?">
                    @foreach(SoundSynthDto soundSynth in _soundSynths)
                    {
                        <SelectItem @TValue="int?"
                                    Value="@soundSynth.Id">@soundSynth.CompanyName - @soundSynth.Name</SelectItem>
                    }
                </Select>
                <Button Clicked="@CancelAddSound" Color="Color.Primary" Disabled="@_savingSound">@L["Cancel"]</Button>
                <Button Clicked="@ConfirmAddSound" Color="Color.Success" Disabled="@_savingSound">@L["Add"]</Button>
            </Field>
        </div>
    }

    @if(_machineSound?.Count > 0)
    {
        <div>
            <table class="table table-striped">
                <thead>
                <tr>
                    <th>
                        @L["Company"]
                    </th>
                    <th>
                        @L["Name"]
                    </th>
                    <th></th>
                </tr>
                </thead>
                <tbody>
                @foreach(SoundSynthByMachineDto item in _machineSound)
                {
                    <tr>
                        <td>
                            @item.CompanyName
                        </td>
                        <td>
                            @item.Name
                        </td>
                        <td>
                            <Button Clicked="() => {ShowSoundDeleteModal(item.Id);}" Color="Color.Danger"
                                    Disabled="@_addingSound">@L["Delete"]</Button>
                        </td>
                    </tr>
                }
                </tbody>
            </table>
        </div>
    }

    <hr/>
    <h3>@L["Processors belonging to this machine"]</h3>

    <Button Clicked="OnAddCpuClick" Color="Color.Success"
            Disabled="_addingCpu">@L["Add new (processor by machine)"]</Button>

    @if(_addingCpu)
    {
        <div>
            <Field>
                <FieldLabel>@L["Processors"]</FieldLabel>
                <Select @bind-SelectedValue="@_addingCpuId" Disabled="_savingCpu" @TValue="int?">
                    @foreach(ProcessorDto cpu in _cpus)
                    {
                        <SelectItem @TValue="int?" Value="@cpu.Id">@cpu.CompanyName - @cpu.Name</SelectItem>
                    }
                </Select>
            </Field>
            <Field>
                <FieldLabel>@L["Nominal speed (MHz)"]</FieldLabel>
                <Check @bind-Checked="@_unknownProcessorSpeed"
                       @TValue="bool">@L["Unknown (processor by machine speed)"]</Check>
                @if(!_unknownProcessorSpeed)
                {
                    <Validation Validator="@ValidateProcessorSpeed">
                        <NumericEdit @bind-Value="@_addingProcessorSpeed" Decimals="3" @TValue="float?">
                            <Feedback>
                                <ValidationError>@L["Please enter a valid speed for this processor."]</ValidationError>
                            </Feedback>
                        </NumericEdit>
                    </Validation>
                }
            </Field>
            <Button Clicked="@CancelAddCpu" Color="Color.Primary" Disabled="@_savingCpu">@L["Cancel"]</Button>
            <Button Clicked="@ConfirmAddCpu" Color="Color.Success" Disabled="@_savingCpu">@L["Add"]</Button>
        </div>
    }

    @if(_machineCpus?.Count > 0)
    {
        <div>
            <table class="table table-striped">
                <thead>
                <tr>
                    <th>
                        @L["Company"]
                    </th>
                    <th>
                        @L["Name"]
                    </th>
                    <th>
                        @L["Speed"]
                    </th>
                    <th></th>
                </tr>
                </thead>
                <tbody>
                @foreach(ProcessorByMachineDto item in _machineCpus)
                {
                    <tr>
                        <td>
                            @item.CompanyName
                        </td>
                        <td>
                            @item.Name
                        </td>
                        <td>
                            @string.Format(L["{0:F3} MHz"], item.Speed)
                        </td>
                        <td>
                            <Button Clicked="() => {ShowCpuDeleteModal(item.Id);}" Color="Color.Danger"
                                    Disabled="@_addingCpu">@L["Delete"]</Button>
                        </td>
                    </tr>
                }
                </tbody>
            </table>
        </div>
    }

    <hr/>
    <h3>@L["Memory belonging to this machine"]</h3>

    <Button Clicked="OnAddMemoryClick" Color="Color.Success"
            Disabled="_addingMemory">@L["Add new (memory by machine)"]</Button>

    @if(_addingMemory)
    {
        <div>
            <Field>
                <FieldLabel>@L["Memory type"]</FieldLabel>
                <Select @bind-SelectedValue="@_addingMemoryType" @TValue="int">
                    @foreach(int type in Enum.GetValues(typeof(MemoryType)))
                    {
                        <SelectItem @TValue="int" Value="@type">@(((MemoryType)type).ToString())</SelectItem>
                    }
                </Select>
            </Field>
            <Field>
                <FieldLabel>@L["Memory usage"]</FieldLabel>
                <Select @bind-SelectedValue="@_addingMemoryUsage" @TValue="int">
                    @foreach(int usage in Enum.GetValues(typeof(MemoryUsage)))
                    {
                        <SelectItem @TValue="int" Value="@usage">@(((MemoryUsage)usage).ToString())</SelectItem>
                    }
                </Select>
            </Field>
            <Field>
                <FieldLabel>@L["Nominal speed (Hz)"]</FieldLabel>
                <Check @bind-Checked="@_unknownMemorySpeed"
                       @TValue="bool">@L["Unknown (memory by machine speed)"]</Check>
                @if(!_unknownMemorySpeed)
                {
                    <Validation Validator="@ValidateMemorySpeed">
                        <NumericEdit @bind-Value="@_addingMemorySpeed" Decimals="3" @TValue="double?">
                            <Feedback>
                                <ValidationError>@L["Please enter a valid speed for this memory."]</ValidationError>
                            </Feedback>
                        </NumericEdit>
                    </Validation>
                }
            </Field>
            <Field>
                <FieldLabel>@L["Memory size (bytes)"]</FieldLabel>
                <Check @bind-Checked="@_unknownMemorySize" @TValue="bool">@L["Unknown (memory by machine size)"]</Check>
                @if(!_unknownMemorySize)
                {
                    <Validation Validator="@ValidateMemorySize">
                        <NumericEdit @bind-Value="@_addingMemorySize" Decimals="0" @TValue="long?">
                            <Feedback>
                                <ValidationError>@L["Please enter a valid size for this memory."]</ValidationError>
                            </Feedback>
                        </NumericEdit>
                    </Validation>
                }
            </Field>
            <Button Clicked="@CancelAddMemory" Color="Color.Primary" Disabled="@_savingMemory">@L["Cancel"]</Button>
            <Button Clicked="@ConfirmAddMemory" Color="Color.Success" Disabled="@_savingMemory">@L["Add"]</Button>
        </div>
    }

    @if(_machineMemories?.Count > 0)
    {
        <div>
            <table class="table table-striped">
                <thead>
                <tr>
                    <th>
                        @L["Type"]
                    </th>
                    <th>
                        @L["Usage"]
                    </th>
                    <th>
                        @L["Size"]
                    </th>
                    <th>
                        @L["Speed"]
                    </th>
                    <th></th>
                </tr>
                </thead>
                <tbody>
                @foreach(MemoryByMachineDto item in _machineMemories)
                {
                    <tr>
                        <td>
                            @item.Type
                        </td>
                        <td>
                            @item.Usage
                        </td>
                        <td>
                            @if(item.Size.HasValue)
                            {
                                @string.Format(L["{0} bytes"], item.Size)
                            }
                            else
                            {
                                @L["Unknown (memory by machine size)"]
                            }
                        </td>
                        <td>
                            @if(item.Speed.HasValue)
                            {
                                @string.Format(L["{0:F3} Hz"], item.Speed)
                            }
                            else
                            {
                                @L["Unknown (memory by machine speed)"]
                            }
                        </td>
                        <td>
                            <Button Clicked="() => {ShowMemoryDeleteModal(item.Id);}" Color="Color.Danger"
                                    Disabled="@_addingMemory">@L["Delete"]</Button>
                        </td>
                    </tr>
                }
                </tbody>
            </table>
        </div>
    }

    <hr/>
    <h3>@L["Storage belonging to this machine"]</h3>

    <Button Clicked="OnAddStorageClick" Color="Color.Success"
            Disabled="_addingStorage">@L["Add new (storage by machine)"]</Button>

    @if(_addingStorage)
    {
        <div>
            <Field>
                <FieldLabel>@L["Storage type"]</FieldLabel>
                <Select @bind-SelectedValue="@_addingStorageType" @TValue="int">
                    @foreach(int type in Enum.GetValues(typeof(StorageType)))
                    {
                        <SelectItem @TValue="int" Value="@type">@(((StorageType)type).ToString())</SelectItem>
                    }
                </Select>
            </Field>
            <Field>
                <FieldLabel>@L["Storage interface"]</FieldLabel>
                <Select @bind-SelectedValue="@_addingStorageInterface" @TValue="int">
                    @foreach(int usage in Enum.GetValues(typeof(StorageInterface)))
                    {
                        <SelectItem @TValue="int" Value="@usage">@(((StorageInterface)usage).ToString())</SelectItem>
                    }
                </Select>
            </Field>
            <Field>
                <FieldLabel>@L["Nominal capacity (bytes)"]</FieldLabel>
                <Check @bind-Checked="@_unknownStorageSize"
                       @TValue="bool">@L["Unknown or empty (storage by machine nominal capacity)"]</Check>
                @if(!_unknownStorageSize)
                {
                    <Validation Validator="@ValidateStorageSize">
                        <NumericEdit @bind-Value="@_addingStorageSize" Decimals="0" @TValue="long?">
                            <Feedback>
                                <ValidationError>@L["Please enter a valid size for this storage."]</ValidationError>
                            </Feedback>
                        </NumericEdit>
                    </Validation>
                }
            </Field>
            <Button Clicked="@CancelAddStorage" Color="Color.Primary" Disabled="@_savingStorage">@L["Cancel"]</Button>
            <Button Clicked="@ConfirmAddStorage" Color="Color.Success" Disabled="@_savingStorage">@L["Add"]</Button>
        </div>
    }

    @if(_machineStorage?.Count > 0)
    {
        <div>
            <table class="table table-striped">
                <thead>
                <tr>
                    <th>
                        @L["Type"]
                    </th>
                    <th>
                        @L["Interface"]
                    </th>
                    <th>
                        @L["Nominal capacity"]
                    </th>
                    <th></th>
                </tr>
                </thead>
                <tbody>
                @foreach(StorageByMachineDto item in _machineStorage)
                {
                    <tr>
                        <td>
                            @item.Type
                        </td>
                        <td>
                            @item.Interface
                        </td>
                        <td>
                            @if(item.Capacity.HasValue)
                            {
                                @string.Format(L["{0} bytes"], item.Capacity)
                            }
                            else
                            {
                                @L["Unknown or empty (storage by machine nominal capacity)"]
                            }
                        </td>
                        <td>
                            <Button Clicked="() => {ShowStorageDeleteModal(item.Id);}" Color="Color.Danger"
                                    Disabled="@_addingStorage">@L["Delete"]</Button>
                        </td>
                    </tr>
                }
                </tbody>
            </table>
        </div>
    }

    <hr/>
    <h3>@L["Screens attached to this machine"]</h3>

    <Button Clicked="OnAddScreenClick" Color="Color.Success"
            Disabled="_addingScreen">@L["Add new (screen by machine)"]</Button>

    @if(_addingScreen)
    {
        <div>
            <Field>
                <FieldLabel>@L["Screens"]</FieldLabel>
                <Select @bind-SelectedValue="@_addingScreenId" Disabled="_savingScreen" @TValue="int?">
                    @foreach(ScreenDto screen in _screens)
                    {
                        if(_machineScreens.All(s => s.ScreenId != screen.Id))
                        {
                            <SelectItem @TValue="int?"
                                        Value="@screen.Id">@string.Format(L["{0}\" with a native resolution of {1}"], screen.Diagonal, screen.NativeResolution)</SelectItem>
                        }
                    }
                </Select>
                <Button Clicked="@CancelAddScreen" Color="Color.Primary" Disabled="@_savingScreen">@L["Cancel"]</Button>
                <Button Clicked="@ConfirmAddScreen" Color="Color.Success" Disabled="@_savingScreen">@L["Add"]</Button>
            </Field>
        </div>
    }

    @if(_machineScreens?.Count > 0)
    {
        <div>
            <table class="table table-striped">
                <thead>
                <tr>
                    <th>
                        @L["Diagonal"]
                    </th>
                    <th>
                        @L["Width"]
                    </th>
                    <th>
                        @L["Height"]
                    </th>
                    <th>
                        @L["Type"]
                    </th>
                    <th>
                        @L["Effective colors"]
                    </th>
                    <th>
                        @L["Native resolution"]
                    </th>
                    <th></th>
                </tr>
                </thead>
                <tbody>
                @foreach(ScreenByMachineDto item in _machineScreens)
                {
                    <tr>
                        <td>
                            @item.Screen.Diagonal
                        </td>
                        <td>
                            @item.Screen.Width
                        </td>
                        <td>
                            @item.Screen.Height
                        </td>
                        <td>
                            @item.Screen.Type
                        </td>
                        <td>
                            @item.Screen.EffectiveColors
                        </td>
                        <td>
                            @item.Screen.NativeResolution
                        </td>
                        <td>
                            <Button Clicked="() => {ShowScreenDeleteModal(item.Id);}" Color="Color.Danger"
                                    Disabled="@_addingScreen">@L["Delete"]</Button>
                        </td>
                    </tr>
                }
                </tbody>
            </table>
        </div>
    }

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

    @if(_photos.Count > 0)
    {
        foreach(Guid photo in _photos)
        {
            <div class="col-md-2">
                <figure class="figure">
                    <picture>
                        <source
                            srcset="/assets/photos/machines/thumbs/avif/hd/@(photo).avif, /assets/photos/machines/thumbs/avif/1440p/@(photo).avif 2x, /assets/photos/machines/thumbs/avif/4k/@(photo).avif 3x"
                            type="image/avif">
                        <source
                            srcset="/assets/photos/machines/thumbs/heif/hd/@(photo).heic, /assets/photos/machines/thumbs/heif/1440p/@(photo).heic 2x, /assets/photos/machines/thumbs/heif/4k/@(photo).heic 3x"
                            type="image/heic">
                        <source
                            srcset="/assets/photos/machines/thumbs/webp/hd/@(photo).webp, /assets/photos/machines/thumbs/webp/1440p/@(photo).webp 2x, /assets/photos/machines/thumbs/webp/4k/@(photo).webp 3x"
                            type="image/webp">
                        <source
                            srcset="/assets/photos/machines/thumbs/jp2k/hd/@(photo).jp2, /assets/photos/machines/thumbs/jp2k/1440p/@(photo).jp2 2x, /assets/photos/machines/thumbs/jp2k/4k/@(photo).jp2 3x"
                            type="image/jp2">
                        <img alt="" class="figure-img img-fluid rounded" height="auto"
                             src="/assets/photos/machines/thumbs/jpeg/hd/@(photo).jpg"
                             srcset="/assets/photos/machines/thumbs/jpeg/hd/@(photo).jpg, /assets/photos/machines/thumbs/jpeg/1440p/@(photo).jpg 2x, /assets/photos/machines/thumbs/jpeg/4k/@(photo).jpg 3x"
                             style="max-height: 256px; max-width: 256px" width="auto"/>
                    </picture>
                    <figcaption class="figure-caption">
                        <a href="/admin/machines/photo/details/@photo" target="_blank">@L["Details"]</a>
                    </figcaption>
                </figure>
            </div>
        }
    }
}