package com.goafanti.comment.controller;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.aliyuncs.http.HttpRequest;
import com.goafanti.comment.bo.CommentInput;
import com.goafanti.comment.bo.CommentResult;
import com.goafanti.comment.service.CommentService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.mybatis.page.Pagination;


@RestController
@RequestMapping(value = "/api/user/comment")
public class UserCommentController extends BaseApiController{
	@Resource
	CommentService commentService;

	/**
	 * 新增评价
	 */
	@RequestMapping(value = "/apply", method = RequestMethod.POST)
	public Result addNewComment(HttpServletRequest request,CommentInput commentInput) {
		Result result=new Result();
		String ip=UserCommentController.getClientIp(request);
		if(StringUtils.isBlank(commentInput.getCommodityId())) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"商品id为空","商品id"));return result;
		}
		if(commentInput.getStar()==null || commentInput.getStar()<0 || commentInput.getStar()>5) {
			result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","评分"));return result;
		}
		if(StringUtils.isBlank(commentInput.getOrderNo())) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号"));return result;
		}
		if(StringUtils.isBlank(commentInput.getContent()) || commentInput.getContent().length()>512) {
			result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","内容限制1-512字符"));return result;
		}
		int res=commentService.addNewComment(commentInput,ip);
		if(res ==-1) {
			result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","提交失败"));return result;
		}
		result.setData(res);
		
		return result;
	}
	
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Result listComment(String commodityId,Integer pageNo,Integer pageSize) {
		Result result=new Result();
		if(StringUtils.isBlank(commodityId)) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","商品id"));
		}
		CommentResult commentResult=new CommentResult();
		commentResult.setPositiveCommentCount(commentService.getCommentCount(0, commodityId));
		commentResult.setOrdinaryCommentCount(commentService.getCommentCount(1, commodityId));
		commentResult.setNegativeCommentCount(commentService.getCommentCount(2, commodityId));
		commentResult.setComments(commentService.searchComment(commodityId, pageNo, pageSize));
		result.setData(commentResult);
		return result;
		
	}
	public static String getClientIp(HttpServletRequest request) {
		 String ip = request.getHeader("x-forwarded-for"); 
	       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	           ip = request.getHeader("Proxy-Client-IP"); 
	       } 
	       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	           ip = request.getHeader("WL-Proxy-Client-IP"); 
	       } 
	       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	           ip = request.getRemoteAddr(); 
	           if(ip.equals("127.0.0.1")){   
	        	   //根据网卡取本机配置的IP   
	        	   InetAddress inet=null;   
		           try {   
		        	   inet = InetAddress.getLocalHost();   
		           } catch (UnknownHostException e) {   
		        	   e.printStackTrace();   
		           }   
		           ip= inet.getHostAddress();   
	           }
	       } 
	       // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
	       if(ip != null && ip.length() > 15){  
	           if(ip.indexOf(",")>0){   
	               ip = ip.substring(0,ip.indexOf(","));   
	           }   
	       }   
	       return ip; 

    }
}
