// Copyright 2019 Google LLC
//
// 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
//
//     https://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.google.sps.data;

import com.google.sps.servlets.Post;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;

/** Creates and converts HTML templates for email sending. */
public final class Email {

  public static final String welcomeSubject = "⭐ noms: welcome to your free food finder!";
  public static final String newPostSubject = "⭐ noms: new free food near you!";

  // Set paths from /noms/target folder to accommodate maven run/deploy.
  public static final String welcomeContentPath = "./assets/html_templates/WelcomeEmail.html";
  public static final String newPostPath = "./assets/html_templates/NewPost.html";

  /**
    * Convert HTML to String for MimeMessage to configure it into emails.
    *
    * @param path path of HTML file
    * @return string version of HTML file
    * @throws IOException
    */
  public static String getStringFromHTML(String path) throws IOException {

    File HTMLfile = new File(path);
    String str = FileUtils.readFileToString(HTMLfile, "utf-8");
    return str;
  }

  /**
    * Add new post information to emails.
    *
    * @param post new post object
    * @return string of HTML file with new post
    * @throws IOException
    */
  public static String addNewPost(Post post) throws IOException {

    // TODO: Include AM/PM, date formatted, and time zone to be more user friendly.
    String emailContent = getStringFromHTML(newPostPath);
    emailContent = emailContent.replace("[organizationName]", post.organizationName);
    emailContent = emailContent.replace("[location]", post.location);
    emailContent = emailContent.replace("[month]",  Integer.toString(post.month));
    emailContent = emailContent.replace("[day]", Integer.toString(post.day));
    emailContent = emailContent.replace("[startHour]", Integer.toString(post.startHour));
    emailContent = emailContent.replace("[startMinute]", Integer.toString(post.startMinute));
    emailContent = emailContent.replace("[endHour]", Integer.toString(post.endHour));
    emailContent = emailContent.replace("[endMinute]", Integer.toString(post.endMinute));
    emailContent = emailContent.replace("[description]", post.description);
    return emailContent;
  }
}

@@ -2093,17 +2093,20 @@
 %22, post.
-o
+getO
 rganizat
@@ -2112,16 +2112,18 @@
 tionName
+()
 );%0A    e
@@ -2176,24 +2176,29 @@
 %22, post.
-l
+getL
 ocation
+()
 );%0A    e
@@ -2266,21 +2266,26 @@
 ng(post.
-m
+getM
 onth
+()
 ));%0A    
@@ -2355,14 +2355,20 @@
 ost.
-day));
+getMonth()))
 %0A   
@@ -2441,25 +2441,28 @@
 ng(post.
-s
+getS
 tartHour
 ));%0A    
@@ -2449,24 +2449,26 @@
 getStartHour
+()
 ));%0A    emai
@@ -2538,17 +2538,20 @@
 ng(post.
-s
+getS
 tartMinu
@@ -2548,24 +2548,26 @@
 tStartMinute
+()
 ));%0A    emai
@@ -2633,23 +2633,28 @@
 ng(post.
-e
+getE
 ndHour
+()
 ));%0A    
@@ -2726,25 +2726,28 @@
 ng(post.
-e
+getE
 ndMinute
 ));%0A    
@@ -2738,18 +2738,20 @@
 ndMinute
+(
 ))
+)
 ;%0A    em
@@ -2806,17 +2806,20 @@
 %22, post.
-d
+getD
 escripti
@@ -2820,16 +2820,18 @@
 cription
+()
 );%0A    r

use getters instead of public variables