@page "/setting/system-user"
@using Indotalent.Features.Setting.SystemUser
@using Indotalent.Features.Setting.SystemUser.Cqrs
@using MudBlazor

@if (_currentView == ViewMode.Create)
{
    <_SystemUserCreateForm OnCancel="BackToTable" OnSuccess="HandleSuccess" />
}
else if (_currentView == ViewMode.Update || _currentView == ViewMode.View)
{
    <_SystemUserUpdateForm Data="_selectedData!" ReadOnly="@(_currentView == ViewMode.View)" OnCancel="BackToTable" OnSuccess="HandleSuccess" />
}
else if (_currentView == ViewMode.ChangePassword)
{
    <_SystemUserChangePasswordForm Data="_selectedData!" OnCancel="BackToTable" OnSuccess="HandleSuccess" />
}
else if (_currentView == ViewMode.ChangeRole)
{
    <_SystemUserChangeRoleForm Data="_selectedData!" OnCancel="BackToTable" OnSuccess="HandleSuccess" />
}
else if (_currentView == ViewMode.ChangeAvatar)
{
    <_SystemUserChangeAvatarForm Data="_selectedData!" OnCancel="BackToTable" OnSuccess="HandleSuccess" />
}
else
{
    <_SystemUserDataTable OnAdd="() => ShowCreate()"
                          OnEdit="(item) => ShowUpdate(item, false)"
                          OnView="(item) => ShowUpdate(item, true)"
                          OnChangePassword="(item) => ShowChangePassword(item)"
                          OnManageRoles="(item) => ShowChangeRole(item)"
                          OnChangeAvatar="(item) => ShowChangeAvatar(item)" />
}

@code {
    private enum ViewMode { Table, Create, Update, View, ChangePassword, ChangeRole, ChangeAvatar }
    private ViewMode _currentView = ViewMode.Table;
    private UpdateSystemUserRequest? _selectedData;

    private void ShowCreate() => _currentView = ViewMode.Create;
    private void ShowUpdate(UpdateSystemUserRequest data, bool isReadOnly)
    {
        _selectedData = data;
        _currentView = isReadOnly ? ViewMode.View : ViewMode.Update;
    }
    private void ShowChangePassword(UpdateSystemUserRequest data)
    {
        _selectedData = data;
        _currentView = ViewMode.ChangePassword;
    }
    private void ShowChangeRole(UpdateSystemUserRequest data)
    {
        _selectedData = data;
        _currentView = ViewMode.ChangeRole;
    }
    private void ShowChangeAvatar(UpdateSystemUserRequest data)
    {
        _selectedData = data;
        _currentView = ViewMode.ChangeAvatar;
    }

    private void BackToTable()
    {
        _currentView = ViewMode.Table;
        _selectedData = null;
    }

    private void HandleSuccess()
    {
        _currentView = ViewMode.Table;
        _selectedData = null;
    }
}