package com.cab.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.RequestParam;

import com.cab.model.CabThirdParty;
import com.cab.model.User;
import com.cab.service.CabMyBoundServiceImpl;
import com.cab.service.UserService;
import com.cku.core.RESTResponse;
import com.cku.util.ServletUtils;

@Controller
@RequestMapping("myBound")
public class CabMyBoundController {

	@Autowired
	public CabMyBoundServiceImpl cabMyBoundService;
	
	/**
	 * 
	 * @Description：查询我的账号绑定
	 * @author: zhuoHeng
	 * @version: 2016年4月13日 下午4:39:50
	 */
	@RequestMapping("/getMyBoundMessage")
	public void getMyBoundMessage(HttpServletResponse response,HttpServletRequest request) throws IOException{
		RESTResponse result = null;
		try
		{
			Long userId = UserService.verifyUserId(request);
			CabThirdParty obj = cabMyBoundService.getMyBoundMessage(new Long(userId).intValue());
			result = new RESTResponse("item", obj);
		}
		catch (Exception e)
		{
			result = new RESTResponse(e);
		}
		
		ServletUtils.writeResponse(response, result);
	}
	/**
	 * 
	 * @Description：绑定第三方账号
	 * @author: zhuoHeng
	 * @version: 2016年4月15日 下午5:10:08
	 * @throws IOException 
	 */
	@RequestMapping("/bindAccount")
	public void bindAccount(HttpServletResponse response,HttpServletRequest request,
			@RequestParam(value = "flag", required = false)String flag,
			@RequestParam(value = "value", required = false)String value)throws IOException{
		RESTResponse result = null;
		try
		{
			Long userId = UserService.verifyUserId(request);
			CabThirdParty cabThirdParty = new CabThirdParty();
			cabThirdParty.setCabUserId(new Long(userId).intValue());
			cabMyBoundService.bindAccount(cabThirdParty,flag,value);
			result = new RESTResponse();
		}
		catch (Exception e)
		{
			result = new RESTResponse(e);
		}
		
		ServletUtils.writeResponse(response, result);
	}
	
	/**
	 * 
	 * @Description：解绑第三方账号
	 * @author: zhuoHeng
	 * @version: 2016年4月15日 下午5:10:32
	 * @throws IOException 
	 */
	@RequestMapping("/unbindAccount")
	public void unbindAccount(HttpServletResponse response,HttpServletRequest request,
			@RequestParam(value = "flag", required = false)String flag) throws IOException{
		RESTResponse result = null;
		try
		{
			Long userId = UserService.verifyUserId(request);
			CabThirdParty cabThirdParty = new CabThirdParty();
			cabThirdParty.setCabUserId(new Long(userId).intValue());
			cabMyBoundService.unbindAccount(cabThirdParty,flag);
			result = new RESTResponse();
		}
		catch (Exception e)
		{
			result = new RESTResponse(e);
		}
		
		ServletUtils.writeResponse(response, result);
	}
	
	/**
	 * 
	 * @Description：账号手机号绑定
	 * @author: zhuoHeng
	 * @version: 2016年4月15日 下午5:10:56
	 * @throws IOException 
	 */
	@RequestMapping("/bindPhone")
	public void bindPhone(HttpServletResponse response,HttpServletRequest request) throws IOException{
		RESTResponse result = null;
		try
		{
			Long userId = UserService.verifyUserId(request);
			String realName = ServletUtils.getParameter(request, "realName",null);
			String phone = ServletUtils.getParameter(request, "phone");
			String validation_code = ServletUtils.getParameter(request, "validation_code");
			User user = new User();
			user.setId(userId);
			user.setPhone(phone);
			user.setRealName(realName);
			cabMyBoundService.bindPhone(user,validation_code);
			result = new RESTResponse();
		}
		catch (Exception e)
		{
			result = new RESTResponse(e);
		}
		
		ServletUtils.writeResponse(response, result);
	}
	
	/**
	 * 
	 * @Description：账号手机号解绑
	 * @author: zhuoHeng
	 * @version: 2016年4月15日 下午5:12:02
	 */
	@RequestMapping("/unbindPhone")
	public void unbindPhone(HttpServletResponse response,HttpServletRequest request){
		
	}
	/**
	 * 第三方登录
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping("/thirdLogin")
	public void thirdLogin(HttpServletResponse response,HttpServletRequest request) throws IOException{
		RESTResponse result = null;
		try{
			String type = ServletUtils.getParameter(request, "type");
			String openId = ServletUtils.getParameter(request, "openId");
			String name = ServletUtils.getParameter(request, "name");
			String avatar = ServletUtils.getParameter(request, "avatar");
			String ip = request.getRemoteAddr();
			User user = cabMyBoundService.addWechatLogin(type,ip,openId,name,avatar);
			result = new RESTResponse("items",user);
		}catch (Exception e){
			result = new RESTResponse(e);
		}
		
		ServletUtils.writeResponse(response, result);
	
	}
	
	
}
