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

import java.awt.AWTException;

import org.fundacionjala.automation.framework.pages.tablet.home.HomePage;
import org.fundacionjala.automation.framework.pages.tablet.scheduler.SchedulerPage;
import org.fundacionjala.automation.framework.pages.tablet.settings.NavigationPage;
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 {
        HomePage homePage = new HomePage();

	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);
    }
    
    @Then("^The organizer \"([^\"]*)\" of current meeting \"([^\"]*)\" is displayed on Home$")
    public void verifyCurrentMeetingOrganizer(String organizer, String subject) throws Throwable {
	SchedulerPage schedulerPage = new SchedulerPage();
        HomePage homePage = new HomePage();
        
        schedulerPage
        .topMenu
        .clickOnHomeButton();
        
        boolean actualResult = homePage.verifyCurrentOrganizerDisplayed(organizer);
      
        deleteMeeting(subject);
        
        Assert.assertTrue(actualResult);
    }
    
    @Then("^The meeting \"([^\"]*)\" is displayed on Home Page as next$")
    public void verifyNextMeeting(String subject) throws Throwable {
	SchedulerPage schedulerPage = new SchedulerPage();
        HomePage homePage = new HomePage();
        
        schedulerPage
        .topMenu
        .clickOnHomeButton();
        
        boolean actualResult = homePage.verifyNextMeetingDisplayed(subject);
        
        deleteMeeting(subject);
        
        Assert.assertTrue(actualResult);
    }
    
    @Then("^The organizer \"([^\"]*)\" of next meeting \"([^\"]*)\" is displayed on Home$")
    public void verifyNextMeetingOrganizer(String organizer, String subject) throws Throwable {
	SchedulerPage schedulerPage = new SchedulerPage();
        HomePage homePage = new HomePage();
        
        schedulerPage
        .topMenu
        .clickOnHomeButton();
        
        boolean actualResult = homePage.verifyNextOrganizerDisplayed(organizer);
      
        deleteMeeting(subject);
        
        Assert.assertTrue(actualResult);
    }
    
    @Then("^The correct time left until end of the day should be displayed on Home page$")
    public void verifyCorrectLeftTime() throws Throwable {
	HomePage homePage = new HomePage();
        Assert.assertTrue(homePage.verifyTimeLeft());
    }
    
    @Then("^Room \"([^\"]*)\" is displayed on Home Page$")
    public void room_is_displayed_on_Home_Page(String roomName) throws Throwable {
	NavigationPage navigation = new NavigationPage();
	HomePage homePage = new HomePage();
	
	navigation.topMenu
		  .clickOnHomeButton();
	
	 boolean actualResult = homePage.verifyRoomNameDisplayed(roomName);
	 
	 Assert.assertTrue(actualResult);
    }
    
    public void deleteMeeting(String subject) throws AWTException{
	HomePage homePage = new HomePage();
	
	homePage
        .clickOnScheduleButton()
        .displayAllDayOnTimeline()
        .clickOnMeeting(subject)
        .clickOnRemoveButton()
	.setPassword(PropertiesReader.getExchangeOrganizerPwd())
	.clickOkButton();
    }
    
    @Then("^The correct time left should be displayed according the current meeting \"([^\"]*)\"$")
    public void the_correct_time_left_should_be_displayed_according_the_current_meeting(String subject) throws Throwable {
	NavigationPage navigation = new NavigationPage();
	HomePage homePage = new HomePage();
	
	navigation.topMenu
		  .clickOnHomeButton();

	Assert.assertTrue(homePage.verifyTimeLeftOfCurrentMeeting(), "The Time Left displayed is correct");
    }

}

@@ -5015,23 +5015,561 @@
 ayed is 
-correct
+not according the current Meeting%22);%0A    %7D%0A    %0A    @Then(%22%5EThe correct time left should be displayed according the next meeting %5C%22(%5B%5E%5C%22%5D*)%5C%22$%22)%0A    public void the_correct_time_left_should_be_displayed_according_the_next_meeting(String subject) throws Throwable %7B%0A    %09NavigationPage navigation = new NavigationPage();%0A    %09HomePage homePage = new HomePage();%0A    %09%0A    %09navigation.topMenu%0A    %09%09  %09  .clickOnHomeButton();%0A    %09Assert.assertTrue(homePage.verifyTimeLeftOfNextMeeting(), %22The Time Left displayed is not according the next Meeting
 %22);%0A    

Fix Framework: HomeThenSteps