/*
 * Copyright (C) 2003-2015 eXo Platform SAS.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.exoplatform.cs.integration.notification;


import org.exoplatform.commons.api.notification.NotificationContext;
import org.exoplatform.commons.api.notification.model.ArgumentLiteral;
import org.exoplatform.commons.api.notification.model.NotificationInfo;
import org.exoplatform.commons.api.notification.plugin.BaseNotificationPlugin;
import org.exoplatform.commons.utils.CommonsUtils;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.cs.entity.TopicEntity;
import org.exoplatform.cs.service.util.CSUtils;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.services.organization.User;
import org.exoplatform.social.core.manager.IdentityManager;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;


public class CSSLAReminderPlugin extends BaseNotificationPlugin {

  private static final Log LOG = ExoLogger.getLogger(CSSLAReminderPlugin.class);

  public final static String ID = "CSSLAReminderPlugin";


  public final static ArgumentLiteral<TopicEntity> TICKET = new ArgumentLiteral<TopicEntity>(TopicEntity.class, "ticket");
  public final static ArgumentLiteral<String> DAYS = new ArgumentLiteral<String>(String.class, "days");
  public final static ArgumentLiteral<String> HOURS = new ArgumentLiteral<String>(String.class, "hours");
  public final static ArgumentLiteral<String> MINUTES = new ArgumentLiteral<String>(String.class, "minutes");
  public final static ArgumentLiteral<String> RECEIVERS = new ArgumentLiteral<String>(String.class, "receivers");




  IdentityManager identityManager;

  public CSSLAReminderPlugin(InitParams initParams, IdentityManager identityManager) {

    super(initParams);
    this.identityManager = identityManager;

  }



  @Override

  public String getId() {

    return ID;

  }



  @Override

  public boolean isValid(NotificationContext ctx) {

    return true;

  }




  @Override

  protected NotificationInfo makeNotification(NotificationContext ctx) {

    TopicEntity ticket = ctx.value(TICKET);
    String receivers_ = ctx.value(RECEIVERS);
    Set<String> receivers = new HashSet<String>();
   try {
     // List<User> users = CSUtils.getSupportMembers();

      for(String user : receivers_.split(",")){
          receivers.add(user);
      }
    } catch (Exception e) {
      LOG.warn("Cannot get the list of support mrmbers");
    }

    StringBuilder activityId = new StringBuilder("support");
    activityId.append("-").append(ticket.getId());
    String ticketUrl = CommonsUtils.getCurrentDomain()+ticket.getLink();
    String days = ctx.value(DAYS);
    String hours = ctx.value(HOURS);
    String minutes = ctx.value(MINUTES);
    String ticketId = ticket.getTicketId();
    String ticketTitle = ticket.getTitle();


    return NotificationInfo.instance()

            .setFrom("support")
            .to(new LinkedList<String>(receivers))
            .with(NotificationUtils.CREATOR, "support")
            .with(NotificationUtils.USER_NAME, ticket.getAssignee())
            .with(NotificationUtils.TICKET_URL, ticketUrl)
            .with(NotificationUtils.ACTIVITY_ID, activityId.toString())
            .with(NotificationUtils.TICKET_ID, ticketId)
            .with(NotificationUtils.TICKET_TITLE, ticketTitle)
            .with(NotificationUtils.DAYS, days)
            .with(NotificationUtils.HOURS, hours)
            .with(NotificationUtils.MINUTES, minutes)
            .with(NotificationUtils.TICKET_STATUS, ticket.getStatus())
            .key(getKey()).end();

  }
}