@layout PageLayout

@inject IStringLocalizer<Localizations> locale

@inject IKeyService keyService
@inject IImmortalSelectionService filterService
@inject IBuildOrderService buildOrderService
@inject IEconomyService economyService
@inject IToastService toastService
@inject ITimingService timingService

@page "/build-calculator"

@implements IDisposable

<LayoutLargeContentComponent>
    <WebsiteTitleComponent>Build Calculator</WebsiteTitleComponent>

    <AlertComponent Type="@SeverityType.Warning">
        <Title>Work In Progress and Not Fully Tested</Title>
        <Message>
            Currently not considering training queue times.
        </Message>
    </AlertComponent>

    <ContentDividerComponent></ContentDividerComponent>

    <div class="calculatorGrid">
        <div class="gridItem" style="grid-area: timing;">
            <ButtonComponent ButtonType="ButtonType.Secondary" OnClick="OnResetClicked">Clear Build Order</ButtonComponent>
            <PanelComponent>
                <InfoTooltipComponent InfoText="@locale["Tooltip Timing Info"]">
                    <TimingComponent></TimingComponent>
                </InfoTooltipComponent>
            </PanelComponent>
            <PanelComponent>
                <InfoTooltipComponent InfoText="@locale["Tooltip Filter Info"]">
                    <FilterComponent></FilterComponent>
                </InfoTooltipComponent>
            </PanelComponent>

            <PanelComponent>
                <InfoTooltipComponent InfoText="@locale["Tooltip Options Info"]">
                    <OptionsComponent></OptionsComponent>
                </InfoTooltipComponent>
            </PanelComponent>
        </div>


        <div class="gridItem" style="grid-area: chart;">
            <PanelComponent>
                <InfoTooltipComponent InfoText="@locale["Tooltip Chart Info"]">
                    <BuildChartComponent></BuildChartComponent>
                </InfoTooltipComponent>
            </PanelComponent>
        </div>

        <div class="gridItem" style="grid-area: view;">
            <PanelComponent>
                <InfoTooltipComponent InfoText="@locale["Tooltip Entity Info"]">
                    <EntityClickViewComponent/>
                </InfoTooltipComponent>

            </PanelComponent>
        </div>

        <div class="gridItem" style="grid-area: bank;">
            <PanelComponent>
                <InfoTooltipComponent InfoText="@locale["Tooltip Bank Info"]">
                    <BankComponent></BankComponent>
                </InfoTooltipComponent>
            </PanelComponent>
        </div>

        <div class="gridItem" style="grid-area: army;">
            <PanelComponent>
                <InfoTooltipComponent InfoText="@locale["Tooltip Army Info"]">
                    <ArmyComponent></ArmyComponent>
                </InfoTooltipComponent>
            </PanelComponent>
        </div>

        <div class="gridItem gridKeys">
            <PanelComponent>
                <InfoTooltipComponent InfoText="@locale["Tooltip Hotkey Info"]">
                    <HotkeyViewerComponent Size="80"></HotkeyViewerComponent>
                </InfoTooltipComponent>
            </PanelComponent>
        </div>

        <div class="gridItem" style="grid-area: highlights;">
            <PanelComponent>
                <InfoTooltipComponent InfoText="@locale["Tooltip Highlights Info"]">
                    <HighlightsComponent></HighlightsComponent>
                </InfoTooltipComponent>
            </PanelComponent>
        </div>

        <div class="gridItem" style="grid-area: buildorder;">
            <PanelComponent>
                <InfoTooltipComponent InfoText="@locale["Tooltip BuildOrder Info"]">
                    <BuildOrderComponent></BuildOrderComponent>
                </InfoTooltipComponent>
            </PanelComponent>
        </div>
    </div>

    <ContentDividerComponent></ContentDividerComponent>

    <PaperComponent>
        <FormLayoutComponent>
            <InfoBodyComponent>
                <InfoQuestionComponent>
                    What is this tool?
                </InfoQuestionComponent>
                <InfoAnswerComponent>
                    This is a calculator to determine build timings. Mostly so someone can quickly try out a few build orders to see if they somewhat make sense.
                </InfoAnswerComponent>
            </InfoBodyComponent>

            <InfoBodyComponent>
                <InfoQuestionComponent>
                    How does it work?
                </InfoQuestionComponent>
                <InfoAnswerComponent>
                    The tool calculates every second of game time. So if you attempt to build a <b>Legion Hall</b> as your first action, the tool will scan every second, until you get to one where the request can be made. In this case, that is interval 58.
                    <br/>
                    <br/>
                    If you then build 2 <b>Apostle of Bindings</b> a <b>Soul Foundry</b> and a 3 <b>Absolvers</b> you should see yourself roughly floating 500 alloy, with barely having any ether. Which means you could of gotten an <b>Acropolis</b> and a <b>Zentari</b> without hurting your build.
                    <br/>
                    <br/>
                    Try building <b>Apostle of Bindings</b> before the <b>Legion Hall</b> and see how that changes the timing of your 3 <b>Absolvers</b>. (Spoiler: <SpoilerTextComponent> your <b>Absolvers</b> will be built much faster, and you won't be floating so much alloy.</SpoilerTextComponent>)
                </InfoAnswerComponent>
            </InfoBodyComponent>

            <InfoBodyComponent>
                <InfoQuestionComponent>
                    What is CONTROL key for?
                </InfoQuestionComponent>
                <InfoAnswerComponent>
                    Economy and tech related upgrades for townhalls.
                </InfoAnswerComponent>
            </InfoBodyComponent>

            <InfoBodyComponent>
                <InfoQuestionComponent>
                    What is SHIFT key for?
                </InfoQuestionComponent>
                <InfoAnswerComponent>
                    Misc building related upgrades. (Omnivores)
                </InfoAnswerComponent>
            </InfoBodyComponent>

            <InfoBodyComponent>
                <InfoQuestionComponent>
                    What is 2 key for?
                </InfoQuestionComponent>
                <InfoAnswerComponent>
                    It will be for Pyre camps. Currently not implemented.
                </InfoAnswerComponent>
            </InfoBodyComponent>
        </FormLayoutComponent>
    </PaperComponent>
</LayoutLargeContentComponent>


<style>
    .gridItem {
        display: flex;
        flex-direction: column;
        gap: 8px;
    }

    .calculatorGrid {
        display: grid;
        gap: 8px;
        max-width: 90vw;
        grid-template-columns: 1fr 1fr 1fr 1fr;
        grid-template-rows: 600px 400px 450px;
        grid-template-areas:
            'timing view view view'
            'timing bank army army'
            'keys keys highlights buildorder'
            'chart chart chart chart';
    }

    .gridKeys {
        grid-area: keys;
    }

    @@media only screen and (max-width: 1025px) {
        .gridKeys {
            background-color: #282A30;
        }

        .calculatorGrid {
            grid-template-columns: 1fr;
            grid-template-rows: auto;
            grid-template-areas:
                'timing'
                'view'
                'keys'
                'bank'
                'army'
                'highlights'
                'buildorder'
                'chart';
            padding-left: 2px;
            padding-right: 2px;
        }
    }
</style>


@code {

    protected override void OnInitialized()
    {
        economyService.Calculate(buildOrderService, timingService, 0);

        keyService.Subscribe(HandleClick);
    }

    void IDisposable.Dispose()
    {
        keyService.Unsubscribe(HandleClick);
    }

    private void OnResetClicked()
    {
        toastService.AddToast(new ToastModel
        {
            SeverityType = SeverityType.Success,
            Message = "Build order has been cleared.",
            Title = "Reset"
        });

        buildOrderService.Reset();
    }


    private void HandleClick()
    {
        var hotkey = keyService.GetHotkey();

        if (hotkey == "")
        {
            return;
        }

        if (hotkey == "`")
        {
            buildOrderService.RemoveLast();
            economyService.Calculate(buildOrderService, timingService, buildOrderService.GetLastRequestInterval());
            return;
        }

        var hotkeyGroup = keyService.GetHotkeyGroup();
        var isHoldSpace = keyService.IsHoldingSpace();
        var faction = filterService.GetFactionType();
        var immortal = filterService.GetImmortalType();

        var entity = EntityModel.GetFrom(hotkey!, hotkeyGroup, isHoldSpace, faction, immortal);

        if (entity == null)
        {
            return;
        }

        if (buildOrderService.Add(entity, economyService))
        {
            economyService.Calculate(buildOrderService, timingService, buildOrderService.GetLastRequestInterval());
        }
    }

}