package ru.qatools.school.vorobushek.resources;

import org.glassfish.jersey.server.mvc.ErrorTemplate;
import org.glassfish.jersey.server.mvc.Template;
import org.javalite.activejdbc.LazyList;
import ru.qatools.school.vorobushek.models.User;
import ru.qatools.school.vorobushek.models.UserContext;
import ru.qatools.school.vorobushek.service.DatabaseProvider;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import static java.lang.String.format;

/**
 * eroshenkoam
 * 15/11/14
 */
@Path("/")
@Produces(value = javax.ws.rs.core.MediaType.TEXT_HTML)
@ErrorTemplate(name = "/error.ftl")
public class IndexResource {

    @Context HttpServletRequest httpRequest;

    @GET
    @Path("/")
    @Template(name = "/index.ftl")
    public UserContext showIndex(){
        return DatabaseProvider.getUserContext(httpRequest);
    }

    @GET
    @Path("/login")
    public Response showLogin(@Context UriInfo uriInfo) {

        MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();

        if (queryParams.containsKey("code")) {

            if (DatabaseProvider.isTestingMode()){
                String login = "test" + queryParams.get("code").toString().replace("[", "").replace("]","");
                String email = login + "@yandex.ru";
                String displayName = login.toUpperCase();
                String id = "0";
                UserContext userContext = new UserContext(DatabaseProvider.getUser(login, email, displayName, id));
                DatabaseProvider.setUserContext(httpRequest, userContext);
            }
            else {
                String responseCode = queryParams.get("code").toString().replace("[", "").replace("]","");
                String token = DatabaseProvider.getYandexToken(responseCode);
                UserContext userContext = new UserContext(token);
                DatabaseProvider.setUserContext(httpRequest, userContext);
            }

        }

        return javax.ws.rs.core.Response.seeOther(URI.create("/")).build();

    }

//    @GET
//    @Path("/testlogin/{login}/{password}/{email}")
//    public Response showTestLogin(@PathParam("login") String login
//            , @PathParam("password") String password
//            , @PathParam("email") String email) {
//
//        User tester = new User();
//
//        LazyList<User> users = User.findBySQL(format(
//                "select * from users where login='%s' and email='%s' and displayName='%s'", login, email, login));
//
//        if (!users.isEmpty()) {
//            tester = users.get(0);
//        }
//
//        tester.setLogin(login);
//        tester.setEmail(email);
//        tester.setDisplayName(login);
//        tester.saveIt();
//        DatabaseProvider.setUserContext(httpRequest, new UserContext(tester));
//
//        return javax.ws.rs.core.Response.seeOther(URI.create("/")).build();
//    }


    @GET
    @Path("/logout")
    public Response logout() {
        DatabaseProvider.setUserContext(httpRequest, new UserContext());
        return javax.ws.rs.core.Response.seeOther(URI.create("/")).build();
    }
}

@@ -2432,858 +2432,8 @@
  %7D%0A%0A
-//    @GET%0A//    @Path(%22/testlogin/%7Blogin%7D/%7Bpassword%7D/%7Bemail%7D%22)%0A//    public Response showTestLogin(@PathParam(%22login%22) String login%0A//            , @PathParam(%22password%22) String password%0A//            , @PathParam(%22email%22) String email) %7B%0A//%0A//        User tester = new User();%0A//%0A//        LazyList%3CUser%3E users = User.findBySQL(format(%0A//                %22select * from users where login='%25s' and email='%25s' and displayName='%25s'%22, login, email, login));%0A//%0A//        if (!users.isEmpty()) %7B%0A//            tester = users.get(0);%0A//        %7D%0A//%0A//        tester.setLogin(login);%0A//        tester.setEmail(email);%0A//        tester.setDisplayName(login);%0A//        tester.saveIt();%0A//        DatabaseProvider.setUserContext(httpRequest, new UserContext(tester));%0A//%0A//        return javax.ws.rs.core.Response.seeOther(URI.create(%22/%22)).build();%0A//    %7D%0A%0A
 %0A   

remove old code (login test user)