// Path: src/main/java/io/pivotal/web/domain/RegistrationRequest.java
// @Data
// public class RegistrationRequest {
// 
//     @NotNull
//     @NotEmpty
//     private String givenNames;
// 
//     private String surname;
// 
//     @NotNull
//     @NotEmpty
//     @Email
//     private String email;
// 
//     @NotEmpty
//     @NotNull
//     //TODO - more complex validation on password
//     private String password;
// 
//     @NotEmpty
//     @NotNull
//     //TODO - more complex validation on password
//     private String confirmPassword;
// }


import io.pivotal.web.domain.RegistrationRequest;
import io.pivotal.web.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.validation.Valid;
import java.security.Principal;
	@Autowired
	private PortfolioService portfolioService;
	
	@Autowired
	private MarketSummaryService summaryService;
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String showHome(Model model,Principal princ,  @AuthenticationPrincipal OAuth2User principal){
		//check if user is logged in!
		if (principal != null) {
			return "redirect:/home";
		}
		model.addAttribute("marketSummary", summaryService.getMarketSummary());
		return "index";
	}

	@GetMapping("/home")
	public String authorizedHome(Model model, @AuthenticationPrincipal OAuth2User principal, @RegisteredOAuth2AuthorizedClient("pivotalbank") OAuth2AuthorizedClient oAuth2AuthorizedClient) {
		model.addAttribute("marketSummary", summaryService.getMarketSummary());
		String currentUserName = principal.getName();
		log.debug("User logged in: " + currentUserName);
		model.addAttribute("accounts",accountService.getAccounts(oAuth2AuthorizedClient));
		model.addAttribute("portfolio",portfolioService.getPortfolio(oAuth2AuthorizedClient));
		model.addAttribute("user", userService.getUser(currentUserName, oAuth2AuthorizedClient, principal));
		return "index";
	}


	@RequestMapping(value = "/registration", method = RequestMethod.GET)
	public String registration(Model model) {
// Path: src/main/java/io/pivotal/web/domain/RegistrationRequest.java
// @Data
// public class RegistrationRequest {
// 
//     @NotNull
//     @NotEmpty
//     private String givenNames;
// 
//     private String surname;
// 
//     @NotNull
//     @NotEmpty
//     @Email
//     private String email;
// 
//     @NotEmpty
//     @NotNull
//     //TODO - more complex validation on password
//     private String password;
// 
//     @NotEmpty
//     @NotNull
//     //TODO - more complex validation on password
//     private String confirmPassword;
// }


// Path: src/main/java/io/pivotal/web/controller/UserController.java
import io.pivotal.web.domain.RegistrationRequest;
import io.pivotal.web.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.validation.Valid;
import java.security.Principal;

	@Autowired
	private PortfolioService portfolioService;
	
	@Autowired
	private MarketSummaryService summaryService;
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String showHome(Model model,Principal princ,  @AuthenticationPrincipal OAuth2User principal){
		//check if user is logged in!
		if (principal != null) {
			return "redirect:/home";
		}
		model.addAttribute("marketSummary", summaryService.getMarketSummary());
		return "index";
	}

	@GetMapping("/home")
	public String authorizedHome(Model model, @AuthenticationPrincipal OAuth2User principal, @RegisteredOAuth2AuthorizedClient("pivotalbank") OAuth2AuthorizedClient oAuth2AuthorizedClient) {
		model.addAttribute("marketSummary", summaryService.getMarketSummary());
		String currentUserName = principal.getName();
		log.debug("User logged in: " + currentUserName);
		model.addAttribute("accounts",accountService.getAccounts(oAuth2AuthorizedClient));
		model.addAttribute("portfolio",portfolioService.getPortfolio(oAuth2AuthorizedClient));
		model.addAttribute("user", userService.getUser(currentUserName, oAuth2AuthorizedClient, principal));
		return "index";
	}


	@RequestMapping(value = "/registration", method = RequestMethod.GET)
	public String registration(Model model) {
		model.addAttribute("registration", new RegistrationRequest());