package com.apobates.forum.thrones.controller;

import com.apobates.forum.core.MoodCollectResult;
import com.apobates.forum.core.entity.proxy.TopicReplica;
import com.apobates.forum.core.security.strategy.Strategy;
import com.apobates.forum.core.security.strategy.StrategyEntityParam;
import com.apobates.forum.core.service.PostsMoodRecordsService;
import com.apobates.forum.event.elderly.ActionEventCulpritor;
import com.apobates.forum.event.elderly.ForumActionEnum;
import com.apobates.forum.member.storage.core.MemberSessionBean;
import com.apobates.forum.thrones.controller.helper.OnlineDescriptor;
import com.apobates.forum.utils.Commons;
import com.apobates.forum.utils.TipMessage;
import com.google.gson.Gson;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 回复喜好控制器
 *
 * @author xiaofanku
 * @since 20200527
 */
@Controller
@RequestMapping(value = "/posts/mood")
public class PostsMoodController {
    @Autowired
    private PostsMoodRecordsService postsMoodRecordsService;
    @Value("${site.domain}")
    private String siteDomain;
    
    @ModelAttribute("topicArg")
    public TopicReplica getTopic(HttpServletRequest request) {
        return TopicReplica.of(request.getHeader("referer"), siteDomain).orElse(null);
    }
    
    // 回复点赞
    @PostMapping(path = "/like", produces = "application/json;charset=UTF-8")
    @ResponseBody
    @OnlineDescriptor(action=ForumActionEnum.POSTS_LIKED)
    @Strategy(action=ForumActionEnum.POSTS_LIKED, param=StrategyEntityParam.QUERY_STR, paramId="id")
    public TipMessage likePostsAction(
            @RequestParam("id") long postsId,
            @RequestParam("mood") int moodStatus,
            @RequestParam(value = "token", required = false, defaultValue = "0") String token,
            @ModelAttribute("topicArg") TopicReplica tpObj,
            MemberSessionBean mbean,
            HttpServletRequest request,
            Model model) {
        //----------------------------------从Http Referer中获取需要的参数
        if (null == tpObj || tpObj.getId() < 1) {
            return TipMessage.ofError("操作参数解析失败");
        }
        //----------------------------------
        ActionEventCulpritor aec = BoardController.getInstance(mbean.getMid(), mbean.getNickname(), request, token);
        //----------------------------------不限制角色
        //  移至StrategyInterceptorAdapter
        //----------------------------------
        boolean liked = (moodStatus == 1);
        return TipMessage.Builder.of(()->postsMoodRecordsService.toggleMoodRecord(postsId, tpObj.getId(), liked, aec)>0).success("回复点赞成功").error("点赞失败");
    }
    
    //查看点赞的计数
    @GetMapping(path = "/list/jsonp", produces = "application/javascript;charset=UTF-8")
    @ResponseBody
    public String getAllBoardForJsonp(
            @RequestParam("ids") String postsIdString,
            @RequestParam("callback") String callBackFun,
            @ModelAttribute("topicArg") TopicReplica tpObj,
            HttpServletRequest request,
            Model model) {
        //-------------------------------------------------从Http Referer中获取需要的参数
        if (null == tpObj || tpObj.getId() < 1) {
            return callBackFun + "({});";
        }
        //-------------------------------------------------
        Set<Long> postsIdSet = Commons.toLongSet(postsIdString);
        if (null == postsIdSet || postsIdSet.isEmpty()) {
            return callBackFun + "({});";
        }
        List<MoodCollectResult> data = postsMoodRecordsService.getByPosts(tpObj.getId(), postsIdSet);
        if (data.isEmpty()) {
            return callBackFun + "({});";
        }
        Map<String, Object> result = Map.ofEntries(Map.entry("topic", tpObj.getId()), Map.entry("result", data));
        return callBackFun + "(" + new Gson().toJson(result) + ");";
    }
}