package com.sdl.bootstrap.button;

import com.sdl.selenium.web.WebDriverConfig;
import com.sdl.selenium.web.WebLocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions;

public class UploadFile extends WebLocator implements Upload {
    private static final Logger LOGGER = LoggerFactory.getLogger(UploadFile.class);

    public UploadFile() {
        setClassName("UploadFile");
        setClasses("fileupload");
        setTag("div");
    }

    /**
     * @param container parent
     */
    public UploadFile(WebLocator container) {
        this();
        setContainer(container);
    }

    public UploadFile(WebLocator container, String label) {
        this(container);
        setLabel(label);
    }

    /**
     * Upload file with AutoIT.
     * Use only this: button.upload("Browse", "C:\\upload.exe", "C:\\text.txt");
     *
     * @param text     button text
     * @param filePath e.g. "C:\\upload.exe", "C:\\text.txt"
     */
    public boolean upload(String text, String... filePath) {
        WebLocator upload = new WebLocator(this).setTag("span").setClasses("fileupload-new").setText(text);
        return upload(upload, filePath);
    }

    public void change(String... filePath) {
        change("Change", filePath);
    }

    public boolean change(String text, String... filePath) {
        WebLocator upload = new WebLocator(this).setTag("span").setClasses("fileupload-exists").setText(text);
        return upload(upload, filePath);
    }

    /**
     * Upload file with AutoIT.
     * Use only this: button.upload("C:\\upload.exe", "C:\\text.txt");
     *
     * @param filePath new String[] {"C:\\upload.exe", "C:\\text.txt"}
     */
    @Override
    public boolean upload(String... filePath) {
        WebLocator uploadButton = new WebLocator(this).setTag("span").setClasses("fileupload-new").setElPathSuffix("icon-folder-open", "count(.//i[@class='icon-folder-open']) > 0");
        return upload(uploadButton, filePath);
    }

    public boolean reUpload(String... filePath) {
        WebLocator uploadButton = new WebLocator(this).setTag("span").setClasses("fileupload-exists").setElPathSuffix("icon-refresh", "count(.//i[@class='icon-refresh']) > 0");
        return upload(uploadButton, filePath);
    }

    public boolean removeFile() {
        WebLocator removeButton = new WebLocator(this).setTag("a").setClasses("fileupload-exists").setElPathSuffix("icon-trash", "count(.//i[@class='icon-trash']) > 0");
        return removeButton.clickAt();
    }

    public String uploadedNameFile() {
        WebLocator upload = new WebLocator(this).setTag("span").setClasses("fileupload-preview");
        return upload.getHtmlText();
    }

    public boolean upload(WebLocator el, String... filePath) {
        return browse(el) && RunExe.getInstance().upload(filePath);
    }

    private boolean browse(WebLocator el) {
        try {
            WebDriver driver = WebDriverConfig.getDriver();
            el.focus();
            Actions builder = new Actions(driver);
            builder.moveToElement(el.currentElement).perform();
            builder.click().perform();
            return true;
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return false;
        }
    }
}
@@ -267,16 +267,852 @@
 ons;%0D%0A%0D%0A
+/**%0D%0A * %3Cp%3E%3Cb%3E%3Ci%3EUsed for finding element process (to generate xpath address)%3C/i%3E%3Cb%3E%3C/p%3E%0D%0A * %3Cp%3EExample:%3C/p%3E%0D%0A * %3Cpre%3E%7B@code%0D%0A * %3Cdiv%3E%0D%0A *  %3Cdiv class=%22fileupload fileupload-new%22 data-provides=%22fileupload%22%3E%0D%0A *      %3Cspan class=%22btn btn-file%22%3E%3Cspan class=%22fileupload-new%22%3EBrowse%3C/span%3E%3Cspanclass=%22fileupload-exists%22%3EChange%3C/span%3E%3Cinput type=%22file%22/%3E%3C/span%3E%0D%0A *      %3Cspan class=%22fileupload-preview%22%3E%3C/span%3E%0D%0A *      %3Ca href=%22#%22 class=%22close fileupload-exists%22 data-dismiss=%22fileupload%22 style=%22float: none%22%3E%C3%97%3C/a%3E%0D%0A *  %3C/div%3E%0D%0A * %3C/div%3E%0D%0A * %7D%3C/pre%3E%0D%0A * %3Cp%3EIn Java write this:%3C/p%3E%0D%0A * %3Cpre%3E%7B@code%0D%0A * private UploadButton uploadButton = new UploadButton().setText(%22Browse%22);%0D%0A * uploadButton.upload(InputData.RESOURCES_DIRECTORY_PATH + %22%5C%5Cupload%5C%5Cupload.exe%22, InputData.RESOURCES_DIRECTORY_PATH + %22%5C%5Cupload%5C%5Ctext.docx%22);%0D%0A * %7D%3C/pre%3E%0D%0A */%0D%0A
 public c

add documentation UploadFile