package cn.reghao.tnb.content.app.mobile.service;

import cn.reghao.jutil.jdk.db.PageList;
import cn.reghao.tnb.account.api.iface.AccountQuery;
import cn.reghao.tnb.content.api.dto.VideoUrls;
import cn.reghao.tnb.content.app.vod.model.vo.VideoDetail;
import cn.reghao.tnb.content.app.vod.model.query.VideoQuery;
import cn.reghao.tnb.content.app.vod.service.*;
import cn.reghao.tnb.content.app.mobile.model.VideoBean;
import cn.reghao.tnb.content.app.mobile.model.VideoBeanResult;
import cn.reghao.tnb.content.app.mobile.model.VideoDetails;
import cn.reghao.tnb.content.app.mobile.model.VideoDetailsResult;
import cn.reghao.tnb.user.api.dto.UserCard;
import cn.reghao.tnb.user.api.iface.UserService;
import cn.reghao.tnb.content.api.dto.VideoCard;
import cn.reghao.tnb.content.api.dto.VideoRegion;
import cn.reghao.tnb.content.app.vod.db.mapper.VideoPostMapper;
import cn.reghao.tnb.content.app.vod.db.mapper.VideoTagMapper;
import cn.reghao.tnb.content.app.vod.model.dto.PageParam;
import cn.reghao.tnb.content.app.mobile.model.play.HDVideoInfo;
import cn.reghao.tnb.content.app.mobile.model.play.UrlEntity;
import cn.reghao.tnb.content.app.mobile.model.search.*;
import cn.reghao.tnb.content.app.mobile.model.search.ListBean;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2023-09-02 01:13:44
 */
@Service
public class AndroidService {
    @DubboReference(check = false, retries = 0, timeout = 60_000)
    private AccountQuery accountQuery;
    @DubboReference(check = false)
    private UserService userService;

    private final VideoPlayService videoPlayService;
    private final SearchService searchService;
    private final VideoPostMapper videoPostMapper;
    private final VideoTagMapper videoTagMapper;
    private final CategoryService categoryService;
    private final ContentPermission contentPermission;

    public AndroidService(VideoPlayService videoPlayService, SearchService searchService,
                          VideoPostMapper videoPostMapper, VideoTagMapper videoTagMapper,
                          CategoryService categoryService, ContentPermission contentPermission) {
        this.videoPlayService = videoPlayService;
        this.searchService = searchService;
        this.videoPostMapper = videoPostMapper;
        this.videoTagMapper = videoTagMapper;
        this.categoryService = categoryService;
        this.contentPermission = contentPermission;
    }

    public HotSearchTag getSearchTag() {
        List<ListBean> list = searchService.getHotSearch().stream()
                .map(hotSearch -> new ListBean(hotSearch.getKeyword(), ""+hotSearch.getRank()))
                .collect(Collectors.toList());
        HotSearchTag hotSearchTag = new HotSearchTag();
        hotSearchTag.setList(list);
        return hotSearchTag;
    }

    public List<NavBean> searchAll(String keyword) {
        NavBean navBean = new NavBean("视频", 101);
        NavBean navBean1 = new NavBean("用户", 98);

        List<NavBean> navBeans = new ArrayList<>();
        navBeans.add(navBean);
        navBeans.add(navBean1);
        return navBeans;
    }
    
    public SearchUpperInfo searchUser(String keyword) {
        return new SearchUpperInfo();
    }

    public SearchVideoResult searchVideo(String keyword) {
        return new SearchVideoResult(Collections.emptyList());
    }

    public List<VideoRegion> getCategories() {
        return categoryService.getCategories(true);
    }

    public VideoBeanResult getCategoryVideo() {
        PageParam pageParam = new PageParam();
        pageParam.setPageNumber(1);
        pageParam.setCategoryId(18);
        pageParam.setPrevId("0");
        pageParam.setNextId("0");
        PageList<VideoCard> pageList = PageList.empty();

        List<VideoBean> list =  pageList.getList().stream().map(VideoBean::new).collect(Collectors.toList());
        return new VideoBeanResult(list);
    }

    public VideoBeanResult getRecommend() {
        PageParam pageParam = new PageParam();
        pageParam.setPageNumber(1);
        pageParam.setCategoryId(12);
        pageParam.setPrevId("0");
        pageParam.setNextId("0");
        PageList<VideoCard> pageList = PageList.empty();

        List<VideoBean> list =  pageList.getList().stream().map(VideoBean::new).collect(Collectors.toList());
        return new VideoBeanResult(list);
    }

    public VideoDetailsResult getVideoDetailsResult(String videoId) {
        List<Integer> scopes = contentPermission.getUserScopes();
        VideoQuery videoQuery = new VideoQuery.Builder().scope(scopes).videoId(videoId).build();
        VideoDetail videoDetail = videoPostMapper.findVideoPostDetail(videoQuery);
        if (videoDetail == null) {
            return null;
        }

        List<String> tags = videoTagMapper.findVideoTags(videoId);
        videoDetail.setTags(tags);
        String publishByStr = videoDetail.getUserId();
        long publishBy = accountQuery.getUserIdLong(publishByStr);
        UserCard userCard = userService.getUserAvatar(publishBy);

        VideoDetails videoDetails = new VideoDetails(videoDetail, userCard);
        return new VideoDetailsResult(videoDetails);
    }

    public HDVideoInfo getVideoUrl(String videoId) {
        VideoUrls videoUrls = videoPlayService.getVideoPlayData(videoId);
        String url = videoUrls.getUrls().get(0).getUrl();

        UrlEntity urlEntity = new UrlEntity();
        urlEntity.setUrl("http:" + url);
        List<UrlEntity> list = new ArrayList<>();
        list.add(urlEntity);

        HDVideoInfo hdVideoInfo = new HDVideoInfo();
        hdVideoInfo.setDurl(list);
        return hdVideoInfo;
    }
}
