@page "/Quotes/Add"

@using Blazorise.Components
@using Gremlin_BlazorServer.Data.EntityClasses;
@using Gremlin_BlazorServer.Services;
@using System.Diagnostics;

@inject QuoteService quoteService
@inject ContactService contactService
@inject AccountService accountService
@inject NavigationManager navigationManager
@inject ClipboardService clipboardService

<h2>Create New Quote</h2>
<hr />

<h3>Quote Details</h3>
<Fields>
    <Field Horizontal>
        <FieldLabel>SalesRep:</FieldLabel>
        <FieldBody>@quote.SalesRep.LastName</FieldBody>
    </Field>               
    <Field Horizontal>
        <FieldLabel>QuotationDate:</FieldLabel>
        <FieldBody>@quote.QuotationDate</FieldBody>
    </Field>  
    <Field Horizontal>
        <FieldLabel>QuotationNumber:</FieldLabel>
        <TextEdit Text="@quote.QuotationNumber"></TextEdit>
    </Field>               
    <Field Horizontal>
        <FieldLabel>ValidFor:</FieldLabel>
        <TextEdit Text="@quote.ValidFor.ToString()"></TextEdit>
    </Field>               
</Fields>

<h3>Recipient</h3>
<DataGrid 
    TItem="Contact" 
    Data="@contacts" 
    SelectedRow="@selectedRecipient"
    SelectedRowChanged="@OnSelectedRowChanged"
    Bordered
    Hoverable
    Filterable
    Striped
    ShowPager
    Responsive>
    <DataGridCommandColumn />
        <DataGridColumn Field="@nameof(Contact.ContactId)" Caption="ContactId" Filterable/>
        <DataGridColumn Field="@nameof(Contact.FirstName)" Caption="FirstName" Filterable/>
        <DataGridColumn Field="@nameof(Contact.LastName)" Caption="LastName" Filterable/>
        <DataGridColumn Field="@nameof(Contact.Gender)" Caption="Gender" Filterable/>
        <DataGridColumn Field="@nameof(Contact.EMail)" Caption="EMail" Filterable/>
        <DataGridColumn Field="@nameof(Contact.SAPContactNumber)" Caption="SAPContactNumber" Filterable/>
</DataGrid>

<Fields>
    <Field ><FieldBody>@selectedRecipient.FirstName @selectedRecipient.LastName</FieldBody></Field>
    <Field ><FieldBody>@correspondingAccount.AccountName</FieldBody></Field>
    <Field ><FieldBody>@correspondingAccount.Street</FieldBody></Field>
    <Field ><FieldBody>@correspondingAccount.ZIP @correspondingAccount.City</FieldBody></Field>
</Fields>

<h3>Line Items</h3>
@if (quote.LineItems.Count != 0)
{
    <DataGrid 
    TItem="LineItem" 
    Data="@quote.LineItems"
    Bordered
    Hoverable
    Striped
    Responsive>
        <DataGridCommandColumn />
        <DataGridColumn Field="@nameof(LineItem.Amount)" Caption="Amount" />
        <DataGridColumn Field="@nameof(LineItem.ProductNumber)" Caption="ProductNumber" />
        <DataGridColumn Field="@nameof(LineItem.OptionNumber)" Caption="OptionNumber" />
        <DataGridColumn Field="@nameof(LineItem.ListPrice)" Caption="ListPrice" />
        <DataGridColumn Field="@nameof(LineItem.TotalDiscount)" Caption="TotalDiscount" />
        <DataGridColumn Field="@nameof(LineItem.Total)" Caption="Total" />
    </DataGrid>
}

<Button Color="Color.Primary" Clicked="@ImportLineItems">Import Clipboard</Button>
<Button Color="Color.Success" Clicked="@CreateQuote">Create Quote</Button>
<Button Color="Color.Danger" Clicked="@Cancel">Cancel</Button>

@code {
    IEnumerable<Contact> contacts = new List<Contact>();
    Quote quote = new();
    Contact selectedRecipient = new();
    Account correspondingAccount = new();

    protected override async Task OnInitializedAsync()
    {
        contacts = await contactService.GetAllContactsAsync();
        await OnSelectedRowChanged(contacts.FirstOrDefault() ?? new());

        Contact salesRep = await contactService.GetContactAsync("Woitschetzki");
        quote = new(salesRep, true);

        await base.OnInitializedAsync();
    }

    protected async Task OnSelectedRowChanged(Contact contact)
    {
        selectedRecipient = contact;
        quote.Recipient = contact;
        correspondingAccount = await accountService.GetAccountAsync(contact.AccountId) ?? new Account();
    }

    protected async void CreateQuote()
    {
        quote.DataModificationByUser = "Gremlin_BlazorServer";

        if (await quoteService.InsertQuoteAsync(quote))
        {
            navigationManager.NavigateTo("Quotes/Index");
        }
    }

    private async Task ImportLineItems()
    {
        try
        {
            string clipboard = await clipboardService.ReadTextAsync();

            if (clipboard != "")
            {
                quote = quoteService.ReadLineItems(quote, clipboard);
            }
        }
        catch
        {
            Debug.WriteLine("Cannot read from clipboard");
        }
    }

    void Cancel() => navigationManager.NavigateTo("Quotes/Index");
}