package com.atlassian.webdriver.bitbucket.page.admin;

import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.webdriver.bitbucket.element.PermissionRow;
import com.atlassian.webdriver.bitbucket.element.PermissionTable;
import com.atlassian.webdriver.bitbucket.page.PermissionsPage;
import com.atlassian.webdriver.bitbucket.page.BitbucketPage;

public class GlobalPermissionsPage extends BitbucketPage implements PermissionsPage {

    @ElementBy(id = "user-permissions-table")
    private PermissionTable userPermissionsTable;

    @ElementBy(id = "group-permissions-table")
    private PermissionTable groupPermissionsTable;

    @Override
    public String getUrl() {
        return "/admin/permissions";
    }

    @Override
    public void doWait() {
        super.doWait();
        // wait for the permission table to be fully populated
        userPermissionsTable.waitUntilLoaded();
        groupPermissionsTable.waitUntilLoaded();
    }

    private PermissionTable getPermissionTable(PermissionEntityType entityType) {
        return entityType == PermissionEntityType.GROUP ? groupPermissionsTable : userPermissionsTable;
    }

    public void removeItem(PermissionEntityType entityType, String objectId) {
        getPermissionTable(entityType).removeItem(objectId);
    }

    public void removeUser(String username) {
        userPermissionsTable.removeItem(username);
    }

    public void removeGroup(String name) {
        groupPermissionsTable.removeItem(name);
    }

    public PermissionRow removeItemAndExpectError(PermissionEntityType entityType, String objectId) {
        return getPermissionTable(entityType).removeItemAndExpectError(objectId);
    }

    public PermissionRow removeUserAndExpectError(String username) {
        return userPermissionsTable.removeItemAndExpectError(username);
    }

    public PermissionRow removeGroupAndExpectError(String name) {
        return groupPermissionsTable.removeItemAndExpectError(name);
    }

    public PermissionRow getRow(PermissionEntityType entityType, String objectId) {
        return getPermissionTable(entityType).getRow(objectId);
    }

    public boolean rowIsVisible(PermissionEntityType entityType, String objectId) {
        return getPermissionTable(entityType).rowIsPresent(objectId);
    }

    public GlobalPermissionsPage showAllRemoveButtons() {
        userPermissionsTable.showAllRemoveButtons();
        groupPermissionsTable.showAllRemoveButtons();
        return this;
    }

    public void loadMore(PermissionEntityType entityType) {
        PermissionTable permissionTable = getPermissionTable(entityType);
        permissionTable.getLoadMoreLink().click();
        permissionTable.waitUntilLoaded();
    }

    public PermissionRow getUserItem(String username) {
        return userPermissionsTable.getRow(username);
    }

    public PermissionRow getGroupItem(String group) {
        return groupPermissionsTable.getRow(group);
    }

    @Override
    public PermissionTable getPermissionsTable(PermissionEntityType entityType) {
        switch (entityType) {
            case USER:
                return userPermissionsTable;
            case GROUP:
                return groupPermissionsTable;
            default:
                throw new UnsupportedOperationException();
        }
    }
}
