@using Bit.Admin.Enums;
@inject Bit.Admin.Services.IAccessControlService AccessControlService
@inject Bit.Core.Services.IFeatureService FeatureService
@inject Bit.Core.Settings.GlobalSettings GlobalSettings
@inject IWebHostEnvironment HostingEnvironment
@model UserEditModel
@{
    ViewData["Title"] = "User: " + Model.User.Email;

    var canViewUserInformation = AccessControlService.UserHasPermission(Permission.User_UserInformation_View);
    var canViewNewDeviceException = AccessControlService.UserHasPermission(Permission.User_NewDeviceException_Edit) &&
                                    GlobalSettings.EnableNewDeviceVerification;
    var canViewBillingInformation = AccessControlService.UserHasPermission(Permission.User_BillingInformation_View);
    var canViewGeneral = AccessControlService.UserHasPermission(Permission.User_GeneralDetails_View);
    var canViewPremium = AccessControlService.UserHasPermission(Permission.User_Premium_View);
    var canViewLicensing = AccessControlService.UserHasPermission(Permission.User_Licensing_View);
    var canViewBilling = AccessControlService.UserHasPermission(Permission.User_Billing_View);

    var canEditPremium = AccessControlService.UserHasPermission(Permission.User_Premium_Edit);
    var canEditLicensing = AccessControlService.UserHasPermission(Permission.User_Licensing_Edit);
    var canEditBilling = AccessControlService.UserHasPermission(Permission.User_Billing_Edit);
    var canLaunchGateway = AccessControlService.UserHasPermission(Permission.User_Billing_LaunchGateway);
    var canUpgradePremium = AccessControlService.UserHasPermission(Permission.User_UpgradePremium);
    var canDeleteUser = AccessControlService.UserHasPermission(Permission.User_Delete);
}

@section Scripts {
    <script>
        (() => {
            document.getElementById('upgrade-premium').addEventListener('click', () => {
                if (document.getElementById('@(nameof(Model.Premium))').checked) {
                    alert('User is already premium.');
                    return;
                }

                // Premium
                document.getElementById('@(nameof(Model.MaxStorageGb))').value = '1';
                document.getElementById('@(nameof(Model.Premium))').checked = true;
                // Licensing
                document.getElementById('@(nameof(Model.LicenseKey))').value = '@Model.RandomLicenseKey';
                document.getElementById('@(nameof(Model.PremiumExpirationDate))').value =
                    '@Model.OneYearExpirationDate';
            });

            document.getElementById('gateway-customer-link').addEventListener('click', () => {
                const gateway = document.getElementById('@(nameof(Model.Gateway))');
                const customerId = document.getElementById('@(nameof(Model.GatewayCustomerId))');
                if (!gateway || gateway.value === '' || !customerId || customerId.value === '') {
                    return;
                }

                if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Stripe)') {
                    const url = '@(HostingEnvironment.IsDevelopment()
                ? "https://dashboard.stripe.com/test"
                    : "https://dashboard.stripe.com")';
                    window.open(`${url}/customers/${customerId.value}/`, '_blank');
                } else if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Braintree)') {
                    const url = '@(HostingEnvironment.IsDevelopment()
                ? $"https://www.sandbox.braintreegateway.com/merchants/{Model.BraintreeMerchantId}"
                    : $"https://www.braintreegateway.com/merchants/{Model.BraintreeMerchantId}")';
                    window.open(`${url}/${customerId.value}`, '_blank');
                }
            });

            document.getElementById('gateway-subscription-link').addEventListener('click', () => {
                const gateway = document.getElementById('@(nameof(Model.Gateway))');
                const subId = document.getElementById('@(nameof(Model.GatewaySubscriptionId))');
                if (!gateway || gateway.value === '' || !subId || subId.value === '') {
                    return;
                }

                if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Stripe)') {
                    const url = '@(HostingEnvironment.IsDevelopment() || HostingEnvironment.IsEnvironment("QA")
                ? "https://dashboard.stripe.com/test"
                    : "https://dashboard.stripe.com")'
                    window.open(`${url}/subscriptions/${subId.value}`, '_blank');
                } else if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Braintree)') {
                    const url = '@(HostingEnvironment.IsDevelopment() || HostingEnvironment.IsEnvironment("QA")
                ? $"https://www.sandbox.braintreegateway.com/merchants/{Model.BraintreeMerchantId}"
                    : $"https://www.braintreegateway.com/merchants/{Model.BraintreeMerchantId}")';
                    window.open(`${url}/subscriptions/${subId.value}`, '_blank');
                }
            });
        })();
    </script>
}

<h1>User <small>@Model.User.Email</small></h1>

