package com.happy.action;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.happy.Model.BookingComment;
import com.happy.Model.weixin.Users;
import com.happy.Until.*;
import com.happy.dto.BookCommentDto;
import com.happy.dto.HotelEto;
import com.happy.dto.IPage;
import com.happy.dto.OrderRateDto;
import com.happy.service.BookingCommentService;
import com.happy.service.UserService;
import com.happy.vo.*;
import com.opensymphony.xwork2.ActionSupport;
import com.alibaba.fastjson.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.BeanUtils;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 评价
 */

public class AppBookingCommentAction extends ActionSupport implements ServletRequestAware {

    private HttpServletRequest request;

    public HttpServletResponse response;

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    @Resource
    public BookingCommentService bookingCommentService;

    @Resource
    public UserService userService;

    //    回复评论id
    public String commentId;
    //    回复主评论id
    public String commentParentId;
    //    回复人名称
    public String commentName;

    //    评论订单id
    public String bookingId;

    //    评论民宿id
    public String hotelId;

    //    评论房型id
    public String houseId;

    //    内容
    public String content;

    //    评价状态
    public String commentStatus;

    //    总评分
    public Double score;

    //    卫生评分
    public Double scoreWs;

    //    服务评分
    public Double scoreFw;

    //    设施评分
    public Double scoreSs;

    //    位置评分
    public Double scoreWz;

    //    创建人
    public Integer createId;

    public String createUsername;

    //    创建时间
    public String createDate;

    //    修改时间
    public String modifyDate;

    public List<String> pictureList;

    public List<String> getPictureList() {
        return pictureList;
    }

    public void setPictureList(List<String> pictureList) {
        this.pictureList = pictureList;
    }

    public String getBookingId() {
        return bookingId;
    }

    public void setBookingId(String bookingId) {
        this.bookingId = bookingId;
    }

    public String getCommentId() {
        return commentId;
    }

    public void setCommentId(String commentId) {
        this.commentId = commentId;
    }

    public String getCommentParentId() {
        return commentParentId;
    }

    public void setCommentParentId(String commentParentId) {
        this.commentParentId = commentParentId;
    }

    public String getCommentName() {
        return commentName;
    }

    public void setCommentName(String commentName) {
        this.commentName = commentName;
    }

    public String getHotelId() {
        return hotelId;
    }

    public void setHotelId(String hotelId) {
        this.hotelId = hotelId;
    }

    public String getHouseId() {
        return houseId;
    }

    public void setHouseId(String houseId) {
        this.houseId = houseId;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Double getScore() {
        return score;
    }

    public void setScore(Double score) {
        this.score = score;
    }

    public Double getScoreWs() {
        return scoreWs;
    }

    public void setScoreWs(Double scoreWs) {
        this.scoreWs = scoreWs;
    }

    public Double getScoreFw() {
        return scoreFw;
    }

    public void setScoreFw(Double scoreFw) {
        this.scoreFw = scoreFw;
    }

    public Double getScoreSs() {
        return scoreSs;
    }

    public void setScoreSs(Double scoreSs) {
        this.scoreSs = scoreSs;
    }

    public Double getScoreWz() {
        return scoreWz;
    }

    public void setScoreWz(Double scoreWz) {
        this.scoreWz = scoreWz;
    }

    public Integer getCreateId() {
        return createId;
    }

    public void setCreateId(Integer createId) {
        this.createId = createId;
    }

    public String getCreateUsername() {
        return createUsername;
    }

    public void setCreateUsername(String createUsername) {
        this.createUsername = createUsername;
    }

    public String getCreateDate() {
        return createDate;
    }

    public void setCreateDate(String createDate) {
        this.createDate = createDate;
    }

    public String getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(String modifyDate) {
        this.modifyDate = modifyDate;
    }

    public int page; // 当前页
    public int rows;// 每页显示的行数rows
    public int status; //状态，0显示全部，1显示有附件，2显示有回复

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }


    public String bookingCommentId;

    public String getBookingCommentId() {
        return bookingCommentId;
    }

    public void setBookingCommentId(String bookingCommentId) {
        this.bookingCommentId = bookingCommentId;
    }

    public String usersId;

    public String getUsersId() {
        return usersId;
    }

    public void setUsersId(String usersId) {
        this.usersId = usersId;
    }

