package com.ycwl.basic.service.pc.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycwl.basic.mapper.SourceMapper;
import com.ycwl.basic.model.pc.source.entity.SourceEntity;
import com.ycwl.basic.model.pc.source.req.SourceReqQuery;
import com.ycwl.basic.model.pc.source.resp.SourceRespVO;
import com.ycwl.basic.repository.SourceRepository;
import com.ycwl.basic.service.pc.SourceService;
import com.ycwl.basic.task.VideoPieceGetter;
import com.ycwl.basic.utils.ApiResponse;
import com.ycwl.basic.utils.SnowFlakeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.List;

/**
 * @Author：longbinbin
 * @Date：2024/12/3 15:49
 */
@Service
public class SourceServiceImpl implements SourceService {
    @Autowired
    private SourceMapper sourceMapper;
    @Autowired
    private SourceRepository sourceRepository;

    @Override
    public ApiResponse<PageInfo<SourceRespVO>> pageQuery(SourceReqQuery sourceReqQuery) {
        PageHelper.startPage(sourceReqQuery.getPageNum(), sourceReqQuery.getPageSize());
        List<SourceRespVO> list = sourceMapper.list(sourceReqQuery);
        list.forEach(sourceRespVO -> {
            if (sourceRespVO.getVideoUrl() != null) {
                try {
                    URL url = new URL(sourceRespVO.getVideoUrl());
                    if (StringUtils.startsWith(url.getHost(), "100.64.")) {
                        // 内网地址，需要代理
                        sourceRespVO.setVideoUrl("https://zhentuai.com/proxy?url=" + sourceRespVO.getVideoUrl());
                    } else {
                        sourceRespVO.setVideoUrl(sourceRespVO.getVideoUrl());
                    }
                } catch (MalformedURLException e) {
                    sourceRespVO.setVideoUrl(sourceRespVO.getVideoUrl());
                }
            }
        });
        PageInfo<SourceRespVO> pageInfo = new PageInfo<>(list);
        return ApiResponse.success(pageInfo);
    }

    @Override
    public ApiResponse<List<SourceRespVO>> list(SourceReqQuery sourceReqQuery) {
        List<SourceRespVO> list = sourceMapper.list(sourceReqQuery);
        list.forEach(sourceRespVO -> {
            if (sourceRespVO.getVideoUrl() != null) {
                try {
                    URL url = new URL(sourceRespVO.getVideoUrl());
                    if (StringUtils.startsWith(url.getHost(), "100.64.")) {
                        // 内网地址，需要代理
                        sourceRespVO.setVideoUrl("https://zhentuai.com/proxy?url=" + sourceRespVO.getVideoUrl());
                    } else {
                        sourceRespVO.setVideoUrl(sourceRespVO.getVideoUrl());
                    }
                } catch (MalformedURLException e) {
                    sourceRespVO.setVideoUrl(sourceRespVO.getVideoUrl());
                }
            }
        });
        return ApiResponse.success(list);
    }

    @Override
    public ApiResponse<SourceRespVO> getById(Long id, Long userId) {
        SourceRespVO sourceRespVO = sourceMapper.userGetById(id, userId);
        if (sourceRespVO.getVideoUrl() != null) {
            try {
                URL url = new URL(sourceRespVO.getVideoUrl());
                if (StringUtils.startsWith(url.getHost(), "100.64.")) {
                    // 内网地址，需要代理
                    sourceRespVO.setVideoUrl("https://zhentuai.com/proxy?url=" + sourceRespVO.getVideoUrl());
                } else {
                    sourceRespVO.setVideoUrl(sourceRespVO.getVideoUrl());
                }
            } catch (MalformedURLException e) {
                sourceRespVO.setVideoUrl(sourceRespVO.getVideoUrl());
            }
        }
        return ApiResponse.success(sourceRespVO);
    }

    @Override
    public ApiResponse<Integer> add(SourceEntity source) {
        source.setId(SnowFlakeUtil.getLongId());
        int add = sourceMapper.add(source);
        if (add > 0) {
            return ApiResponse.success(add);
        }else {
            return ApiResponse.fail("添加失败");
        }
    }

    @Override
    public ApiResponse<Integer> deleteById(Long id) {
        int i = sourceMapper.deleteById(id);
        if (i > 0) {
            return ApiResponse.success(i);
        }else {
            return ApiResponse.fail("删除失败");
        }
    }

    @Override
    public ApiResponse<Integer> update(SourceEntity source) {
        int i = sourceMapper.update(source);
        if (i > 0) {
            return ApiResponse.success(i);
        }else {
            return ApiResponse.fail("修改失败");
        }
    }

    @Override
    public ApiResponse cutVideo(Long id) {
        SourceEntity source = sourceRepository.getSource(id);
        if (Integer.valueOf(2).equals(source.getType())) {
            // 下载切片
            VideoPieceGetter.Task task = new VideoPieceGetter.Task();
            task.faceSampleIds = Collections.singletonList(source.getFaceSampleId());
            task.force = true;
            VideoPieceGetter.addTask(task);
        }
        return ApiResponse.success("任务已下发");
    }
}
