package com.example.bot.spring;

import com.example.bot.spring.database.DBEngine;
import com.example.bot.spring.textsender.*;

public class TextProcessor {

	DBEngine DBE;
	public TextProcessor() {
		// TODO Auto-generated constructor stub
		DBE=new DBEngine();
	}
	
	private String classifyText(String userId, String text){
		String reply="";
		String tag;
		try {
			SQTextSender sqsender = new SQTextSender();
			reply = sqsender.process(userId, text)+"\n";
		}catch(Exception e) {}
		try {	
			if(text.toLowerCase().contains("recommend")||
			   text.toLowerCase().contains("do you have")) {
				RecommendationTextSender rsender = new RecommendationTextSender();
				//reply += rsender.process(userId, text);
				DBE.updateLineUserInfo(userId,"categorization","reco");
				return "in recomend";
			}

			if(text.toLowerCase().contains("tell me")) {
				GQTextSender gqsender = new GQTextSender();
				//reply += gqsender.process(userId, text);
				DBE.updateLineUserInfo(userId,"categorization", "gq");
				return "in general q";
			}

			if(text.toLowerCase().contains("book")) {
				BookingTextSender bsender = new BookingTextSender();
				//reply += bsender.process(userId, text);
				DBE.updateLineUserInfo(userId,"categorization","book");
				return "in booking";
			}
			
			tag=DBE.getLineUserInfo(userId,"categorization");
			switch(tag) {
			case "reco":
				RecommendationTextSender rsender = new RecommendationTextSender();
				reply += rsender.process(userId, text);
				//return "in recomend via tag";
				break;
			case "book":
				BookingTextSender bsender = new BookingTextSender();
				reply += bsender.process(userId, text);
				//return "in booking via tag";
				break;
			case "gq":
				GQTextSender gqsender = new GQTextSender();
				reply += gqsender.process(userId, text);
				//return "in general q via tag";
				break;
			default:
				reply += "Could you please specify that you want some recommendation, asking some general question, or booking a trip";
			}
			return reply;
			
		} catch (Exception e) {
			UQAutomateSender uqSender = new UQAutomateSender();
			reply = uqSender.process(userId, text);
			//return "exception here";
			return reply;
		}

	}

	public String processText(String userId, String text) throws Exception{
		DBE.updateLineUserInfo(userId,"lastq",text);
		if (text==null) {// yet text won't be null?
			throw new Exception("no input");
		}
		String reply=classifyText(userId,text);
		DBE.updateLineUserInfo(userId,"lasta",reply);
		return reply;
	}
}

@@ -765,21 +765,41 @@
 %22reco%22);
-%0A
 %09%09%09%09
+%0A%09%09%09%09return reply; //
 return %22
@@ -1019,21 +1019,41 @@
 , %22gq%22);
-%0A
 %09%09%09%09
+%0A%09%09%09%09return reply; //
 return %22
@@ -1280,21 +1280,41 @@
 %22book%22);
-%0A
 %09%09%09%09
+%0A%09%09%09%09return reply; //
 return %22
@@ -1527,37 +1527,49 @@
 s(userId, text);
+%09%09%09%09
 %0A%09%09%09%09
+break;  
 //return %22in rec
@@ -1580,35 +1580,24 @@
 d via tag%22;%0A
-%09%09%09%09break;%0A
 %09%09%09case %22boo
@@ -1692,37 +1692,48 @@
 s(userId, text);
+%09%09%09%09
 %0A%09%09%09%09
+break; 
 //return %22in boo
@@ -1747,27 +1747,16 @@
 a tag%22;%0A
-%09%09%09%09break;%0A
 %09%09%09case 
@@ -1853,21 +1853,32 @@
 , text);
+%09%09%09%09
 %0A%09%09%09%09
+break; 
 //return
@@ -1906,19 +1906,8 @@
 g%22;%0A
-%09%09%09%09break;%0A
 %09%09%09d
@@ -2187,20 +2187,37 @@
 , text);
+%09%09%09
 %0A%09%09%09
+return reply; 
 //return
@@ -2235,33 +2235,16 @@
  here%22;%0A
-%09%09%09return reply;%0A
 %09%09%7D%0A%0A%09%7D%0A

fix some bugs