/*
 * Copyright 2016 EPAM Systems
 * 
 * 
 * This file is part of EPAM Report Portal.
 * https://github.com/reportportal/service-api
 * 
 * Report Portal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Report Portal 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Report Portal.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.epam.ta.reportportal.ws.converter.builders;

import com.epam.ta.reportportal.commons.SendCase;
import com.epam.ta.reportportal.database.entity.Project;
import com.epam.ta.reportportal.database.entity.ProjectSpecific;
import com.epam.ta.reportportal.database.entity.StatisticsCalculationStrategy;
import com.epam.ta.reportportal.database.entity.project.EntryType;
import com.epam.ta.reportportal.database.entity.project.InterruptionJobDelay;
import com.epam.ta.reportportal.database.entity.project.KeepLogsDelay;
import com.epam.ta.reportportal.database.entity.project.KeepScreenshotsDelay;
import com.epam.ta.reportportal.exception.ReportPortalException;
import com.epam.ta.reportportal.ws.model.ErrorType;
import com.epam.ta.reportportal.ws.model.project.CreateProjectRQ;
import com.epam.ta.reportportal.ws.model.project.email.EmailSenderCase;
import com.epam.ta.reportportal.ws.model.project.email.ProjectEmailConfig;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Optional;

import static com.google.common.collect.Lists.newArrayList;
import static java.util.Collections.emptyList;

/**
 * New project object builder
 *
 * @author Andrei_Ramanchuk
 */
@Service
@Scope("prototype")
public class ProjectBuilder extends Builder<Project> {

	public ProjectBuilder addCreateProjectRQ(CreateProjectRQ createProjectRQ) {
		if (createProjectRQ != null) {
			getObject().setName(createProjectRQ.getProjectName().trim());
			getObject().setCreationDate(new Date());

			getObject().getConfiguration().setEntryType(EntryType.findByName(createProjectRQ.getEntryType()).orElse(null));
			if (null != createProjectRQ.getCustomer())
				getObject().setCustomer(createProjectRQ.getCustomer().trim());
			if (null != createProjectRQ.getAddInfo())
				getObject().setAddInfo(createProjectRQ.getAddInfo().trim());

			// Empty fields creation by default
			getObject().getConfiguration().setExternalSystem(new ArrayList<>());
			getObject().getConfiguration().setProjectSpecific(ProjectSpecific.DEFAULT);
			getObject().getConfiguration().setInterruptJobTime(InterruptionJobDelay.ONE_DAY.getValue());
			getObject().getConfiguration().setKeepLogs(KeepLogsDelay.THREE_MONTHS.getValue());
			getObject().getConfiguration().setKeepScreenshots(KeepScreenshotsDelay.TWO_WEEKS.getValue());
			getObject().getConfiguration().setIsAutoAnalyzerEnabled(false);

			// Email settings by default
			EmailSenderCase defaultOne = new EmailSenderCase(newArrayList("OWNER"), SendCase.ALWAYS.name(), emptyList(), emptyList());
			ProjectEmailConfig config = new ProjectEmailConfig(false, "reportportal@example.com", newArrayList(defaultOne));
			getObject().getConfiguration().setEmailConfig(config);

			// Users
			getObject().setUsers(new HashMap<>());
		}
		return this;
	}

	@Override
	protected Project initObject() {
		return new Project();
	}
}

@@ -847,58 +847,8 @@
 s;%0A%0A
-import com.epam.ta.reportportal.commons.SendCase;%0A
 impo
@@ -1374,133 +1374,23 @@
 tal.
-exception.ReportPortalException;%0Aimport com.epam.ta.reportportal.ws.model.ErrorType;%0Aimport com.epam.ta.reportportal.ws.model
+database.entity
 .pro
@@ -1394,31 +1394,28 @@
 project.
-Create
 Project
-RQ
+Utils
 ;%0Aimport
@@ -1461,104 +1461,23 @@
 ect.
-email.EmailSenderCase;%0Aimport com.epam.ta.reportportal.ws.model.project.email.ProjectEmailConfig
+CreateProjectRQ
 ;%0Aim
@@ -1655,143 +1655,8 @@
 Map;
-%0Aimport java.util.Optional;%0A%0Aimport static com.google.common.collect.Lists.newArrayList;%0Aimport static java.util.Collections.emptyList;
 %0A%0A/*
@@ -2906,327 +2906,184 @@
 e);%0A
-%0A
 %09%09%09
-// Email settings by default%0A%09%09%09EmailSenderCase defaultOne = new EmailSenderCase(newArrayList(%22OWNER%22), SendCase.ALWAYS.name(), emptyList(), emptyList()
+getObject().getConfiguration().setStatisticsCalculationStrategy(StatisticsCalculationStrategy.STEP_BASED
 );%0A
+%0A
 %09%09%09
-ProjectEmailConfig config = new ProjectEmailConfig(false, %22reportportal@example.com%22, newArrayList(defaultOne));%0A%09%09%09getObject().getConfiguration().se
+// Email settings by default%0A%09%09%09ProjectUtils.setDefaul
 tEmailCo
 nfig
@@ -3082,19 +3082,30 @@
 ilCo
-n
 fig
-(config
+uration(getObject()
 );%0A%0A

update project builder to avoid creating project with empty stats strategy