/*
 * Copyright 2018 EPAM Systems
 *
 * 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.epam.ta.reportportal.core.events.activity;

import com.epam.ta.reportportal.core.events.ActivityEvent;
import com.epam.ta.reportportal.entity.Activity;
import com.epam.ta.reportportal.entity.ActivityDetails;
import com.epam.ta.reportportal.entity.HistoryField;
import com.epam.ta.reportportal.entity.item.TestItem;
import com.epam.ta.reportportal.entity.item.issue.IssueEntity;
import com.epam.ta.reportportal.ws.model.externalsystem.Ticket;

import java.time.LocalDateTime;
import java.util.stream.Collectors;

import static com.epam.ta.reportportal.core.events.activity.details.ActivityDetailsUtil.TICKET_ID;

/**
 * @author Andrei Varabyeu
 */
public class TicketPostedEvent implements ActivityEvent {

	private final Ticket ticket;
	private final Long postedBy;
	private final TestItem testItem;
	private final Long projectId;

	public TicketPostedEvent(Ticket ticket, TestItem testItem, Long postedBy, Long projectId, String itemName) {
		this.ticket = ticket;
		this.postedBy = postedBy;
		this.testItem = testItem;
		this.projectId = projectId;
	}

	@Override
	public Activity toActivity() {
		Activity activity = new Activity();
		activity.setCreatedAt(LocalDateTime.now());
		activity.setAction(ActivityAction.POST_ISSUE.toString());
		activity.setEntity(Activity.Entity.TICKET);
		activity.setUserId(postedBy);
		activity.setProjectId(projectId);
		activity.setObjectId(testItem.getItemId());

		ActivityDetails details = new ActivityDetails(testItem.getName());
		processTicketId(details);

		activity.setDetails(details);
		return activity;
	}

	private void processTicketId(ActivityDetails details) {
		String oldValue = null;
		if (testItem != null && testItem.getItemResults() != null) {
			oldValue = issuesIdsToString(testItem.getItemResults().getIssue());
		}

		String newValue = ticket.getId() + ":" + ticket.getTicketUrl();
		if (null != oldValue) {
			newValue = oldValue + "," + newValue;
		}

		details.addHistoryField(HistoryField.of(TICKET_ID, oldValue, newValue));
	}

	static String issuesIdsToString(IssueEntity issue) {
		return issue.getTickets()
				.stream()
				.map(t -> t.getTicketId().concat(":").concat(t.getUrl()))
				.collect(Collectors.joining(","));
	}

}

@@ -1316,22 +1316,16 @@
 private 
-final 
 Ticket t
@@ -1336,30 +1336,24 @@
 t;%0A%09private 
-final 
 Long postedB
@@ -1364,22 +1364,16 @@
 private 
-final 
 TestItem
@@ -1396,14 +1396,8 @@
 ate 
-final 
 Long
@@ -1409,16 +1409,50 @@
 ectId;%0A%0A
+%09public TicketPostedEvent() %7B%0A%09%7D%0A%0A
 %09public 
@@ -1605,16 +1605,16 @@
 stedBy;%0A
-
 %09%09this.t
@@ -1667,16 +1667,526 @@
 Id;%0A%09%7D%0A%0A
+%09public Ticket getTicket() %7B%0A%09%09return ticket;%0A%09%7D%0A%0A%09public void setTicket(Ticket ticket) %7B%0A%09%09this.ticket = ticket;%0A%09%7D%0A%0A%09public Long getPostedBy() %7B%0A%09%09return postedBy;%0A%09%7D%0A%0A%09public void setPostedBy(Long postedBy) %7B%0A%09%09this.postedBy = postedBy;%0A%09%7D%0A%0A%09public TestItem getTestItem() %7B%0A%09%09return testItem;%0A%09%7D%0A%0A%09public void setTestItem(TestItem testItem) %7B%0A%09%09this.testItem = testItem;%0A%09%7D%0A%0A%09public Long getProjectId() %7B%0A%09%09return projectId;%0A%09%7D%0A%0A%09public void setProjectId(Long projectId) %7B%0A%09%09this.projectId = projectId;%0A%09%7D%0A%0A
 %09@Overri

Add default controller and getters and setters