/*
 * PublishRPubsPage.java
 *
 * Copyright (C) 2022 by Posit Software, PBC
 *
 * Unless you have received this program directly from Posit Software pursuant
 * to the terms of a commercial license agreement with Posit Software, then
 * this program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */
package org.rstudio.studio.client.rsconnect.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import org.rstudio.core.client.resources.ImageResource2x;
import org.rstudio.core.client.widget.WizardPage;
import org.rstudio.studio.client.RStudioGinjector;
import org.rstudio.studio.client.rsconnect.RsconnectConstants;
import org.rstudio.studio.client.rsconnect.model.RSConnectPublishInput;
import org.rstudio.studio.client.rsconnect.model.RSConnectPublishResult;
import org.rstudio.studio.client.rsconnect.model.RSConnectPublishSource;
import org.rstudio.studio.client.workbench.prefs.model.UserPrefs;

import com.google.gwt.user.client.ui.Widget;

public class PublishRPubsPage
   extends WizardPage<RSConnectPublishInput, RSConnectPublishResult>
{
   public PublishRPubsPage(String title, String subTitle)
   {
      super(title, subTitle, constants_.publishToRpubs(),
            new ImageResource2x(useDarkDialogTheme() ?
                  RSConnectResources.INSTANCE.rpubsPublishDark2x() :
                  RSConnectResources.INSTANCE.rpubsPublish2x()),
            new ImageResource2x(useDarkDialogTheme() ?
                  RSConnectResources.INSTANCE.rpubsPublishLargeDark2x() :
                  RSConnectResources.INSTANCE.rpubsPublishLarge2x()));
   }

   @Override
   public void focus()
   {
      
   }

   @Override
   protected Widget createWidget()
   {
      return new PublishRPubs();
   }

   @Override
   protected void initialize(RSConnectPublishInput initData)
   {
      initialData_ = initData;
   }

   @Override
   protected RSConnectPublishResult collectInput()
   {
      return new RSConnectPublishResult(
            new RSConnectPublishSource(
                  initialData_.getOriginatingEvent().getPath(), 
                  initialData_.getOriginatingEvent().getHtmlFile(),
                  null, // website directory
                  null, // website output directory
                  initialData_.isSelfContained(),
                  true, // as static
                  initialData_.isShiny(),
                  initialData_.isQuarto(),
                  initialData_.getDescription(),
                  initialData_.getContentType()));
   }

   @Override
   protected boolean validate(RSConnectPublishResult input)
   {
      return true;
   }

   private static boolean useDarkDialogTheme()
   {
      UserPrefs prefs = RStudioGinjector.INSTANCE.getUserPrefs();
      Element container = Document.get().getElementById("rstudio_container");
      return prefs != null &&
             prefs.useDarkThemeModalDialogs().getValue() &&
             container != null &&
             container.hasClassName("rstudio-themes-dark");
   }

   private RSConnectPublishInput initialData_;
   private static final RsconnectConstants constants_ = GWT.create(RsconnectConstants.class);
}