@if (canViewUserInformation)
{
    <h2>User Information</h2>
    @await Html.PartialAsync("_ViewInformation", Model.User)
}
@if (canViewNewDeviceException)
{
    <h2>New Device Verification </h2>
    <dl class="row">
        <dt class="col d-flex">
            <form asp-action="ToggleNewDeviceVerification" asp-route-id="@Model.User.Id" method="post">
                @if (Model.ActiveNewDeviceVerificationException)
                {
                    <p>Status: Bypassed</p>
                    <button type="submit" class="btn btn-success" id="new-device-verification-exception">Require New
                        Device Verification</button>
                }
                else
                {
                    <p>Status: Required</p>
                    <button type="submit" class="btn btn-outline-danger" id="new-device-verification-exception">Bypass New
                        Device Verification</button>
                }
            </form>

        </dt>
    </dl>
}
@if (canViewBillingInformation)
{
    <h2>Billing Information</h2>
    @await Html.PartialAsync("_BillingInformation",
        new BillingInformationModel
{
    BillingInfo = Model.BillingInfo,
    BillingHistoryInfo = Model.BillingHistoryInfo,
    UserId = Model.User.Id,
    Entity = "User"
})
}
@if (canViewGeneral)
{
    <h2>General</h2>
    <dl class="row">
        <dt class="col-sm-4 col-lg-3">Name</dt>
        <dd class="col-sm-8 col-lg-9">@Model.Name</dd>

        <dt class="col-sm-4 col-lg-3">Email</dt>
        <dd class="col-sm-8 col-lg-9">@Model.Email</dd>
    </dl>
    <div class="form-check mb-3">
        <input type="checkbox" class="form-check-input" asp-for="EmailVerified" disabled>
        <label class="form-check-label" asp-for="EmailVerified"></label>
    </div>
}
<form method="post" id="edit-form">
    @if (canViewPremium)
    {
        <h2>Premium</h2>
        <div class="row">
            <div class="col-sm">
                <div class="mb-3">
                    <label asp-for="MaxStorageGb" class="form-label"></label>
                    <input type="number" class="form-control" asp-for="MaxStorageGb" min="1" readonly='@(!canEditPremium)'>
                </div>
            </div>
        </div>
        <div class="form-check mb-3">
            <input type="checkbox" class="form-check-input" asp-for="Premium" readonly='@(!canUpgradePremium)'>
            <label class="form-check-label" asp-for="Premium"></label>
        </div>
    }
    @if (canViewLicensing)
    {
        <h2>Licensing</h2>
        <div class="row">
            <div class="col-sm">
                <div class="mb-3">
                    <label asp-for="LicenseKey" class="form-label"></label>
                    <input type="text" class="form-control" asp-for="LicenseKey" readonly='@(!canEditLicensing)'>
                </div>
            </div>
            <div class="col-sm">
                <div class="mb-3">
                    <label asp-for="PremiumExpirationDate" class="form-label"></label>
                    <input type="datetime-local" class="form-control" asp-for="PremiumExpirationDate"
                        readonly='@(!canEditLicensing)'>
                </div>
            </div>
        </div>
    }
    @if (canViewBilling)
    {
        <h2>Billing</h2>
        <div class="row">
            <div class="col-md">
                <div class="mb-3">
                    <label asp-for="Gateway" class="form-label"></label>
                    <select class="form-select" asp-for="Gateway" disabled='@(canEditBilling ? null : "disabled")'
                        asp-items="Html.GetEnumSelectList<Bit.Core.Enums.GatewayType>()">
                        <option value="">--</option>
                    </select>
                </div>
            </div>
            <div class="col-md">
                <div class="mb-3">
                    <label asp-for="GatewayCustomerId" class="form-label"></label>
                    <div class="input-group">
                        <input type="text" class="form-control" asp-for="GatewayCustomerId" readonly='@(!canEditBilling)'>
                        @if (canLaunchGateway)
                        {
                            <button class="btn btn-secondary" type="button" id="gateway-customer-link">
                                <i class="fa fa-external-link"></i>
                            </button>
                        }
                    </div>
                </div>
            </div>
            <div class="col-md">
                <div class="mb-3">
                    <label asp-for="GatewaySubscriptionId" class="form-label"></label>
                    <div class="input-group">
                        <input type="text" class="form-control" asp-for="GatewaySubscriptionId"
                            readonly='@(!canEditBilling)'>
                        @if (canLaunchGateway)
                        {
                            <button class="btn btn-secondary" type="button" id="gateway-subscription-link">
                                <i class="fa fa-external-link"></i>
                            </button>
                        }
                    </div>
                </div>
            </div>
        </div>
    }
</form>
<div class="d-flex mt-4">
    <button type="submit" class="btn btn-primary" form="edit-form">Save</button>
    <div class="ms-auto d-flex">
        @if (canUpgradePremium)
        {
            <button class="btn btn-secondary me-2" type="button" id="upgrade-premium">
                Upgrade Premium
            </button>
        }
        @if (canDeleteUser)
        {
            <form asp-action="Delete" asp-route-id="@Model.User.Id"
                onsubmit="return confirm('Are you sure you want to delete this user?')">
                <button class="btn btn-danger" type="submit">Delete</button>
            </form>
        }
    </div>
</div>
