package com.wikia.webdriver.testcases.desktop.articlecrudtests;

import com.wikia.webdriver.common.contentpatterns.PageContent;
import com.wikia.webdriver.common.contentpatterns.VideoContent;
import com.wikia.webdriver.common.core.TestContext;
import com.wikia.webdriver.common.core.annotations.Execute;
import com.wikia.webdriver.common.core.annotations.InBrowser;
import com.wikia.webdriver.common.core.annotations.RelatedIssue;
import com.wikia.webdriver.common.core.api.ArticleContent;
import com.wikia.webdriver.common.core.helpers.User;
import com.wikia.webdriver.common.dataprovider.ArticleFeaturesCRUDDataProvider;
import com.wikia.webdriver.common.templates.NewTestTemplate;
import com.wikia.webdriver.pageobjectsfactory.componentobject.addphoto.AddPhotoComponentObject;
import com.wikia.webdriver.pageobjectsfactory.componentobject.addtable.TableBuilderComponentObject;
import com.wikia.webdriver.pageobjectsfactory.componentobject.addtable.TableBuilderComponentObject.Alignment;
import com.wikia.webdriver.pageobjectsfactory.componentobject.addtable.TableBuilderComponentObject.Headers;
import com.wikia.webdriver.pageobjectsfactory.componentobject.gallery.GalleryBuilderComponentObject;
import com.wikia.webdriver.pageobjectsfactory.componentobject.gallery.GalleryBuilderComponentObject.Orientation;
import com.wikia.webdriver.pageobjectsfactory.componentobject.gallery.GalleryBuilderComponentObject.PositionsGallery;
import com.wikia.webdriver.pageobjectsfactory.componentobject.gallery.GalleryBuilderComponentObject.SpacingGallery;
import com.wikia.webdriver.pageobjectsfactory.componentobject.photo.PhotoAddComponentObject;
import com.wikia.webdriver.pageobjectsfactory.componentobject.photo.PhotoOptionsComponentObject;
import com.wikia.webdriver.pageobjectsfactory.componentobject.slider.SliderBuilderComponentObject;
import com.wikia.webdriver.pageobjectsfactory.componentobject.slider.SliderBuilderComponentObject.MenuPositions;
import com.wikia.webdriver.pageobjectsfactory.componentobject.slideshow.SlideshowBuilderComponentObject;
import com.wikia.webdriver.pageobjectsfactory.componentobject.slideshow.SlideshowBuilderComponentObject.Positions;
import com.wikia.webdriver.pageobjectsfactory.componentobject.vet.VetAddVideoComponentObject;
import com.wikia.webdriver.pageobjectsfactory.componentobject.vet.VetOptionsComponentObject;
import com.wikia.webdriver.pageobjectsfactory.pageobject.WikiBasePageObject;
import com.wikia.webdriver.pageobjectsfactory.pageobject.article.ArticlePageObject;
import com.wikia.webdriver.pageobjectsfactory.pageobject.article.editmode.VisualEditModePageObject;
import com.wikia.webdriver.pageobjectsfactory.pageobject.article.editmode.VisualEditModePageObject.Components;

import org.testng.annotations.Test;

@Test(groups = {"ArticleFeaturesCRUDUser"})
@Execute(asUser = User.USER)
public class ArticleFeaturesCRUDTestsUser extends NewTestTemplate {

  private static final int ADDITIONAL_PROPERTY_VALUE = 10;

  @Test(groups = {"ArticleFeaturesCRUDUser_001", "Smoke"})
  public void addGallery() {
    new ArticleContent().clear();

    VisualEditModePageObject visualEditMode = new VisualEditModePageObject().open();
    visualEditMode.clearContent();
    GalleryBuilderComponentObject galleryBuiler = visualEditMode.clickGalleryButton();
    AddPhotoComponentObject galleryAddPhoto = galleryBuiler.clickAddPhoto();
    galleryAddPhoto.search("image");
    galleryAddPhoto.choosePhotos(4);
    galleryAddPhoto.clickSelect();
    galleryBuiler.adjustPosition(PositionsGallery.CENTER);
    galleryBuiler.adjustColumns("2");
    galleryBuiler.adjustSpacing(SpacingGallery.SMALL);
    galleryBuiler.adjustOrientation(Orientation.LANDSCAPE);
    galleryBuiler.clickFinish();
    visualEditMode.verifyGallery();
    visualEditMode.submitArticle().verifyGallery();
  }

