/*
 * Copyright 2018 Netflix, Inc.
 *
 * Licensed 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 com.netflix.spinnaker.echo.googlechat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class GoogleChatMessage {
  // TODO: Make the Spinnaker info configurable to make reskinning viable.
  static transient String SPINNAKER_ICON_URL =
      "https://avatars0.githubusercontent.com/u/7634182?s=200&v=4";
  static transient String SPINNAKER_FRONT_PAGE_URL = "https://www.spinnaker.io/";

  Card cards;

  transient String message;

  public GoogleChatMessage(String message) {
    this.message = message;
    cards = new Card();
  }

  /** Classes below are used to build the JSON object for the Chat API. * */
  class Card {
    List<Section> sections = new ArrayList<>();

    public Card() {
      sections.add(new Section());
    }
  }

  class Section {
    List<Object> widgets = new ArrayList<>();

    public Section() {
      widgets.add(new TextParagraphWidget());
      widgets.add(new ButtonWidget());
    }
  }

  class TextParagraphWidget {
    HashMap<String, String> textParagraph = new HashMap<>();

    public TextParagraphWidget() {
      textParagraph.put("text", message);
    }
  }

  class ButtonWidget {
    List<Object> buttons = new ArrayList<>();

    public ButtonWidget() {
      buttons.add(new ImageButtonWidget());
      buttons.add(new TextButtonWidget());
    }
  }

  class ImageButtonWidget {
    ImageButton imageButton = new ImageButton();
  }

  class ImageButton {
    String iconUrl = SPINNAKER_ICON_URL;
    OnClick onClick = new OnClick(SPINNAKER_FRONT_PAGE_URL);
  }

  class TextButtonWidget {
    TextButton textButton = new TextButton();
  }

  class TextButton {
    String text = "From Spinnaker";
    OnClick onClick = new OnClick(SPINNAKER_FRONT_PAGE_URL);
  }

  class OnClick {
    OpenLink openLink;

    public OnClick(String link) {
      openLink = new OpenLink(link);
    }
  }

  class OpenLink {
    String url;

    public OpenLink(String link) {
      url = link;
    }
  }
}

@@ -1022,16 +1022,23 @@
 o/%22;%0A%0A  
+public 
 Card car
@@ -1265,24 +1265,31 @@
  Card %7B%0A    
+public 
 List%3CSection
@@ -1405,24 +1405,31 @@
 ction %7B%0A    
+public 
 List%3CObject%3E
@@ -1612,16 +1612,23 @@
 t %7B%0A    
+public 
 HashMap%3C
@@ -1792,16 +1792,23 @@
 t %7B%0A    
+public 
 List%3CObj
@@ -2000,16 +2000,23 @@
 t %7B%0A    
+public 
 ImageBut
@@ -2079,24 +2079,31 @@
 utton %7B%0A    
+public 
 String iconU
@@ -2127,24 +2127,31 @@
 ON_URL;%0A    
+public 
 OnClick onCl
@@ -2231,16 +2231,23 @@
 t %7B%0A    
+public 
 TextButt
@@ -2306,24 +2306,31 @@
 utton %7B%0A    
+public 
 String text 
@@ -2353,16 +2353,23 @@
 r%22;%0A    
+public 
 OnClick 
@@ -2444,16 +2444,23 @@
 k %7B%0A    
+public 
 OpenLink
@@ -2576,16 +2576,23 @@
 k %7B%0A    
+public 
 String u

Fix field visibility. (#622)