/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.texera.amber.operator.visualization.waterfallChart

import com.fasterxml.jackson.annotation.{JsonProperty, JsonPropertyDescription}
import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaTitle
import org.apache.texera.amber.core.tuple.{AttributeType, Schema}
import org.apache.texera.amber.pybuilder.PythonTemplateBuilder.PythonTemplateBuilderStringContext
import org.apache.texera.amber.pybuilder.PyStringTypes.EncodableString
import org.apache.texera.amber.core.workflow.PortIdentity
import org.apache.texera.amber.operator.PythonOperatorDescriptor
import org.apache.texera.amber.operator.metadata.annotations.AutofillAttributeName
import org.apache.texera.amber.operator.metadata.{OperatorGroupConstants, OperatorInfo}
import org.apache.texera.amber.pybuilder.PythonTemplateBuilder

class WaterfallChartOpDesc extends PythonOperatorDescriptor {

  @JsonProperty(value = "xColumn", required = true)
  @JsonSchemaTitle("X Axis Values")
  @JsonPropertyDescription("The column representing categories or stages")
  @AutofillAttributeName
  var xColumn: EncodableString = _

  @JsonProperty(value = "yColumn", required = true)
  @JsonSchemaTitle("Y Axis Values")
  @JsonPropertyDescription("The column representing numeric values for each stage")
  @AutofillAttributeName
  var yColumn: EncodableString = _

  override def getOutputSchemas(
      inputSchemas: Map[PortIdentity, Schema]
  ): Map[PortIdentity, Schema] = {
    val outputSchema = Schema()
      .add("html-content", AttributeType.STRING)
    Map(operatorInfo.outputPorts.head.id -> outputSchema)
    Map(operatorInfo.outputPorts.head.id -> outputSchema)
  }

  override def operatorInfo: OperatorInfo =
    OperatorInfo.forVisualization(
      "Waterfall Chart",
      "Visualize data as a waterfall chart",
      OperatorGroupConstants.VISUALIZATION_FINANCIAL_GROUP
    )

  def createPlotlyFigure(): PythonTemplateBuilder = {
    pyb"""
       |        x_values = table[$xColumn]
       |        y_values = table[$yColumn]
       |
       |        fig = go.Figure(go.Waterfall(
       |            name="Waterfall", orientation="v",
       |            measure=["relative"] * (len(y_values) - 1) + ["total"],
       |            x=x_values,
       |            y=y_values,
       |            textposition="outside",
       |            text=[f"{v:+}" for v in y_values],
       |            connector={"line": {"color": "rgb(63, 63, 63)"}}
       |        ))
       |
       |        fig.update_layout(showlegend=True, waterfallgap=0.3)
       |"""
  }

  override def generatePythonCode(): String = {
    val finalCode =
      pyb"""
         |from pytexera import *
         |
         |import plotly.graph_objects as go
         |import plotly.io
         |
         |class ProcessTableOperator(UDFTableOperator):
         |
         |    # Generate custom error message as html string
         |    def render_error(self, error_msg) -> str:
         |        return '''<h1>Waterfall chart is not available.</h1>
         |                  <p>Reason is: {} </p>
         |               '''.format(error_msg)
         |
         |    @overrides
         |    def process_table(self, table: Table, port: int) -> Iterator[Optional[TableLike]]:
         |        if table.empty:
         |            yield {'html-content': self.render_error("input table is empty.")}
         |            return
         |        ${createPlotlyFigure()}
         |        html = plotly.io.to_html(fig, include_plotlyjs='cdn', auto_play=False)
         |        yield {'html-content': html}
         |"""
    finalCode.encode
  }

}
