/**
 * Yona, 21st Century Project Hosting SW
 * <p>
 * Copyright Yona & Yobi Authors & NAVER Corp.
 * https://yona.io
 **/
package controllers;

import com.feth.play.module.pa.PlayAuthenticate;
import controllers.annotation.AnonymousCheck;
import jsmessages.JsMessages;
import models.Project;
import models.UserCredential;
import play.Logger;
import play.mvc.Controller;
import play.mvc.Http;
import play.mvc.Result;
import playRepository.RepositoryService;
import views.html.error.notfound_default;
import views.html.index.index;

import static com.feth.play.module.pa.controllers.Authenticate.*;

public class Application extends Controller {
    public static final String FLASH_MESSAGE_KEY = "message";
    public static final String FLASH_ERROR_KEY = "error";

    @AnonymousCheck
    public static Result index() {
        return ok(index.render(UserApp.currentUser()));
    }

    public static Result oAuth(final String provider) {
        return authenticate(provider);
    }

    public static Result oAuthLogout() {
        UserApp.logout();
        return logout();
    }

    public static Result oAuthDenied(final String providerKey) {
        noCache(response());
        flash(FLASH_ERROR_KEY,
                "You need to accept the OAuth connection in order to use this website!");
        return redirect(routes.Application.index());
    }

    public static UserCredential getLocalUser(final Http.Session session) {
        final UserCredential localUser = UserCredential.findByAuthUserIdentity(PlayAuthenticate
                .getUser(session));
        return localUser;
    }

    public static UserCredential getLocalUser() {
        final UserCredential localUser = UserCredential.findByAuthUserIdentity(PlayAuthenticate
                .getUser(session()));
        return localUser;
    }

    public static Result removeTrailer(String paths){
        String path = request().path();
        if( path.charAt(path.length()-1) == '/' ) {
            path = path.substring(0, path.length() - 1);
        } else {
            Logger.error("Unexpected url call : " + request().path());
            return notFound(notfound_default.render("error.notfound"));
        }
        Logger.debug("Trailing slash removed and redirected: " + request().path() + " to " + path );
        return redirect(path);
    }

    public static Result init() {
        makeTestRepository();
        return redirect(routes.Application.index());
    }

    static final JsMessages messages = JsMessages.create(play.Play.application());

    public static Result jsMessages() {
        return ok(messages.generate("Messages")).as("application/javascript");
    }

    private static void makeTestRepository() {
        for (Project project : Project.find.all()) {
            Logger.debug("makeTestRepository: " + project.name);
            try {
                RepositoryService.createRepository(project);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static Result navi() {
        return ok(index.render(UserApp.currentUser()));
    }

    public static Result UIKit(){
        return ok(views.html.help.UIKit.render());
    }

    public static Result fake() {
        // Do not call this.
        return badRequest();
    }
}

@@ -316,16 +316,61 @@
 ential;%0A
+import org.apache.commons.lang3.StringUtils;%0A
 import p
@@ -1103,21 +1103,48 @@
     
-return logout
+logout();%0A        return returnToReferer
 ();%0A
@@ -3363,11 +3363,167 @@
 );%0A    %7D
+%0A%0A    public static Result returnToReferer() %7B%0A        return redirect(StringUtils.defaultString(request().getHeader(%22referer%22), index().toString()));%0A    %7D
 %0A%7D%0A

Return to current page when logged out