package org.fundacionjala.automation.scenario.steps.tablet.home;

import org.fundacionjala.automation.framework.pages.tablet.home.HomePage;
import org.fundacionjala.automation.framework.pages.tablet.scheduler.SchedulerPage;
import org.fundacionjala.automation.framework.utils.common.PropertiesReader;
import org.testng.Assert;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;

import cucumber.api.java.en.Then;

public class HomeThenSteps {

    @Then("^All meetings of \"([^\"]*)\" room are displayed on home time line even \"([^\"]*)\" meeting$")
    public void verifyTimeLineMeetings(String roomName, String subject) throws Throwable {
	SchedulerPage schedulerPage = new SchedulerPage();
        HomePage homePage = new HomePage();
        
        schedulerPage
        .topMenu
        .clickOnHomeButton();

	MongoClient mongoClient = new MongoClient(
		PropertiesReader.getHostIPAddress(),
		PropertiesReader.getMongoDBConnectionPort());
	DB db = mongoClient.getDB(PropertiesReader.getDBName());
	DBCollection table = db.getCollection(PropertiesReader
		.getMongoDBMeetingTable());
	BasicDBObject query = new BasicDBObject("location", roomName);
	DBCursor cursor = table.find();

	boolean actualResult = homePage.verifyTimeLineMeetings(cursor
		.getCollection().count(query));
	
	deleteMeeting(subject);
	
	Assert.assertTrue(actualResult);
    }
    
    @Then("^The meeting \"([^\"]*)\" is displayed on Home Page as current$")
    public void verifyCurrentMeeting(String subject) throws Throwable {
        SchedulerPage schedulerPage = new SchedulerPage();
        HomePage homePage = new HomePage();
        
        schedulerPage
        .topMenu
        .clickOnHomeButton();
        
        boolean actualResult = homePage.verifyCurrentMeetingDisplayed(subject);
        
        deleteMeeting(subject);
        
        Assert.assertTrue(actualResult);
    }
    
    public void deleteMeeting(String subject){
	HomePage homePage = new HomePage();
	
	homePage
        .clickOnScheduleButton()
        .clickOnMeeting(subject)
        .clickOnRemoveButton()
	.setPassword(PropertiesReader.getExchangeOrganizerPwd())
	.clickOkButton();
    }
}

@@ -2252,10 +2252,262 @@
 ;%0A    %7D%0A
+    %0A    @Then(%22%5EThe correct time left until end of the day should be displayed on Home page$%22)%0A    public void verifyCorrectLeftTime() throws Throwable %7B%0A%09HomePage homePage = new HomePage();%0A        Assert.assertTrue(homePage.verifyTimeLeft());%0A    %7D%0A
 %7D%0A

Fix Scenario: HomeThenSteps