  @Test(groups = {"Smoke"})
  public void modifyGallery() {
    new ArticleContent().push(
        "<gallery position=\"right\" columns=\"1\" spacing=\"medium\">\n" + "default_Image010.jpg\n"
        + "default_Image009.jpg\n" + "</gallery>");

    VisualEditModePageObject visualEditMode = new VisualEditModePageObject().open();
    GalleryBuilderComponentObject
        galleryBuiler
        = (GalleryBuilderComponentObject) visualEditMode.modifyComponent(Components.GALLERY);
    AddPhotoComponentObject galleryAddPhoto = galleryBuiler.clickAddPhoto();
    galleryAddPhoto.search("image");
    galleryAddPhoto.choosePhotos(2);
    galleryAddPhoto.clickSelect();
    galleryBuiler.adjustPosition(PositionsGallery.RIGHT);
    galleryBuiler.adjustColumns("3");
    galleryBuiler.adjustSpacing(SpacingGallery.MEDIUM);
    galleryBuiler.adjustOrientation(Orientation.PORTRAIT);
    galleryBuiler.clickFinish();
    visualEditMode.verifyGallery();
    visualEditMode.submitArticle().verifyGallery();
  }

  @Test(groups = {"ArticleFeaturesCRUDUser_002"})
  public void deleteGallery() {
    new ArticleContent().push(
        "<gallery position=\"right\" columns=\"2\" spacing=\"medium\">\n" + "default_Image010.jpg\n"
        + "default_default_Image009.jpg\n" + "default_Image008.jpg\n" + "default_Image007.jpg\n"
        + "</gallery>");

    VisualEditModePageObject visualEditMode = new VisualEditModePageObject().open();
    visualEditMode.removeComponent(Components.GALLERY);
    visualEditMode.verifyComponentRemoved(Components.GALLERY);
  }

  /**
   * @perequisites: 4 images with image in the name
   */
  @Test(groups = {"ArticleFeaturesCRUDUser_003"})
  public void addSlideshow() {
    new ArticleContent().clear();

    VisualEditModePageObject visualEditMode = new VisualEditModePageObject().open();
    SlideshowBuilderComponentObject slideshowBuilder = visualEditMode.clickSlideshowButton();
    AddPhotoComponentObject slideshowAddPhoto = slideshowBuilder.clickAddPhoto();
    slideshowAddPhoto.search("image");
    slideshowAddPhoto.choosePhotos(4);
    slideshowAddPhoto.clickSelect();
    slideshowBuilder.adjustPosition(Positions.CENTER);
    slideshowBuilder.clickFinish();
    visualEditMode.verifySlideshow();
    visualEditMode.submitArticle().verifySlideshow();
  }

  /**
   * @prerequisites 8 images with "Image" in the name
   */
  @Test(groups = {"ArticleFeaturesCRUDUser_003"})
  public void modifySlideshow() {
    new ArticleContent().push(
        "<gallery type=\"slideshow\">\ndefault_Image010.jpg\ndefault_Image009.jpg\ndefault_Image008.jpg\ndefault_Image007.jpg\n"
        + "default_Image010.jpg\ndefault_Image009.jpg\ndefault_Image008.jpg\ndefault_Image007.jpg\ndefault_Image006.jpg\ndefault_Image005.jpg\n"
        + "default_Image004.jpg\ndefault_Image003.jpg\n</gallery>");

    VisualEditModePageObject visualEditor = new VisualEditModePageObject().open();
    SlideshowBuilderComponentObject
        slideshowBuilder
        = (SlideshowBuilderComponentObject) visualEditor.modifyComponent(Components.SLIDESHOW);
    AddPhotoComponentObject slideshowAddPhoto = slideshowBuilder.clickAddPhoto();
    slideshowAddPhoto.search("image");
    slideshowAddPhoto.choosePhotos(8);
    slideshowAddPhoto.clickSelect();
    slideshowBuilder.adjustPosition(Positions.RIGHT);
    slideshowBuilder.clickFinish();
    visualEditor.verifySlideshow();
    visualEditor.submitArticle().verifySlideshow();
  }

