@page "/Login"

@using Gremlin_BlazorServer.Data.EntityClasses;
@using Gremlin_BlazorServer.Services;

@inject LoginService loginService

<h3>Login</h3>

<Container>
    <Row>
        <Column>
            <Tabs SelectedTab="@selectedTab" SelectedTabChanged="@OnSelectedTabChanged" Pills FullWidth>
                <Items>
                    <Tab Name="login">Login</Tab>
                    <Tab Name="register">Register</Tab>
                </Items>
                <Content>
                    <TabPanel Name="login">
                        <Card>
                            <CardBody>
                                <CardTitle>
                                    Welcome Back, Please Login.
                                </CardTitle>
                            </CardBody>
                            <CardBody>
                                <Validations @ref="@loginValidationsRef" Mode="ValidationMode.Manual" ValidateOnLoad="false">
                                    <Validation Validator="ValidationRule.IsNotEmpty">
                                        <Field>
                                            <FieldLabel>User Name</FieldLabel>
                                            <TextEdit @bind-Text="@userName" Placeholder="Enter user name...">
                                                <Feedback>
                                                    <ValidationNone>Please Enter Your User Name. </ValidationNone>
                                                    <ValidationSuccess>User Name is good</ValidationSuccess>
                                                    <ValidationError>Please Enter A Valid User Name</ValidationError>
                                                </Feedback>
                                            </TextEdit>
                                        </Field>
                                    </Validation>
                                    <Validation Validator="ValidationRule.IsNotEmpty">
                                        <Field>
                                            <FieldLabel>Password</FieldLabel>
                                            <TextEdit @bind-Text="password" Role="TextRole.Password" Placeholder="Enter Password..." />
                                        </Field>
                                    </Validation>
                                    <Field>
                                        <Check TValue="bool" @bind-Checked="@rememberMe">Remember Me</Check>
                                    </Field>
                                </Validations>
                            </CardBody>
                            <CardBody>
                                <Button Color="Color.Primary" Clicked="@OnLoginClicked">Login</Button>
                                <Button Color="Color.Secondary">Forgot Password</Button>
                            </CardBody>
                        </Card>
                    </TabPanel>
                    <TabPanel Name="register">
                        <Card>
                            <CardBody>
                                <CardTitle>
                                    New Here? Create An Account
                                </CardTitle>
                            </CardBody>
                            <CardBody>
                                <Validations @ref="@registerValidationsRef" Mode="ValidationMode.Manual" ValidateOnLoad="false">
                                    <Validation Validator="ValidationRule.IsNotEmpty">
                                        <Field>
                                            <FieldLabel>Name</FieldLabel>
                                            <TextEdit @bind-Text="userName" Placeholder="Enter Your User Name" />
                                        </Field>
                                    </Validation>
                                    <Validation Validator="ValidationRule.IsNotEmpty">
                                        <Field>
                                            <FieldLabel>Password</FieldLabel>
                                            <TextEdit @bind-Text="password" Role="TextRole.Password" Placeholder="Enter Password" />
                                            <FieldHelp>Password Strength: <Text TextColor="TextColor.Danger">Strong</Text></FieldHelp>
                                        </Field>
                                    </Validation>
                                </Validations>
                            </CardBody>
                            <CardBody>
                                <Button Color="Color.Primary" Clicked="@OnRegisterClicked">
                                    Create Account
                                </Button>
                            </CardBody>
                        </Card>
                    </TabPanel>
                </Content>
            </Tabs>
        </Column>
    </Row>
</Container>

@code {
    Validations loginValidationsRef = new();
    Validations registerValidationsRef = new();

    private RegisteredUser registeredUser = new();

    private string selectedTab = "login";
    private string userName = "";
    private string password = "";
    private bool rememberMe = false;

    private Task OnSelectedTabChanged(string name)
    {
        selectedTab = name;

        return Task.CompletedTask;
    }

    private async Task OnLoginClicked()
    {
        if (await loginValidationsRef.ValidateAll())
        {
            RegisteredUser registeredUser = new()
                {
                    UserName = userName,
                    PasswordHash = password
                };

            bool IsValidated = await Task.Run(() => loginService.ValidateUserAsync(registeredUser));
            await loginValidationsRef.ClearAll();
        }
    }

    private async Task OnRegisterClicked()
    {
        if (await registerValidationsRef.ValidateAll())
        {
            registeredUser.UserName = userName;
            registeredUser.PasswordHash = password;

            _ = await Task.Run(() => loginService.InsertRegisteredUserAsync(registeredUser));

            await registerValidationsRef.ClearAll();
        }
    }
}


