package com.iamberry.app.api.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
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;

import com.iamberry.app.api.util.Utility;
import com.iamberry.app.config.Response;
import com.iamberry.app.core.entity.Machine;

/**
 * 	机器操作接口
 * @author Moon Cheng
 * @date 2016年3月23日 下午2:22:25
 */
@Controller
@RequestMapping("/secure/machine")
public class MachineController extends BaseController {
	@InitBinder("machine")
	public void initBinderMachine(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("machine.");
	}

	/**
	 * 设置机器信息
	 *
	 * @author Moon Cheng
	 * @param machine 机器信息
	 * @param token 用户token
	 * @return
	 */
	@RequestMapping(value = "/set_machine", method = RequestMethod.POST)
	@ResponseBody
	private Response setMachine(Machine machine, @RequestParam("token") String token) {
		String ip = Utility.getIp(request);
		machine = machineService.setMachine(machine, token, ip);
		if (machine == null) {
			return Response.INVALID_TOKEN;
		}
		return Response.SUCCESS.setData(machine, 1);
	}

	/**
	 * 更新机器状态
	 * @author Moon Cheng
	 * @param machineId 机器id
	 * @param status 机器状态
	 * @param token 唯一标识符
	 * @return 2100：无效token； 1000：请求成功；
	 */
	@RequestMapping(value = "/update_machine_status", method = RequestMethod.POST)
	@ResponseBody
	private Response updateMachineStatus(@RequestParam("machine_id") Long machineId,
			@RequestParam("status") String status, @RequestParam("token") String token) {
		int reslut = machineService.updateMachineStatus(machineId, status, token);
		if (reslut == -1) {
			return Response.INVALID_TOKEN;
		}
		return Response.SUCCESS.setData(null);
	}

	/**
	 * 更新机器地理位置
	 * @author Moon Cheng
	 * @param machineId 机器id
	 * @param location 机器位置
	 * @param token 唯一标识符
	 * @return
	 */
	@RequestMapping(value = "/update_machine_location", method = RequestMethod.POST)
	@ResponseBody
	private Response updateMachineLocation(@RequestParam("machine_id") Long machineId,
			@RequestParam("location") String location, @RequestParam("token") String token) {
		int reslut = machineService.updateMachineLocation(machineId, location, token);
		if (reslut == -1) {
			return Response.INVALID_TOKEN;
		}
		return Response.SUCCESS.setData(null);
	}

	/**
	 * 按用户，查询机器
	 * @author Moon Cheng
	 * @param owner 用户id
	 * @param token 唯一标识符
	 * @return
	 */
	@RequestMapping(value = "/search_machine_by_owner", method = RequestMethod.POST)
	@ResponseBody
	private Response searchMachineByOwner(@RequestParam("user_id") Long owner, @RequestParam("token") String token) {
		List<Machine> machines = machineService.searchMachineByOwner(owner, token);
		if (machines == null) {
			return Response.INVALID_TOKEN;
		}
		return Response.SUCCESS.setData(machines, machines.size());
	}

	/**
	 * 按机器id，查询机器
	 * @author Moon Cheng
	 * @param machineId 机器id
	 * @param token 用户唯一标识符
	 * @return
	 */
	@RequestMapping(value = "/search_machine_by_id", method = RequestMethod.POST)
	@ResponseBody
	private Response searchMachineById(@RequestParam("machine_id") Long machineId,
			@RequestParam("token") String token) {
		Machine machine = machineService.searchMachineById(machineId, token);
		if (machine == null) {
			return Response.INVALID_TOKEN;
		}
		return Response.SUCCESS.setData(machine, 1);
	}

	/**
	 * updateStatus
	 * 更改机器在线状态
	 * @param status
	 * @return
	 */
	@RequestMapping(value = "/update_status", method = RequestMethod.POST)
	@ResponseBody
	private Response updateStatus(@RequestParam("status") String status) {
		machineService.updateStatus(status);
		return Response.SUCCESS;
	}
}