  @Test(groups = {"ArticleFeaturesCRUDUser_004"})
  public void deleteSlideshow() {
    new ArticleContent().push(
        "<gallery type=\"slideshow\">\ndefault_Image010.jpg\ndefault_Image009.jpg\ndefault_Image008.jpg\ndefault_Image007.jpg\n"
        + "default_Image010.jpg\ndefault_Image009.jpg\ndefault_Image008.jpg\ndefault_Image007.jpg\ndefault_Image006.jpg\ndefault_Image005.jpg\n"
        + "default_Image004.jpg\ndefault_Image003.jpg\n</gallery>");

    VisualEditModePageObject visualEditMode = new VisualEditModePageObject().open();
    visualEditMode.removeComponent(Components.SLIDESHOW);
    visualEditMode.verifyComponentRemoved(Components.SLIDESHOW);
  }

  /**
   * @prerequisites 4 images with image in the name
   */
  @Test(groups = {"ArticleFeaturesCRUDUser_005"})
  public void addSlider() {
    new ArticleContent().clear();

    VisualEditModePageObject visualEditMode = new VisualEditModePageObject().open();
    SliderBuilderComponentObject sliderBuilder = visualEditMode.clickSliderButton();
    sliderBuilder.selectMenuPosition(MenuPositions.VERTICAL);
    AddPhotoComponentObject sliderAddPhoto = sliderBuilder.clickAddPhoto();
    sliderAddPhoto.search("image");
    sliderAddPhoto.choosePhotos(4);
    sliderAddPhoto.clickSelect();
    sliderBuilder.clickFinish();
    visualEditMode.verifySlider();
    visualEditMode.submitArticle().verifySlider();
  }

  @Test(groups = {"ArticleFeaturesCRUDUser_006"})
  public void modifySlider() {
    new ArticleContent()
        .push("<gallery type=\"slider\" orientation=\"right\">\ndefault_Image010.jpg\n"
              + "default_Image009.jpg\ndefault_Image008.jpg\ndefault_Image007.jpg\n</gallery>");

    VisualEditModePageObject visualEditor = new VisualEditModePageObject().open();
    SliderBuilderComponentObject
        sliderBuilder
        = (SliderBuilderComponentObject) visualEditor.modifyComponent(Components.SLIDER);
    sliderBuilder.selectMenuPosition(MenuPositions.HORIZONTAL);
    AddPhotoComponentObject sliderAddPhoto = sliderBuilder.clickAddPhoto();
    sliderAddPhoto.search("image");
    sliderAddPhoto.choosePhotos(8);
    sliderAddPhoto.clickSelect();
    sliderBuilder.clickFinish();
    visualEditor.verifySlider();
    visualEditor.submitArticle().verifySlider();
  }

  @Test(groups = {"ArticleFeaturesCRUDUser_007"})
  public void deleteSlider() {
    new ArticleContent()
        .push("<gallery type=\"slider\" orientation=\"right\">\ndefault_Image010.jpg\n"
              + "default_Image009.jpg\ndefault_Image008.jpg\ndefault_Image007.jpg\n</gallery>");

    VisualEditModePageObject visualEditor = new VisualEditModePageObject().open();
    visualEditor.removeComponent(Components.SLIDER);
    visualEditor.verifyComponentRemoved(Components.SLIDER);
  }

  @Test(groups = {"ArticleFeaturesCRUDUser_008", "Media"})
  @RelatedIssue(issueID = "XW-3797")
  public void addVideo() {
    new ArticleContent().clear();

    VisualEditModePageObject visualEditMode = new VisualEditModePageObject().open();
    VetAddVideoComponentObject vetAddVideo = visualEditMode.clickVideoButton();
    VetOptionsComponentObject
        vetOptions
        = vetAddVideo.addVideoByUrl(VideoContent.YOUTUBE_VIDEO_URL);
    vetOptions.setCaption(PageContent.CAPTION);
    vetOptions.submit();
    visualEditMode.verifyVideo();
    visualEditMode.submitArticle().verifyVideo();
  }

