@page "/permissions"

@inject IPermissionService PermissionService

@inject IMyDialogService MyDialogService

@inherits BasePage
@using Services.Website
@implements IDisposable


<LayoutMediumContentComponent>
    <PaperComponent>
        <FormLayoutComponent>
            <FormToggleComponent
                Label="Storage Enabled"
                Info="Is storage enabled?"
                Value="_storageEnabled"
                OnChange="StoragePermissionChanged"/>

            <FormToggleComponent
                Label="Data Collection Enabled"
                Info="Is data collection enabled?"
                Value="_dataCollectionEnabled"
                OnChange="DataCollectionPermissionChanged"/>
        </FormLayoutComponent>
    </PaperComponent>

    <ContentDividerComponent/>

    <PaperComponent>
        <InfoBodyComponent>
            <InfoQuestionComponent>What's this page?</InfoQuestionComponent>
            <InfoAnswerComponent>This page has options to enable and disable certain permissions settings. Such as
                Storage and Data Collection.
            </InfoAnswerComponent>
        </InfoBodyComponent>

        <InfoBodyComponent>
            <InfoQuestionComponent>What does this website store?</InfoQuestionComponent>
            <InfoAnswerComponent>This website usages storage to track user defined default on the Storage page.
            </InfoAnswerComponent>
        </InfoBodyComponent>

        <InfoBodyComponent>
            <InfoQuestionComponent>Why would I enable storage?</InfoQuestionComponent>
            <InfoAnswerComponent>Enable storage if you want to website to remeber past settings whenever you visit the
                website on the same web browser.
            </InfoAnswerComponent>
        </InfoBodyComponent>

        <InfoBodyComponent>
            <InfoQuestionComponent>What data does this website collect?</InfoQuestionComponent>
            <InfoAnswerComponent>
                This website usages Google Analytics to collect data on usage of this website.
                <br/><br/>
                Items include: if people use keyboard or mouse in build calculator, what pages people visit, and other
                usages.
            </InfoAnswerComponent>
        </InfoBodyComponent>

        <InfoBodyComponent>
            <InfoQuestionComponent>Why would I enable data collection?</InfoQuestionComponent>
            <InfoAnswerComponent>Enable data tracking if you want the website maintainer to know how your using the
                website.
            </InfoAnswerComponent>
        </InfoBodyComponent>
    </PaperComponent>
</LayoutMediumContentComponent>


@code {
    private bool _storageEnabled;
    private bool _dataCollectionEnabled;

    protected override void OnInitialized()
    {
        PermissionService.Subscribe(Update);

        Update();
    }

    void Update()
    {
        _storageEnabled = PermissionService.GetIsStorageEnabled();
        _dataCollectionEnabled = PermissionService.GetIsDataCollectionEnabled();
        StateHasChanged();
    }

    void IDisposable.Dispose()
    {
        PermissionService.Unsubscribe(Update);
    }

    private void StoragePermissionChanged(ChangeEventArgs obj)
    {
        PermissionService.SetIsStorageEnabled(!PermissionService.GetIsStorageEnabled());
    }


    private void DataCollectionPermissionChanged(ChangeEventArgs obj)
    {
        void OnDataCollectionConfirmClicked(MouseEventArgs mouseEventArgs)
        {
            PermissionService.SetIsDataCollectionEnabled(!PermissionService.GetIsDataCollectionEnabled());
            MyDialogService.Hide();
        }

        void OnDataCollectionCancelClicked(MouseEventArgs mouseEventArgs)
        {
            MyDialogService.Hide();
        }

        if (_storageEnabled && !PermissionService.GetIsDataCollectionEnabled())
        {
            MyDialogService.Show(new DialogContents
            {
                Title = "Permission Request",
                Message = "Are you sure you want to enable data collection? This feature is implemented with Google Analytics, and your data will be used to gauge interests, find bugs, and optimize updates in IGP Fan Reference.",
                OnConfirm = new EventCallback<EventArgs>(this, OnDataCollectionConfirmClicked),
                OnCancel = new EventCallback<EventArgs>(this, OnDataCollectionCancelClicked),
                ConfirmButtonLabel = "Enable Data Collection"
            });
        }
        else
        {
            PermissionService.SetIsDataCollectionEnabled(!PermissionService.GetIsDataCollectionEnabled());
        }
    }

}