package org.bench4q.master.api;

import java.util.ArrayList;
import java.util.List;

import org.bench4q.master.domain.entity.Port;
import org.bench4q.master.domain.factory.BusinessModelMapFactory;
import org.bench4q.master.domain.service.PortPoolService;
import org.bench4q.master.domain.service.UserService;
import org.bench4q.share.models.master.OrganizeRecordPortResponseModel;
import org.bench4q.share.models.master.PortModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/RecordPort")
public class RecordPortController extends BaseController {

	private PortPoolService portPoolService = new PortPoolService();
	private static Object syncObject = new Object();

	public PortPoolService getPortPoolService() {
		return portPoolService;
	}

	@Autowired
	private void setPortPoolService(PortPoolService portPoolService) {
		this.portPoolService = portPoolService;
	}

	public static Object getSyncObject() {
		return syncObject;
	}

	public static void setSyncObject(Object syncObject) {
		RecordPortController.syncObject = syncObject;
	}

	@RequestMapping(value = "/addPortToPortPool", method = {
			RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public OrganizeRecordPortResponseModel addPortToPortPool(
			@RequestParam int port) {

		if (!this.checkScope(UserService.SUPER_AUTHENTICATION)) {
			return _buildResponseModel(false,
					"you don't hava the power to add port to pool!",
					new ArrayList<PortModel>());
		}

		if (!this.getPortPoolService().addPortToDBPool(port)) {
			return _buildResponseModel(false, "add to DB pool fails",
					new ArrayList<PortModel>());
		}
		return _buildResponseModel(true, "", new ArrayList<PortModel>());
	}

	@RequestMapping(value = "/removePortFromPool", method = RequestMethod.POST)
	@ResponseBody
	public OrganizeRecordPortResponseModel removePortFromPool(
			@RequestParam int portId) {
		if (!this.checkScope(UserService.SUPER_AUTHENTICATION)) {
			return _buildResponseModel(false,
					"you don't hava the power to remove port from pool!",
					new ArrayList<PortModel>());
		}
		if (!this.getPortPoolService().removePortFromDBPool(portId)) {
			return _buildResponseModel(false, "remove from local fails",
					new ArrayList<PortModel>());
		}
		return _buildResponseModel(true, "", new ArrayList<PortModel>());
	}

	@RequestMapping(value = "/loadPortList", method = RequestMethod.POST)
	@ResponseBody
	public OrganizeRecordPortResponseModel loadPortList() {
		if (!this.checkScope(UserService.SUPER_AUTHENTICATION)) {
			return _buildResponseModel(false,
					"you don't hava the power to load!",
					new ArrayList<PortModel>());
		}
		List<PortModel> portModels = new ArrayList<PortModel>();
		for (Port port : this.portPoolService.loadPortList()) {
			portModels.add(BusinessModelMapFactory.toModel(port));
		}
		return this._buildResponseModel(true, "", portModels);
	}

	@RequestMapping(value = "/editPort", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public OrganizeRecordPortResponseModel editPort(@RequestParam PortModel port) {
		if (!this.checkScope(UserService.SUPER_AUTHENTICATION)) {
			return _buildResponseModel(false, "you don't have power to edit",
					new ArrayList<PortModel>());
		}

		return this._buildResponseModel(
				this.getPortPoolService().editPort(
						BusinessModelMapFactory.toBusiness(port)), "",
				new ArrayList<PortModel>());
	}

	private OrganizeRecordPortResponseModel _buildResponseModel(
			boolean success, String failString, List<PortModel> ports) {
		OrganizeRecordPortResponseModel result = new OrganizeRecordPortResponseModel();
		result.setSuccess(success);
		result.setFailCauseString(failString);
		result.setPortModels(ports);
		return result;
	}
}
