package controllers.guest_panel;

import authorization.Authenticator;
import helpers.SessionMessages;
import models.Message;
import models.Model;
import play.data.Form;
import play.data.FormFactory;
import play.db.Database;
import play.mvc.Controller;
import play.mvc.Http;
import play.mvc.Result;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ContactController  extends Controller {
    private final play.data.FormFactory formFactory;

    @Inject
    public ContactController(Database db, FormFactory formFactory) {
        this.formFactory = formFactory;
    }

    public Result get() {
        Form<Message> form = formFactory.form(Message.class);
        Http.Context context = Http.Context.current();
        List<MessageWrapper> messageWrapperList =  MessageWrapper.getHistory(Authenticator.getUser().id);
        return ok(views.html.guest_panel_views.contact.render(form, messageWrapperList, context.messages()));
    }

    public Result post() {
        Form<Message> form = formFactory.form(Message.class).bindFromRequest();
        Http.Context context = Http.Context.current();
        if(form.hasErrors()) {
            List<MessageWrapper> messageWrapperList =  MessageWrapper.getHistory(Authenticator.getUser().id);
            return ok(views.html.guest_panel_views.contact.render(form, messageWrapperList, context.messages()));
        }

        Message message = form.get();

        if(form.hasErrors()) {
            List<MessageWrapper> messageWrapperList =  MessageWrapper.getHistory(Authenticator.getUser().id);
            return ok(views.html.guest_panel_views.contact.render(form, messageWrapperList, context.messages()));
        }
        message.date = new Date().getTime();
        message.sender = Authenticator.getUser().id;
        message.addressee = Message.getManagerId();
        message.save();
        SessionMessages.addSuccess("Your message has been sent!");
        List<MessageWrapper> messageWrapperList =  MessageWrapper.getHistory(Authenticator.getUser().id);
        return ok(views.html.guest_panel_views.contact.render(form, messageWrapperList, context.messages()));
    }


    static public class MessageWrapper {
        public Message message;
        public boolean isMine;

        MessageWrapper() {}

        MessageWrapper(Message message, boolean isMine) {
            this.message = message;
            this.isMine = isMine;
        }

        static List<MessageWrapper> getHistory(Integer id) {
            List<Message> mineMessages = Model.<Message>findAll("sender", id, Message.class);
            List<Message> addressedToMe = Model.<Message>findAll("addressee", id, Message.class);

            List<MessageWrapper> res = new ArrayList<>();

            for(Message message : mineMessages) {
                res.add(new MessageWrapper(message, true));
            }

            for(Message message : addressedToMe) {
                res.add(new MessageWrapper(message, false));
            }

            res.sort((x,y) -> y.message.date.compareTo(x.message.date) );

            for(MessageWrapper msn : res) {
                System.out.println(msn.message.title);
            }

            return res;
        }
    }
}

@@ -1465,274 +1465,8 @@
 t();
-%0A%0A        if(form.hasErrors()) %7B%0A            List%3CMessageWrapper%3E messageWrapperList =  MessageWrapper.getHistory(Authenticator.getUser().id);%0A            return ok(views.html.guest_panel_views.contact.render(form, messageWrapperList, context.messages()));%0A        %7D
 %0A   
@@ -1820,37 +1820,44 @@
      return 
-ok(views.html
+redirect(controllers
 .guest_panel
@@ -1860,74 +1860,38 @@
 anel
-_views.c
+.routes.C
 ontact
-.render(form, messageWrapperList, context.messages()
+Controller.get(
 ));%0A
@@ -2815,122 +2815,8 @@
 );%0A%0A
-            for(MessageWrapper msn : res) %7B%0A                System.out.println(msn.message.title);%0A            %7D%0A%0A
     

refactor ContactController