package org.bench4q.master.api;

import java.util.List;

import org.apache.log4j.Logger;
import org.bench4q.master.domain.service.PluginService;
import org.bench4q.master.domain.service.UserService;
import org.bench4q.master.exception.Bench4QException;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.share.models.master.plugin.BehaviorInfoModel;
import org.bench4q.share.models.master.plugin.PluginResponseModel;
import org.bench4q.share.models.master.plugin.PluginUIModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/plugin")
public class PluginController extends BaseController {
	private Logger logger = Logger.getLogger(PluginController.class);
	private PluginService pluginService;

	public PluginService getPluginService() {
		return pluginService;
	}

	@Autowired
	public void setPluginService(PluginService pluginService) {
		this.pluginService = pluginService;
	}

	@RequestMapping(value = "/loadPluginList", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public PluginResponseModel loadPluginUIModels() throws Bench4QException {
		if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) {
			throw new Bench4QException(400 + "", "not permitted",
					"/loadPluginList");
		}
		PluginResponseModel pluginResponseModel = new PluginResponseModel();
		try {
			List<PluginUIModel> pluginUIModels = this.getPluginService()
					.loadPluginUIs();
			if (pluginUIModels == null) {
				pluginResponseModel.setSuccess(false);
			} else {
				pluginResponseModel.setSuccess(true);
				pluginResponseModel.setPluginUIModels(pluginUIModels);
			}
			return pluginResponseModel;
		} catch (Exception e) {
			logger.info(ExceptionLog.getStackTrace(e));
			pluginResponseModel.setSuccess(false);
			return pluginResponseModel;
		}

	}

	@RequestMapping("/loadPluginNameList")
	@ResponseBody
	public PluginResponseModel loadPluginList() throws Bench4QException {
		if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) {
			throw new Bench4QException(400 + "", "not permitted",
					"/loadPluginNameList");
		}
		PluginResponseModel pluginResponseModel = new PluginResponseModel();
		pluginResponseModel.setPluginList(this.getPluginService()
				.getPluginNameList());

		pluginResponseModel.setSuccess(true);
		return pluginResponseModel;
	}

	@RequestMapping(value = "/getPlugin/{pluginName}", method = {
			RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public PluginResponseModel getPlugnin(
			@PathVariable("pluginName") String pluginName)
			throws Bench4QException {
		if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) {
			throw new Bench4QException(400 + "", "not permitted",
					"/loadBehaviors/{pluginName}");
		}
		PluginResponseModel pluginResponseModel = new PluginResponseModel();
		PluginUIModel pluginUIModel = this.getPluginService().getPluginUIModel(
				pluginName);
		if (pluginUIModel == null) {
			pluginResponseModel.setSuccess(false);
			pluginResponseModel.setFailMessage("no such plugin");
		} else {
			pluginResponseModel.setSuccess(true);
			pluginResponseModel.setPluginInfoModel(pluginUIModel
					.getPluginInfoModel());
		}
		return pluginResponseModel;
	}

	@RequestMapping(value = "/loadBehaviors/{pluginName}", method = {
			RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public PluginResponseModel getBehaviorsInPlugin(
			@PathVariable("pluginName") String pluginName)
			throws Bench4QException {

		if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) {
			throw new Bench4QException(400 + "", "not permitted",
					"/loadBehaviors/{pluginName}");
		}
		PluginResponseModel pluginResponseModel = new PluginResponseModel();
		try {
			if (!this.getPluginService().isPluginExist(pluginName)) {
				pluginResponseModel.setSuccess(false);
				pluginResponseModel.setFailMessage("plugin not exist");
				return pluginResponseModel;
			}

			List<BehaviorInfoModel> behaviorInfoModels = this
					.getPluginService().loadBehaviorInfoModels(pluginName);
			if (behaviorInfoModels == null) {
				pluginResponseModel.setFailMessage("no behaviors in plugin");
				pluginResponseModel.setSuccess(false);
			}

			else {
				pluginResponseModel.setSuccess(true);
				pluginResponseModel.setBehaviorInfoModels(behaviorInfoModels);
			}

			return pluginResponseModel;
		} catch (Exception e) {
			logger.error(ExceptionLog.getStackTrace(e));
			pluginResponseModel.setSuccess(false);
			return pluginResponseModel;
		}
	}

	@RequestMapping(value = "/addPlugin", method = { RequestMethod.PUT })
	@ResponseBody
	public PluginResponseModel addPlugin(@RequestBody String plugin)
			throws Bench4QException {
		if (!this.checkScope(UserService.SUPER_AUTHENTICATION)) {
			throw new Bench4QException(400 + "", "not permitted",
					"addPlugin/{pluginName}/{methodName}");
		}
		String pluginUIContent = plugin;
		logger.info(pluginUIContent);
		PluginResponseModel pluginResponseModel = new PluginResponseModel();
		pluginResponseModel.setSuccess(this.getPluginService().addPlugin(
				pluginUIContent));

		return pluginResponseModel;

	}

	@RequestMapping(value = "/deletePlugin/{pluginName}", method = {
			RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public PluginResponseModel deletePlugin(
			@PathVariable("pluginName") String pluginName)
			throws Bench4QException {
		if (!this.checkScope(UserService.SUPER_AUTHENTICATION)) {
			throw new Bench4QException(400 + "", "not permitted",
					"deletePlugin/{pluginName}");
		}
		PluginResponseModel pluginResponseModel = new PluginResponseModel();
		pluginResponseModel.setSuccess(this.getPluginService().deletePlugin(
				pluginName));
		return pluginResponseModel;

	}

}
