package com.slodon.b2b2c.controller.video.front;

import com.slodon.b2b2c.core.constant.VideoConst;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.*;
import com.slodon.b2b2c.member.pojo.Member;
import com.slodon.b2b2c.model.video.AuthorFollowModel;
import com.slodon.b2b2c.model.video.AuthorModel;
import com.slodon.b2b2c.model.video.SvideoVideoModel;
import com.slodon.b2b2c.model.video.VideoSettingModel;
import com.slodon.b2b2c.video.example.AuthorExample;
import com.slodon.b2b2c.video.example.AuthorFollowExample;
import com.slodon.b2b2c.video.example.SvideoVideoExample;
import com.slodon.b2b2c.video.pojo.Author;
import com.slodon.b2b2c.video.pojo.AuthorFollow;
import com.slodon.b2b2c.video.pojo.SvideoVideo;
import com.slodon.b2b2c.video.pojo.VideoSetting;
import com.slodon.b2b2c.vo.video.AuthorSearchVO;
import com.slodon.b2b2c.vo.video.VideoSearchVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author slodon
 */
@Api(tags = "front-搜索页")
@RestController
@RequestMapping("v3/video/front/video/search")
public class FrontSearchController extends BaseController {

    @Resource
    private AuthorModel authorModel;
    @Resource
    private SvideoVideoModel svideoVideoModel;
    @Resource
    private AuthorFollowModel authorFollowModel;
    @Resource
    private VideoSettingModel videoSettingModel;

    @ApiOperation("相关用户搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型：type为anchor时查主播，默认不传", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "关键词", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("userList")
    public JsonResult<PageVO<AuthorSearchVO>> userList(HttpServletRequest request, String keyword, String type) {
        //type不为空说明是直播搜索
        if (!StringUtil.isEmpty(type) && "anchor".equals(type)) {
            VideoSetting setting = videoSettingModel.getVideoSettingBySettingName("live_switch");
            AssertUtil.notNull(setting, "查询的直播设置信息为空");
            if (setting.getValue().equals(VideoConst.VIDEO_SWITCH_0)) {
                List<AuthorSearchVO> vos = new ArrayList<>();
                AuthorSearchVO vo = new AuthorSearchVO();
                vos.add(vo);
                return SldResponse.success(new PageVO<>(vos, null));
            }
        }
        Member member = UserUtil.getUser(request, Member.class);
        Author authorDb = null;
        if (member.getMemberId() != null) {
            authorDb = authorModel.getAuthorByMemberId(member.getMemberId());
        }
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        AuthorExample example = new AuthorExample();
        example.setKeyword(keyword);
        if (!StringUtil.isEmpty(type) && "anchor".equals(type)) {
            example.setRoleType(VideoConst.ROLE_TYPE_2);
        }
        example.setOrderBy("create_time desc ");
        List<Author> list = authorModel.getAuthorList(example, pager);
        List<AuthorSearchVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            Author finalAuthorDb = authorDb;
            list.forEach(author -> {
                //设置默认头像
                if (StringUtil.isEmpty(author.getMemberAvatar())) {
                    author.setMemberAvatar(videoSettingModel.getVideoSettingBySettingName("author_default_profile").getValue());
                }
                //无昵称,用会员账号
                if (StringUtil.isEmpty(author.getMemberNickname())) {
                    author.setMemberNickname(author.getMemberName());
                }
                AuthorSearchVO vo = new AuthorSearchVO(author);
                if (finalAuthorDb != null) {
                    if (finalAuthorDb.getAuthorId().equals(author.getAuthorId())) {
                        vo.setIsSelf(true);
                    } else {
                        //查询是否关注
                        AuthorFollowExample followExample = new AuthorFollowExample();
                        followExample.setQueryAuthorId("notNull");
                        followExample.setFromAuthorId(finalAuthorDb.getAuthorId());
                        followExample.setToAuthorId(author.getAuthorId());
                        List<AuthorFollow> followList = authorFollowModel.getAuthorFollowList(followExample, null);
                        if (!CollectionUtils.isEmpty(followList)) {
                            vo.setIsFollow(true);
                        }
                    }
                }
                vos.add(vo);
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("相关视频搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键词", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("videoList")
    public JsonResult<PageVO<VideoSearchVO>> videoList(HttpServletRequest request, String keyword) {
        VideoSetting setting = videoSettingModel.getVideoSettingBySettingName("video_switch");

        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        SvideoVideoExample example = new SvideoVideoExample();
        if (setting.getValue().equals(VideoConst.VIDEO_SWITCH_0)) {
            //短视频关闭只查询图文
            example.setVideoType(VideoConst.SVIDEO_TYPE_2);
        }
        example.setKeyword(keyword);
        example.setState(VideoConst.STATE_2);
        example.setOrderBy("create_time desc ");
        List<SvideoVideo> list = svideoVideoModel.getSvideoVideoList(example, pager);
        List<VideoSearchVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(video -> {
                //查询短视频作者信息
                Author videoAuthor = authorModel.getAuthorByAuthorId(video.getAuthorId());
                AssertUtil.notNull(videoAuthor, "短视频作者不存在");
                //无昵称,用会员账号
                if (StringUtil.isEmpty(videoAuthor.getMemberNickname())) {
                    video.setMemberNickname(videoAuthor.getMemberName());
                } else {
                    video.setMemberNickname(videoAuthor.getMemberNickname());
                }
                video.setMemberName(videoAuthor.getMemberName());
                VideoSearchVO vo = new VideoSearchVO(video);
                //设置默认头像
                if (StringUtil.isEmpty(videoAuthor.getMemberAvatar())) {
                    vo.setMemberAvatar(FileUrlUtil.getFileUrl(videoSettingModel.getVideoSettingBySettingName("author_default_profile").getValue(), null));
                } else {
                    vo.setMemberAvatar(videoAuthor.getMemberAvatar().startsWith("http") ? videoAuthor.getMemberAvatar() : FileUrlUtil.getFileUrl(videoAuthor.getMemberAvatar(), null));
                }
                vos.add(vo);
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }
}
