package com.epam.reportportal.auth;

import com.epam.ta.reportportal.database.dao.ServerSettingsRepository;
import com.epam.ta.reportportal.database.entity.OAuth2LoginDetails;
import com.epam.ta.reportportal.database.entity.ServerSettings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * Created by andrei_varabyeu on 12/26/16.
 */
@Component
public class DemoDataEvent {

	@Autowired
	private ServerSettingsRepository serverSettingsRepository;

	@EventListener(ContextRefreshedEvent.class)
	public void onStart(){
		ServerSettings settings = serverSettingsRepository.findOne("default");
		settings.setoAuth2LoginDetails(null);

//		Optional<OAuth2LoginDetails> githubDetails = Optional.ofNullable(settings.getoAuth2LoginDetails())
//				.flatMap(details -> details.stream().filter(d -> "github".equals(d.getId())).findAny());
//		if (!githubDetails.isPresent()){
//			OAuth2LoginDetails oAuth2LoginDetails = new OAuth2LoginDetails();
//			oAuth2LoginDetails.setScope(Collections.singletonList("user"));
//			oAuth2LoginDetails.setId("github");
//			oAuth2LoginDetails.setGrantType("authorization_code");
//			oAuth2LoginDetails.setClientId("f4cec43d4541283879c4");
//			oAuth2LoginDetails.setClientSecret("a31aa6de3e27c11d90762cad11936727d6b0759e");
//			oAuth2LoginDetails.setAccessTokenUri("https://github.com/login/oauth/access_token");
//			oAuth2LoginDetails.setUserAuthorizationUri("https://github.com/login/oauth/authorize");
//
//			List<OAuth2LoginDetails> detailsList = Optional.ofNullable(settings.getoAuth2LoginDetails()).orElseGet(ArrayList::new);
//			detailsList.add(oAuth2LoginDetails);
//
//			settings.setoAuth2LoginDetails(detailsList);
//
//		}


		OAuth2LoginDetails oAuth2LoginDetails = new OAuth2LoginDetails();
		oAuth2LoginDetails.setScope(Collections.singletonList("user"));
		oAuth2LoginDetails.setId("github");
		oAuth2LoginDetails.setGrantType("authorization_code");
		oAuth2LoginDetails.setClientId("f4cec43d4541283879c4");
		oAuth2LoginDetails.setClientSecret("a31aa6de3e27c11d90762cad11936727d6b0759e");
		oAuth2LoginDetails.setAccessTokenUri("https://github.com/login/oauth/access_token");
		oAuth2LoginDetails.setUserAuthorizationUri("https://github.com/login/oauth/authorize");
		oAuth2LoginDetails.setClientAuthenticationScheme("form");

		List<OAuth2LoginDetails> detailsList = Optional.ofNullable(settings.getoAuth2LoginDetails()).orElseGet(ArrayList::new);
		detailsList.add(oAuth2LoginDetails);

		settings.setoAuth2LoginDetails(detailsList);

		serverSettingsRepository.save(settings);
	}
}

@@ -637,16 +637,60 @@
 mponent%0A
+@Deprecated%0A//TODO do not forget to remove!%0A
 public c
@@ -926,16 +926,16 @@
 ault%22);%0A
+
 %09%09settin
@@ -971,1046 +971,8 @@
 );%0A%0A
-//%09%09Optional%3COAuth2LoginDetails%3E githubDetails = Optional.ofNullable(settings.getoAuth2LoginDetails())%0A//%09%09%09%09.flatMap(details -%3E details.stream().filter(d -%3E %22github%22.equals(d.getId())).findAny());%0A//%09%09if (!githubDetails.isPresent())%7B%0A//%09%09%09OAuth2LoginDetails oAuth2LoginDetails = new OAuth2LoginDetails();%0A//%09%09%09oAuth2LoginDetails.setScope(Collections.singletonList(%22user%22));%0A//%09%09%09oAuth2LoginDetails.setId(%22github%22);%0A//%09%09%09oAuth2LoginDetails.setGrantType(%22authorization_code%22);%0A//%09%09%09oAuth2LoginDetails.setClientId(%22f4cec43d4541283879c4%22);%0A//%09%09%09oAuth2LoginDetails.setClientSecret(%22a31aa6de3e27c11d90762cad11936727d6b0759e%22);%0A//%09%09%09oAuth2LoginDetails.setAccessTokenUri(%22https://github.com/login/oauth/access_token%22);%0A//%09%09%09oAuth2LoginDetails.setUserAuthorizationUri(%22https://github.com/login/oauth/authorize%22);%0A//%0A//%09%09%09List%3COAuth2LoginDetails%3E detailsList = Optional.ofNullable(settings.getoAuth2LoginDetails()).orElseGet(ArrayList::new);%0A//%09%09%09detailsList.add(oAuth2LoginDetails);%0A//%0A//%09%09%09settings.setoAuth2LoginDetails(detailsList);%0A//%0A//%09%09%7D%0A%0A
 %0A%09%09O

add dynamic github oauth