package com.apobates.forum.thrones.admin.controller;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.apobates.forum.member.entity.MemberGroupEnum;
import com.apobates.forum.member.entity.MemberRoleEnum;
import com.apobates.forum.member.entity.MemberStatusEnum;
import com.apobates.forum.member.service.MemberOnlineService;
import com.apobates.forum.member.service.MemberService;
import com.apobates.forum.thrones.admin.controller.helper.AdminUtils;
import com.apobates.forum.thrones.admin.vo.CommonMember;
import com.apobates.forum.utils.lang.CommonBean;
import com.apobates.forum.utils.DateTimeUtils;
import com.google.gson.Gson;

/**
 * 会员统计控制器
 *
 * @author xiaofanku
 * @since 20190829
 */
@Controller
@RequestMapping(value = "/member/stats")
public class AdminMemberStatsController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberOnlineService memberOnlineService;
    private final static Logger logger = LoggerFactory.getLogger(AdminMemberStatsController.class);
    
    @GetMapping(path = "/")
    public String homePage(
            HttpServletRequest request,
            Model model) {
        //近七天会员的活跃情况
        LocalDateTime finish = LocalDateTime.now();
        LocalDateTime start = DateTimeUtils.beforeDayForDate(finish, 7);
        //没有数据填上0
        TreeMap<String, Long> data = memberService.groupMemberForBirthDate(start, finish);
        if (null == data || data.size() < 7) {
            data = AdminUtils.fillEmptyResult(start, finish, data);
        }
        logger.info("[MS] DB Query Size: " + data.size());
        logger.info("[MS] Fill Result Size: " + data.size());
        List<CommonBean> rs = data.entrySet().stream().map(entry->new CommonBean(entry.getValue(), entry.getKey())).collect(Collectors.toList());
        model.addAttribute("rs", new Gson().toJson(rs));
        return "admin/member/stats";
    }
    
    //最近注册的会员
    @GetMapping(path = "/recent/json", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<CommonMember> recentRegisteMember(HttpServletRequest request, Model model) {
        return memberService.getRecent(10).map(CommonMember::new).collect(Collectors.toList());
    }
    
    //当前在线的会员
    @GetMapping(path = "/online/json", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<CommonMember> getOnlineMember(HttpServletRequest request, Model model) {
        return memberOnlineService.getForNow().map(mo -> new CommonMember(mo.getMid(), mo.getMemberNickname(), mo.getActiveDateTime())).collect(Collectors.toList());
    }
    
    //用户组的占比
    @GetMapping(path = "/donut/group", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Long> groupMemberForGroup(HttpServletRequest request, Model model) {
        Map<MemberGroupEnum, Long> rs = memberService.groupMemberForGroup();
        return rs.entrySet().stream().collect(Collectors.toMap(entry->entry.getKey().getTitle(), entry->entry.getValue()));
    }
    
    //用户角色的占比
    @GetMapping(path = "/donut/role", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Long> groupMemberForRole(HttpServletRequest request, Model model) {
        Map<MemberRoleEnum, Long> rs = memberService.groupMemberForRole();
        return rs.entrySet().stream().collect(Collectors.toMap(entry->entry.getKey().getTitle(), entry->entry.getValue()));
    }
    
    //用户状态的占比
    @GetMapping(path = "/donut/status", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Long> groupMemberForStatus(HttpServletRequest request, Model model) {
        Map<MemberStatusEnum, Long> rs = memberService.groupMemberForStatus();
        return rs.entrySet().stream().collect(Collectors.toMap(entry->entry.getKey().getTitle(), entry->entry.getValue()));
    }
}