package com.intland.codebeamer.functionaltest.batchexport;

import com.codeborne.selenide.Selenide;
import com.intland.codebeamer.functionaltest.page.LoginPage;
import com.intland.codebeamer.functionaltest.page.common.MoreMenuHelper;
import com.intland.codebeamer.functionaltest.page.mystart.MyStartPage;
import com.intland.codebeamer.functionaltest.page.trackers.ExportToOfficeDialogPage;
import com.intland.codebeamer.functionaltest.page.trackers.TrackerItemListPage;
import com.intland.codebeamer.functionaltest.page.trackers.TrackersPage;
import com.intland.codebeamer.functionaltest.page.wiki.ProjectWikiPage;
import com.intland.codebeamer.functionaltest.test.BaseTest;

import static com.codeborne.selenide.Condition.visible;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.open;

import org.openqa.selenium.By;
import org.testng.annotations.Test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;

import static com.codeborne.selenide.Selenide.switchTo;
import static com.codeborne.selenide.WebDriverRunner.getWebDriver;

public class BatchExporter extends BaseTest {

    @Test
    public void doBatchExport() {
		// Reading input file containing entities to export

		ArrayList<String>  itemAL = new ArrayList<String>();
		try {
			ClassLoader loader = Thread.currentThread().getContextClassLoader();
			InputStream is = loader.getResourceAsStream("content.csv");
			Reader reader = new InputStreamReader(is);
			BufferedReader csv = new BufferedReader(reader);
			String data;
			while ((data = csv.readLine()) != null){
				itemAL.add(data);
			}
			csv.close();
		} catch(Exception e) {
			e.printStackTrace();
		}

		// Reading codeBeamer url, username, password, project id, release name from first line of input file

		String data = itemAL.get(0);
		String[] dataArr = data.split(";");
		String codeBeamerUrl = dataArr[0];
		String userName = dataArr[1];
		String password = dataArr[2];
		String projectID = dataArr[3];
		String releaseName = dataArr[4];


		// Exporting the entities

		for (int counter = 1; counter < itemAL.size(); counter++) {
			String exportLine = itemAL.get(counter);
			dataArr = exportLine.split(";");
			String entityType = dataArr[0];
			String entityName = dataArr[1];
			String useView = dataArr[2];
			if (entityType.equals("wiki")) {
		        getWebDriver().manage().window().maximize();
		        LoginPage loginPage = open(codeBeamerUrl + "/login.spr", LoginPage.class);
		        loginPage.setUsername(userName);
		        loginPage.setPassword(password);
		        MyStartPage myStartPage = loginPage.clickOnLogin();
		        myStartPage.getMyStartMenu().shouldBe(visible);
		    	Selenide.open(codeBeamerUrl + "/project/" + projectID);
		    	ProjectWikiPage wikiPage = myStartPage.clickOnWiki();
		    	wikiPage.leftPane.openAllLeftTreeNodes();
		    	wikiPage.leftPane.leftTreeItem(entityName,1).click();
		    	wikiPage.centerPane.clickOnMoreMenuItem(MoreMenuHelper.MoreMenuItem.EXPORT_MULTIPLE_PAGES_TO_WORD);
		        switchTo().frame("inlinedPopupIframe");
		    	$(By.id("exportButton")).click();
		    	Selenide.sleep(4000);
		    	Selenide.close();
			} else if (entityType.equals("tracker")) {
		        getWebDriver().manage().window().maximize();
		        LoginPage loginPage = open(codeBeamerUrl + "/login.spr", LoginPage.class);
		        loginPage.setUsername(userName);
		        loginPage.setPassword(password);
		        MyStartPage myStartPage = loginPage.clickOnLogin();
		        myStartPage.getMyStartMenu().shouldBe(visible);
		    	Selenide.open(codeBeamerUrl + "/project/" + projectID);
		        TrackersPage trackersPage = myStartPage.clickOnTrackers();
				TrackerItemListPage itemListPage = trackersPage.clickOnTracker(entityName);
				if (useView.equals("release")) {
					$(By.id("selectedViewSelectBoxIt")).click();
					Selenide.sleep(1000);
					$(By.id("selectedViewSelectBoxItOptions")).$(By.partialLinkText(releaseName)).click();
					Selenide.sleep(1000);
				}
				ExportToOfficeDialogPage exportToOfficeDialog = (ExportToOfficeDialogPage) itemListPage.clickOnMoreMenuItem(MoreMenuHelper.MoreMenuItem.EXPORT_TO_OFFICE);
		        switchTo().frame("inlinedPopupIframe");
				ExportToOfficeDialogPage.WordTab wordTab = exportToOfficeDialog.clickOnWordTab();
		        wordTab.checkIfExport(visible);
		        wordTab.clickOnExport();
		    	Selenide.sleep(4000);
		    	Selenide.close();
			}
		}

	}

}
