package com.atlassian.webdriver.bitbucket.page;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;

public class LicensePage extends BitbucketPage {
    @Override
    public String getUrl() {
        return "/admin/license";
    }
    
    public String getServerId() {
        return driver.findElement(By.id("serverid")).getText();
    }

    public String getLicenseDescription() {
        return driver.findElement(By.id("license-description")).getText();
    }
    
    public String getLicensee() {
        return driver.findElement(By.id("license-owner")).getText();
    }
    
    public String getClustering() {
        return driver.findElement(By.id("license-clustered")).getText();
    }

    public String getSEN() {
        return driver.findElement(By.id("license-sen")).getText();
    }
    
    public String getExpiry() {
        return driver.findElement(By.id("license-expiry")).getText();
    }
    
    public String getMaintenanceExpiry() {
        return driver.findElement(By.id("license-maintenance-expiry")).getText();
    }
    
    public String getUserLimit() {
        return driver.findElement(By.id("license-users")).getText();
    }
    
    public boolean isUpdated() {
        return getAuiSuccessMessages().contains("Your license has been updated.");
    }
    
    public LicenseEditPage edit() {
        driver.findElement(By.id("license-edit")).click();
        return pageBinder.bind(LicenseEditPage.class);
    }

    public String getOverLimitError() {
        return getMessage(By.cssSelector(".aui-message-error.over-limit"));
    }
    
    public String getExpiredEvalError() {
        return getMessage(By.cssSelector(".aui-message-error.eval-expired"));
    }

    public String getEvalWarning() {
        return getMessage(By.cssSelector(".aui-message-warning.eval-notexpired"));
    }
    
    public String getMaintenanceExpiredError() {
        return getMessage(By.cssSelector(".aui-message-error.commercial-expired"));
    }
    
    private String getMessage(By selector) {
        try {
            WebElement element = driver.findElement(selector);
            return element.getText();
        } catch (NoSuchElementException e) {
            return null;
        }
    }
}
