package com.yaoyuan.jiscuss.controller;

import com.yaoyuan.jiscuss.entity.Discussion;
import com.yaoyuan.jiscuss.entity.DiscussionTag;
import com.yaoyuan.jiscuss.entity.Post;
import com.yaoyuan.jiscuss.entity.Tag;
import com.yaoyuan.jiscuss.entity.User;
import com.yaoyuan.jiscuss.entity.UserInfo;
import com.yaoyuan.jiscuss.entity.custom.DiscussionCustom;
import com.yaoyuan.jiscuss.service.IDiscussionsService;
import com.yaoyuan.jiscuss.service.IDiscussionsTagsService;
import com.yaoyuan.jiscuss.service.IPostsService;
import com.yaoyuan.jiscuss.service.ITagsService;
import com.yaoyuan.jiscuss.service.IUsersService;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author yaoyuan2.chu
 * 主题帖子评论控制器
 */
@Controller
public class UserPostController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(UserPostController.class);

    @Autowired
    private IDiscussionsService discussionsService;

    @Autowired
    private ITagsService tagsService;

    @Autowired
    private IDiscussionsTagsService iDiscussionsTagsService;

    @Autowired
    private IPostsService postsService;

    @Autowired
    private IUsersService usersService;


    /**
     * 首页查看主题列表（各种条件筛选，最热最新标签等）
     */


    /**
     * 查看主题详情
     * @param request
     * @param map
     * @param id
     * @return
     */
    @RequestMapping("/getdiscussionsbyid")
    public String getDiscussionsById(HttpServletRequest request, ModelMap map, @RequestParam("id") Integer id) {
        logger.info(">>> getDiscussionsById{}", id);

        Discussion discussion = discussionsService.findOne(id);
        // 获取此主题下的评论
        List<Post> posts = postsService.findOneBy(id);

        DiscussionCustom newdd = new DiscussionCustom();
        BeanUtils.copyProperties(discussion, newdd);
        if (null != newdd.getStartUserId()) {
            User startUser = usersService.findOne(newdd.getStartUserId());
            newdd.setAvatar(startUser.getAvatar());
            newdd.setRealname(startUser.getRealname());
            newdd.setUsername(startUser.getUsername());
        }
        if (null != newdd.getLastUserId()) {
            User lastUser = usersService.findOne(newdd.getLastUserId());
            newdd.setAvatarLast(lastUser.getAvatar());
            newdd.setRealnameLast(lastUser.getRealname());
            newdd.setUsernameLast(lastUser.getUsername());
        }

        List<Tag> tags = tagsService.findByDId(id);
        List postsObj = postsService.findPostCustomById(id);
        map.put("tags", tags);
        map.put("discussions", newdd);
        map.put("posts", postsObj);
        UserInfo user = getUserInfo(request);
        if (user != null) {
            map.put("username", user.getUsername());
        }
        return "discussions";
    }

    /**
     * 新建主题
     * @param discussion
     * @return
     */
    @PostMapping(value = "/newDiscussions")
    @ResponseBody
    public String newDiscussions(@RequestBody DiscussionCustom discussion) {
        logger.info(">>> newPost" + discussion);

        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        UserInfo user = getUserInfo(request);
        if (user != null) {
            discussion.setStartUserId(user.getId());
            discussion.setLastUserId(user.getId());
            discussion.setCreateId(user.getId());
        }

        discussion.setCreateTime(new Date());
        discussion.setStartTime(new Date());
        discussion.setLastTime(new Date());

        Discussion discussionOne = new Discussion();
        BeanUtils.copyProperties(discussion, discussionOne);
        Discussion saveDiscussion = discussionsService.insert(discussionOne);

        if (null != discussion.getTag()) {
            //执行组装标签
            String[] strArray = discussion.getTag().split(",");
            for (String str : strArray) {
                DiscussionTag dtag = new DiscussionTag();
                dtag.setDiscussionId(saveDiscussion.getId());
                dtag.setTagId(Integer.parseInt(str));
                iDiscussionsTagsService.insert(dtag);
            }
        }

        JSONObject resultobj = new JSONObject();

        logger.info(">>>{}", saveDiscussion);
        resultobj.put("msg", "添加主题成功");
        resultobj.put("flag", true);
        return resultobj.toString(); //

    }


    /**
     * 编辑主题
     * @param post
     * @return
     */

    /**
     * 删除主题
     * @param post
     * @return
     */

    /**
     * 新建评论
     * @param post
     * @return
     */
    @PostMapping(value = "/newPost")
    @ResponseBody
    public String newPosts(@RequestBody Post post) {
        logger.info(">>> newpost" + post);

        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();

        UserInfo user = getUserInfo(request);
        if (user != null) {
            post.setCreateId(user.getId());
        }
        post.setCreateTime(new Date());
        if (null != post.getParentId()) {
            Post temp = postsService.findOneByid(post.getParentId());
            post.setUserId(temp.getCreateId());
        }

        Post savePost = postsService.insert(post);
        JSONObject resultobj = new JSONObject();

        logger.info(">>>{}", savePost);
        resultobj.put("msg", "添加评论成功");
        resultobj.put("flag", true);
        return resultobj.toString(); //
    }

    /**
     * 删除评论
     * @param tag
     * @return
     */

    /**
     * 新建标签
     * @param tag
     * @return
     */
    @PostMapping(value = "/newtags")
    @ResponseBody
    public String newTags(@RequestBody Tag tag) {
        logger.info(">>> newTags" + tag);
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();

        UserInfo user = getUserInfo(request);
        if (user != null) {
            tag.setCreateId(user.getId());
        }
        tag.setCreateTime(new Date());

        Tag saveTag = tagsService.insert(tag);
        JSONObject resultobj = new JSONObject();

        logger.info(">>>{}", saveTag);
        resultobj.put("msg", "添加标签成功");
        resultobj.put("flag", true);
        return resultobj.toString();
    }

    /**
     * 排行榜等
     */

    /**
     * 新建主题页
     *
     * @param request
     * @param map
     * @return
     */
    @RequestMapping({"/newDiscussionsPage"})
    public String newdiccuss(HttpServletRequest request, ModelMap map) {
        //获取所有标签（以后尝试去缓存中取）
        List<Tag> allTags = tagsService.getAllListDiscussions();
        map.put("allTags", allTags);

        UserInfo user = getUserInfo(request);
        if (user != null) {
            map.put("username", user.getUsername());
            map.put("data", "Jiscuss 用户:" + user.getUsername());
        }
        return "newdiccuss";
    }

    /**
     * 新建标签页
     *
     * @param request
     * @param map
     * @return
     */
    @RequestMapping({"/newTagPage"})
    public String newtag(HttpServletRequest request, ModelMap map) {

        //获取所有标签（以后尝试去缓存中取）
        List<Tag> allTags = tagsService.getAllList();
        map.put("allTags", allTags);

        UserInfo user = getUserInfo(request);
        if (user != null) {
            map.put("username", user.getUsername());
            map.put("data", "Jiscuss 用户:" + user.getUsername());
        }
        return "newtag";
    }

}
