@implements IDisposable;
@inject IAgileService agileService;

@layout PageLayout

@page "/agile"

@if (!agileService.IsLoaded())
{
    <LoadingComponent/>
}
else
{
    <LayoutMediumContentComponent>
        <WebsiteTitleComponent>Agile</WebsiteTitleComponent>
        <div class="agileViewContainer">
            @foreach (var sprint in agileService.AgileSprintModels!
                .OrderBy(e => e.EndDate).Reverse())
            {
                <details class="sprintDisplayContainer @sprint.GetSprintType().ToLower()"
                         open="@(sprint.GetSprintType() == SprintType.Current)">
                    <summary class="sprintSummary">
                        <div class="sprintTitle">@sprint.Name</div>
                        <div style="flex: 1; flex-grow: 1;"></div>
                        <div class="sprintDates">
                            <div class="sprintStartDate">
                                @if (sprint.StartDate != null)
                                {
                                    <b>Start: </b>
                                    @sprint.StartDate.Value.ToString("dd/MM/yyyy")
                                }
                            </div>
                            <div class="sprintEndDate">
                                @if (sprint.EndDate != null)
                                {
                                    <b>End: </b>
                                    @sprint.EndDate.Value.ToString("dd/MM/yyyy")
                                }

                            </div>
                        </div>
                    </summary>
                    <SprintComponent AgileSprint="sprint"></SprintComponent>

                </details>
            }

            <details class="sprintDisplayContainer">
                <summary class="sprintSummary">
                    <div class="sprintTitle">Backlog</div>
                    <div style="flex: 1; flex-grow: 1;"></div>
                </summary>
                <div>
                    <BacklogComponent Backlog=backlog></BacklogComponent>
                </div>
            </details>
        </div>

        <ContentDividerComponent></ContentDividerComponent>

        <PaperComponent>

            <InfoBodyComponent>
                <InfoQuestionComponent>What is Agile?</InfoQuestionComponent>
                <InfoAnswerComponent>
                    Agile is a work methodology for determing task assignment and release deadlines.
                    <br/><br/>
                    My agile practice will be creating tasks in a backlog. Assigning them to weekly sprints. And completing all tasks in the allotted time frame.
                    <br/><br/>
                    Any unfinished tasks are moved into the next sprint, or the sprint will be extended by a week.
                </InfoAnswerComponent>
            </InfoBodyComponent>
        </PaperComponent>
    </LayoutMediumContentComponent>
}


@code {
    private readonly List<AgileTaskModel> backlog = new();

    protected override void OnInitialized()
    {
        agileService.Subscribe(HasChanged);
        HasChanged();
    }

    void IDisposable.Dispose()
    {
        agileService.Unsubscribe(HasChanged);
    }

    void HasChanged()
    {
        if (!agileService.IsLoaded()) return;

        backlog.Clear();

        foreach (var task in agileService.AgileTaskModels!)
        {
            if (task.AgileSprintModelId == null)
            {
                backlog.Add(task);
            }
        }

        StateHasChanged();
    }

    protected override async Task OnInitializedAsync()
    {
        await agileService.Load();
    }

}