    public Integer userId;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    /**
     * 添加评论
     */
    public JSONObject orderRate() {
        JSONObject jsonObject = new JSONObject();

        JSONObject json = GetHttpParam.getHttpParam(request);
        System.out.println("参数："+json.toString());
        if (json == null) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "缺少数据---orderRate");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }

        OrderRateDto orderRateDto =  JSONObject.parseObject(json.toString(),  OrderRateDto.class);



        if (Func.checkNull(orderRateDto.toString())) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "缺少数据---orderRate");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }
        if (orderRateDto.getScore() == null || orderRateDto.getScore() < 0 || orderRateDto.getScoreFw() == null || orderRateDto.getScoreFw() < 0 || orderRateDto.getScoreSs() == null || orderRateDto.getScoreSs() < 0 || orderRateDto.getScoreWs() == null || orderRateDto.getScoreWs() < 0 || orderRateDto.getScoreWz() == null || orderRateDto.getScoreWz() < 0 || orderRateDto.getBookingId() == null || orderRateDto.getHotelId() == null || orderRateDto.getHouseId() == null) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "缺少数据---orderRate");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }

        BookingComment bookingComment = BookingComment.builder().bookingId(orderRateDto.getBookingId())
                .hotelId(orderRateDto.getHotelId()).houseId(orderRateDto.getHouseId()).content(orderRateDto.getContent()).commentStatus(orderRateDto.getCommentStatus())
                .score(orderRateDto.getScore()).scoreWs(orderRateDto.getScoreWs()).scoreFw(orderRateDto.getScoreFw())
                .scoreSs(orderRateDto.getScoreSs()).scoreWz(orderRateDto.getScoreWz()).createId(orderRateDto.getCreateId())
                .createUsername(orderRateDto.getCreateUsername()).createDate(orderRateDto.getCreateDate())
                .modifyDate(orderRateDto.getModifyDate()).build();


        int a = bookingCommentService.orderRate(bookingComment,orderRateDto.getPictureList());


        if (a > 0) {

            jsonObject.put("code", 200);
            jsonObject.put("message", "添加成功");
            jsonObject.put("success", true);
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }
        jsonObject.put("message", "未知异常");
        jsonObject.put("success", false);
        jsonObject.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
        return null;
    }

    public JSONObject unevaluatedOrder(){
        JSONObject jsonObject = new JSONObject();
        if (page <= 0 || rows <= 0) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "未传入page,rows数据---unevaluatedOrder");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }
        if (Func.checkNull(hotelId)||Func.checkNull(userId+"")) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "未传入hotelId,usersId数据---unevaluatedOrder");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }

        UnevaluatedOrder vo=userService.collect(hotelId,userId);
        IPage<PersonageCommentVo> iPage = bookingCommentService.personageComment(0, userId+"", page, rows);
        vo.setPage(iPage);

        jsonObject.put("code", 200);
        jsonObject.put("message", "查看成功");
        jsonObject.put("success", true);
        jsonObject.put("data", vo);
        ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
        return null;
    }


    /**
     * 评价列表
     *
     * @return
     */
    public JSONObject evaluatePage() {
        JSONObject jsonObject = new JSONObject();
        if (Func.checkNull(hotelId)) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "未传入hotel_id数据---evaluatePage");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }

        if (Func.checkNull(status + "")) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "未传入status数据---evaluatePage");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }

        if (page <= 0 || rows <= 0) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "未传入page,rows数据---evaluatePage");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }
//        获取分页数据
        IPage<BookingCommentPageVo> iPage = bookingCommentService.evaluatePage(status, hotelId, page, rows);
//        计算评分
        EvaluatePageVo vo = bookingCommentService.evaluateScore(hotelId);

        jsonObject.put("code", 200);
        jsonObject.put("message", "成功");
        jsonObject.put("success", true);
        jsonObject.put("page", iPage);
        jsonObject.put("score", vo);

        ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());


        return null;

    }

    /**
     * 回复评论
     *
     * @return
     */
    public JSONObject replyComment() {
        JSONObject jsonObject = new JSONObject();
        if (Func.checkNull(content) || Func.checkNull(commentParentId)||Func.checkNull(commentId)) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "缺少数据---replyComment");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }
        String commentName=this.commentName;
        BookingComment byId = bookingCommentService.getBycommentId(commentId);
        if (byId!=null) {
            Integer createId1 = byId.getCreateId();
            Users users = userService.queryByUserId(createId1 + "");
            if (users!=null) {
                commentName=users.getUser_name();
            }
        }

        BookingComment bookingComment = BookingComment.builder().commentId(commentId).commentParentId(commentParentId).commentName(commentName).bookingId(bookingId)
                .hotelId(hotelId).houseId(houseId).content(content).commentStatus(commentStatus)
                .score(score).scoreWs(scoreWs).scoreFw(scoreFw)
                .scoreSs(scoreSs).scoreWz(scoreWz).createId(createId)
                .createUsername(createUsername).createDate(createDate)
                .modifyDate(modifyDate).build();


        int i = bookingCommentService.replyComment(bookingComment);
        if (i > 0) {
            jsonObject.put("code", 200);
            jsonObject.put("message", "回复成功");
            jsonObject.put("success", true);
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }
        jsonObject.put("message", "未知异常");
        jsonObject.put("code", 205);
        jsonObject.put("success", false);
        ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
        return null;
    }

    /**
     * 评论详情
     * @return
     */
    public JSONObject commentDetails() {
        JSONObject jsonObject = new JSONObject();
        if (Func.checkNull(bookingCommentId)) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "未传入bookingCommentId数据---commentDetails");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }
