<%--
 ~
 ~ Copyright (c) 2021, WSO2 Inc. (http://www.wso2.com). All Rights Reserved.
 ~
 ~ This software is the property of WSO2 Inc. and its suppliers, if any.
 ~ Dissemination of any information or reproduction of any material contained
 ~ herein in any form is strictly forbidden, unless permitted by WSO2 expressly.
 ~ You may not alter or remove any copyright or other notice from copies of this content."
 ~
--%>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="e" uri="https://www.owasp.org/index.php/OWASP_Java_Encoder_Project" %>
<jsp:directive.include file="../includes/init-url.jsp"/>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="static org.wso2.carbon.identity.core.util.IdentityUtil.fillURLPlaceholders" %>
<%@ page import="org.wso2.carbon.identity.mgt.endpoint.util.client.ApplicationDataRetrievalClient" %>
<%@ page import="org.wso2.carbon.identity.mgt.endpoint.util.client.ApplicationDataRetrievalClientException" %>
<%@ page import="org.wso2.carbon.identity.mgt.endpoint.util.IdentityManagementEndpointUtil" %>

<%
    String applicationAccessURLWithoutEncoding = null;
    try {
        // Redirection URL of the back-to-sign resolves from the contextProperties.
        // If not possible, the URL will be resolved to URL of the Console.
        String sp = "console";
        if (request.getAttribute("data") instanceof Map) {
        	Map data = (Map) request.getAttribute("data");
           	if (data.get("inputs") instanceof ArrayList<?>) {
           	    ArrayList<Map> inputs = (ArrayList<Map>) data.get("inputs");
           	    if (inputs.get(0).containsKey("sp")){
       		    	sp = inputs.get(0).get("sp").toString();
        	    }
        	}
        }

        ApplicationDataRetrievalClient applicationDataRetrievalClient = new ApplicationDataRetrievalClient();
        applicationAccessURLWithoutEncoding = applicationDataRetrievalClient.getApplicationAccessURL(tenantDomain, sp);
        applicationAccessURLWithoutEncoding = IdentityManagementEndpointUtil.replaceUserTenantHintPlaceholder(
                                                                applicationAccessURLWithoutEncoding, userTenantDomain);
    } catch (ApplicationDataRetrievalClientException e) {
        // Ignored and fallback to login page url.
    }
%>

<head>
    <script src="../js/scripts.js"></script>
</head>

<script>
    $(".segment").show();

    function nextClick(key) {
        var input = document.createElement("input");
        input.setAttribute("type", "hidden");
        input.setAttribute("name", "action." + key);
        var parent = document.getElementById("account-link");
        parent.appendChild(input);
    }
</script>

<div>
    <h3 class="ui header">
        We found an Asgardeo account with this email!
    </h3>
</div>

<div class="boarder-all">
    <div class="clearfix"></div>
    <div class="padding-double login-form" id="samplet">

        <form action="<%=commonauthURL%>" method="POST">
            <div id="account-link">
                <c:forEach var="input" items='${requestScope.data["inputs"]}'>
                    <div id="account-link-message" class="col-xs-12 col-sm-12 col-md-12 col-lg-12 form-group required">
                        <p style="text-align:left;">Looks like you are signing in with <b>${input.accountType}</b> for
                            the first time!
                        </p>

                        <c:choose>
                            <c:when test="${(empty input.linkingMethod) || (input.linkingMethod eq 'LOCAL')}">
                                <p style="text-align:left;">You already have an Asgardeo account
                                that uses this email address <b>${input.email}</b>.
                                </p>
                            </c:when>
                            <c:otherwise>
                                <p style="text-align:left;">You already have an Asgardeo account signed up via
                                    <b>${input.linkingMethod}</b> that uses this email address <b>${input.email}</b>.
                                </p>
                            </c:otherwise>
                        </c:choose>

                        <c:choose>
                            <c:when test="${(empty input.linkingMethod) || (input.linkingMethod eq 'LOCAL')}">
                                <p style="text-align:left;">Click <b>Next</b> to link the Asgardeo account to your
                                <c:if test="${input.accountType ne 'Google'}">
                                    <b>${input.userName}</b>
                                </c:if>
                                ${input.accountType} account.
                                </p>
                            </c:when>
                            <c:otherwise>
                                <p style="text-align:left;">Click <b>Next</b> to sign in with
                                    <b>${input.linkingMethod}</b> to confirm linking to the existing account.
                                </p>
                            </c:otherwise>
                        </c:choose>
                        </br>
                        <button onClick="nextClick('${input.id}')"
                                data-testid="start-account-link-message-next"
                                class="ui primary fluid large button"
                                value="${input.id}">
                                ${input.label}
                        </button>
                        </br>
                        <a class="clickable-link"
                           data-testid="start-account-link-message-back-to-sign-in-link"
                           href="<%=IdentityManagementEndpointUtil.getURLEncodedCallback(applicationAccessURLWithoutEncoding)%>?prompt=login">Back to Sign In</a>
                    </div>
                </c:forEach>
                <input type="hidden" id="promptResp" name="promptResp" value="true">
                <input type="hidden" id="promptId" name="promptId" value="${requestScope.promptId}">
            </div>
        </form>
        <div class="clearfix"></div>
    </div>
</div>