  /**
   * @prerequisites added video with title Top 20 Shots the Tennis World will NEVER Forget
   */
  @Test(groups = {"ArticleFeaturesCRUDUser_009", "Media"})
  @Execute(asUser = User.USER, onWikia = "mobileregressiontesting")
  public void modifyVideo() {
    new ArticleContent().push(
        "[[File:Top 20 Shots the Tennis World will NEVER Forget|thumb|right|335 px]]");

    VisualEditModePageObject visualEditMode = new VisualEditModePageObject().open();
    VetOptionsComponentObject
        vetOptions
        = (VetOptionsComponentObject) visualEditMode.modifyComponent(Components.VIDEO);
    vetOptions.setCaption(PageContent.CAPTION2);
    vetOptions.update();
    visualEditMode.verifyVideo();
    visualEditMode.submitArticle().verifyVideo();
  }

  @Test(groups = {"ArticleFeaturesCRUDUser_010", "Smoke5", "Media"})
  @Execute(asUser = User.USER, onWikia = "mobileregressiontesting")
  public void deleteVideo() {
    new ArticleContent().push(
        "[[File:Top 20 Shots the Tennis World will NEVER Forget|thumb|right|335 px]]");

    VisualEditModePageObject visualEditMode = new VisualEditModePageObject().open();

    visualEditMode.removeComponent(Components.VIDEO);
    visualEditMode.verifyComponentRemoved(Components.VIDEO);
  }

  @Test(groups = {"ArticleFeaturesCRUDUser_011", "Smoke4"})
  public void modifyImage() {
    new ArticleContent().push("[[File:default_Image010.jpg|thumb|QAWebdriverCaption1]]");

    VisualEditModePageObject visualEditMode = new VisualEditModePageObject().open();
    visualEditMode.modifyComponent(Components.PHOTO);
    PhotoOptionsComponentObject photoOptions = new PhotoOptionsComponentObject(driver);
    photoOptions.setCaption(PageContent.CAPTION2);
    photoOptions.clickAddPhoto();
    visualEditMode.verifyPhoto();
    visualEditMode.submitArticle().verifyPhoto();
  }

  @Test(groups = {"ArticleFeaturesCRUDUser_012", "Smoke1"})
  @InBrowser(browserSize = "1024x720")
  public void addImage() {
    new ArticleContent().clear();

    VisualEditModePageObject visualEditMode = new VisualEditModePageObject().open();
    PhotoAddComponentObject photoAddPhoto = visualEditMode.clickPhotoButton();
    PhotoOptionsComponentObject photoOptions = photoAddPhoto.addPhotoFromWiki("image", 1);
    photoOptions.setCaption(PageContent.CAPTION);
    photoOptions.clickAddPhoto();
    visualEditMode.verifyPhoto();
    visualEditMode.submitArticle();
  }

  @Test(groups = {"ArticleFeaturesCRUDUser_013"})
  public void deleteImage() {
    new ArticleContent().push("[[File:default_Image009.jpg|thumb|QAWebdriverCaption1]]");

    VisualEditModePageObject visualEditMode = new VisualEditModePageObject().open();
    visualEditMode.removeComponent(Components.PHOTO);
    visualEditMode.verifyComponentRemoved(Components.PHOTO);
  }

  @Test(dataProviderClass = ArticleFeaturesCRUDDataProvider.class, dataProvider = "getTableProperties", groups = {
      "ArticleFeaturesCRUDUser_014"})
  public void addingTable(
      int border, int width, int height, int cellspacing, int cellpadding, Alignment alignment
  ) {
    new ArticleContent().clear();

    VisualEditModePageObject visualEditMode = new VisualEditModePageObject().open();
    TableBuilderComponentObject addTable = visualEditMode.clickAddTableButton();
    addTable.verifyAddTableLightbox();
    addTable.typeAmountOfRows(3);
    addTable.typeAmountOfColumns(2);
    addTable.selectHeader(Headers.FIRSTCOLUMN);
    addTable.typeBorderSize(border);
    addTable.selectAlignment(alignment);
    addTable.typeWidth(width);
    addTable.typeHeight(height);
    addTable.typeCellSpacing(cellspacing);
    addTable.typeCellPadding(cellpadding);
    addTable.submitTable();
    ArticlePageObject article = visualEditMode.submitArticle();
    article.verifyTableBorder(border);
    article.verifyTableCellspacing(cellspacing);
    article.verifyTableCellpadding(cellpadding);
    article.verifyTableAlignment(alignment);
    article.verifyTableSize(width, height);
  }

