package com.atlassian.webdriver.bitbucket.page;

import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.pageobjects.elements.query.Poller;
import com.atlassian.webdriver.bitbucket.element.PermissionRow;
import com.atlassian.webdriver.bitbucket.element.PermissionTable;

public class ProjectPermissionsPage extends BaseProjectPage implements PermissionsPage {
    public enum AllUsersPerms {
        READ_WRITE,
        READ,
        NONE
    }

    @ElementBy(id = "licensed-users-permissions-readwrite")
    private PageElement allUsersReadWriteRadio;

    @ElementBy(id = "licensed-users-permissions-read")
    private PageElement allUsersReadOnlyRadio;

    @ElementBy(id = "licensed-users-permissions-none")
    private PageElement allUsersNoAccessRadio;

    @ElementBy(cssSelector = "#licensed-users-permissions .submit-spinner")
    private PageElement allUsersSubmitSpinner;

    @ElementBy(id = "public-access-allowed")
    private PageElement publicAccessCheckbox;

    @ElementBy(cssSelector = "#public-access-permission .submit-spinner")
    private PageElement publicAccessSubmitSpinner;

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

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

    public ProjectPermissionsPage(String key) {
        super(key);
    }

    @Override
    public String getUrl() {
        return String.format("/projects/%s/permissions", getProjectKey());
    }

    @Override
    public void doWait() {
        super.doWait();
        userPermissionTable.waitUntilLoaded();
        groupPermissionTable.waitUntilLoaded();
    }

    public ProjectPermissionsPage selectAllUsersPerms(AllUsersPerms perms) {
        switch (perms) {
            case READ_WRITE:
                allUsersReadWriteRadio.click();
                break;
            case READ:
                allUsersReadOnlyRadio.click();
                break;
            case NONE:
                allUsersNoAccessRadio.click();
                break;
        }
        Poller.waitUntilFalse(allUsersSubmitSpinner.timed().isVisible());
        return this;
    }

    public AllUsersPerms getPermissionsGrantedToAllLicensedUsers() {
        if (allUsersReadWriteRadio.isSelected()) {
            return AllUsersPerms.READ_WRITE;
        } else if (allUsersReadOnlyRadio.isSelected()) {
            return AllUsersPerms.READ;
        } else {
            return AllUsersPerms.NONE;
        }
    }

    public boolean isNoAccessPermissionDisabled() {
        return !allUsersNoAccessRadio.isEnabled();
    }

    public ProjectPermissionsPage setPublicAccess(boolean allowPublicAccess) {
        if (allowPublicAccess) {
            publicAccessCheckbox.select();
        } else if (publicAccessCheckbox.isSelected()) {
            publicAccessCheckbox.toggle();
        }
        Poller.waitUntilFalse(publicAccessSubmitSpinner.timed().isVisible());
        return this;
    }

    public boolean isPublicAccessSelected() {
        return publicAccessCheckbox.isSelected();
    }

    public boolean isPublicAccessAvailable() {
        return publicAccessCheckbox.isVisible();
    }

    public PermissionRow getUserItem(String user) {
        return userPermissionTable.getRow(user);
    }

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

}