//        评论
        List<CommentVo> commentVoList = bookingCommentService.commentVoList(bookingCommentId);
        List<CommentListVo> commentListVos=null;
        if (commentVoList!=null&&commentVoList.size()>0) {
             commentListVos = TreeRecordsUtil.queryCommentTreeRecords(bookingCommentId, commentVoList);
        }

//        详情
        CommentDetailsVo commentDetailsVo =bookingCommentService.commentDetails(bookingCommentId,1);
        if (commentDetailsVo.getCommentTime()!=null) {
            commentDetailsVo.setCommentTime(DateUtil.parseDateToStr((Func.parseDate(commentDetailsVo.getCommentTime())), DateUtil.Time_Formatter_Day));
        }

        if (commentDetailsVo.getLiveTime()!=null) {
            commentDetailsVo.setLiveTime(DateUtil.parseDateToStr((Func.parseDate(commentDetailsVo.getLiveTime())), DateUtil.Time_Formatter_Day));
        }

        jsonObject.put("code", 200);
        jsonObject.put("message", "成功");
        jsonObject.put("success", true);
        jsonObject.put("comment",commentListVos);
        jsonObject.put("commentDetails",commentDetailsVo);
        ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
        return null;
    }


    /**
     * 个人评论分页数据
     * @return
     */
    public JSONObject personageComment(){
        JSONObject jsonObject = new JSONObject();
        if (Func.checkNull(usersId)) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "未传入usersIdId数据---personageComment");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }
        if (Func.checkNull(status + "")) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "未传入status数据---personageComment");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }

        if (page <= 0 || rows <= 0) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "未传入page,rows数据---personageComment");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }


        IPage<PersonageCommentVo> ipage=bookingCommentService.personageComment(status, usersId, page, rows);

        IPage<CommentDetailsVo> voIPage = new IPage<>();
        BeanUtils.copyProperties(ipage,voIPage);

        List<PersonageCommentVo> pageList = ipage.getPageList();

        PersonageCommentCountVo vo = bookingCommentService.personageCommentCount(usersId);
        if (pageList == null || pageList.size() <= 0 ||status==0) {
            jsonObject.put("code", 200);
            jsonObject.put("message", "成功");
            jsonObject.put("success", true);
            jsonObject.put("page", voIPage);
            jsonObject.put("data", vo);
            ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }
        ArrayList<CommentDetailsVo> commentDetailsVos = new ArrayList<>();
        for (PersonageCommentVo personageCommentVo : pageList) {
            String bookingCommentId1 = personageCommentVo.getBookingCommentId();
            CommentDetailsVo commentDetailsVo =bookingCommentService.commentDetails(bookingCommentId1,status);
            commentDetailsVo.setVo(personageCommentVo);
            commentDetailsVos.add(commentDetailsVo);
        }

        voIPage.setPageList(commentDetailsVos);

        jsonObject.put("code", 200);
        jsonObject.put("message", "成功");
        jsonObject.put("success", true);
        jsonObject.put("page", voIPage);
        jsonObject.put("data", vo);
        ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());

        return null;
    }


    /**
     * 个人评论详情
     * @return
     */
    public JSONObject personageDetails(){
        JSONObject jsonObject = new JSONObject();
        if (Func.checkNull(bookingCommentId)) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "未传入bookingCommentId数据---commentDetails");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }
        //        评论
        List<CommentVo> commentVoList = bookingCommentService.commentVoList(bookingCommentId);

        List<CommentListVo> commentListVos=null;
        if (commentVoList!=null&&commentVoList.size()>0) {
            commentListVos = TreeRecordsUtil.queryCommentTreeRecords(bookingCommentId, commentVoList);
        }
//        详情
        PersonageDetailsVo personageDetailsVo =bookingCommentService.personageDetails(bookingCommentId);
        if (personageDetailsVo.getCommentTime()!=null) {
            personageDetailsVo.setCommentTime(DateUtil.parseDateToStr((Func.parseDate(personageDetailsVo.getCommentTime())), DateUtil.Time_Formatter_Day));
        }

        if (personageDetailsVo.getLiveTime()!=null) {
            personageDetailsVo.setLiveTime(DateUtil.parseDateToStr((Func.parseDate(personageDetailsVo.getLiveTime())), DateUtil.Time_Formatter_Day));
        }

        if (personageDetailsVo.getCheckOutTime()!=null) {
            personageDetailsVo.setCheckOutTime(DateUtil.parseDateToStr((Func.parseDate(personageDetailsVo.getCheckOutTime())), DateUtil.Time_Formatter_Day));
        }

        jsonObject.put("code", 200);
        jsonObject.put("message", "成功");
        jsonObject.put("success", true);
        jsonObject.put("comment",commentListVos);
        jsonObject.put("commentDetails",personageDetailsVo);
        ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
        return null;
    }


}