  @Test(dataProviderClass = ArticleFeaturesCRUDDataProvider.class, dataProvider = "getTableProperties", groups = {
      "ArticleFeaturesCRUDUser_0125"}, enabled = false)
  public void modifyTable(
      int border, int width, int height, int cellspacing, int cellpadding, Alignment alignment
  ) {
    new ArticleContent().push(String.format(
        "{| border=\"%d\" cellpadding=\"%d\" cellspacing=\"%d\" "
        + "class=\"article-table article-table-selected\" "
        + "style=\"float: %s; height: %dpx; width: %dpx;\"\n"
        + "! scope=\"row\"|\n|\n|-\n! scope=\"row\"|\n|\n" + "|-\n! scope=\"row\"|\n|\n" + "|}",
        border,
        cellpadding,
        cellspacing,
        alignment.getAlignment(),
        height,
        width
    ));

    VisualEditModePageObject visualEditMode = new VisualEditModePageObject().open();

    visualEditMode.clickPropertiesTableButton();
    TableBuilderComponentObject addTable = new TableBuilderComponentObject(driver);
    addTable.typeBorderSize(border + ADDITIONAL_PROPERTY_VALUE);
    addTable.typeCellSpacing(cellspacing + ADDITIONAL_PROPERTY_VALUE);
    addTable.typeCellPadding(cellpadding + ADDITIONAL_PROPERTY_VALUE);
    addTable.submitTable();
    ArticlePageObject article = visualEditMode.submitArticle();
    article.verifyTableBorder(border + ADDITIONAL_PROPERTY_VALUE);
    article.verifyTableCellspacing(cellspacing + ADDITIONAL_PROPERTY_VALUE);
    article.verifyTableCellpadding(cellpadding + ADDITIONAL_PROPERTY_VALUE);
  }

  @Test(dataProviderClass = ArticleFeaturesCRUDDataProvider.class, dataProvider = "getTableProperties", groups = {
      "ArticleFeaturesCRUDUser_016"})
  public void deleteTable(
      int border, int width, int height, int cellspacing, int cellpadding, Alignment alignment
  ) {
    new ArticleContent().push(String.format(
        "{| border=\"%d\" cellpadding=\"%d\" cellspacing=\"%d\" "
        + "class=\"article-table article-table-selected\" "
        + "style=\"float: %s; height: %dpx; width: %dpx;\"\n"
        + "! scope=\"row\"|\n|\n|-\n! scope=\"row\"|\n|\n" + "|-\n! scope=\"row\"|\n|\n" + "|}",
        border,
        cellpadding,
        cellspacing,
        alignment.getAlignment(),
        height,
        width
    ));

    VisualEditModePageObject visualEditMode = new VisualEditModePageObject().open();
    visualEditMode.clickDeleteTableButton();
    visualEditMode.submitArticle().verifyTableRemoved();
  }

  @Test(groups = {"ArticleFeaturesCRUDUser_017"})
  public void addingImagePlaceholder() {
    new ArticleContent().clear();

    new ArticlePageObject().open();
    VisualEditModePageObject
        visualEditMode
        = new WikiBasePageObject().goToArticleDefaultContentEditPage(wikiURL,
                                                                     TestContext
                                                                         .getCurrentMethodName()
    );
    ArticlePageObject article = visualEditMode.submitArticle();
    PhotoAddComponentObject photoAddPhoto = article.clickAddImagePlaceholder();
    PhotoOptionsComponentObject photoOptions = photoAddPhoto.addPhotoFromWiki("image", 2);
    photoOptions.setCaption(PageContent.CAPTION);
    photoOptions.clickAddPhoto();
    article.verifyPhoto();
  }
}
