package com.playlet.web.service.app.impl;

import cn.hutool.crypto.digest.MD5;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.playlet.system.domain.PlayletItem;
import com.playlet.system.domain.PlayletPublicAccount;
import com.playlet.system.domain.PlayletPublicDetail;
import com.playlet.system.service.IPlayletItemService;
import com.playlet.system.service.IPlayletPublicAccountService;
import com.playlet.system.service.IPlayletPublicDetailService;
import com.playlet.web.service.app.PlayletPublicDetailAppService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PlayletPublicDetailAppServiceImpl implements PlayletPublicDetailAppService {

    private final IPlayletPublicDetailService iPlayletPublicDetailService;

    private final IPlayletPublicAccountService iPlayletPublicAccountService;

    private final IPlayletItemService iPlayletItemService;

    @Override
    public PageInfo<PlayletPublicDetail> getList(PlayletPublicDetail playletPublicDetail, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PlayletPublicDetail> list =  iPlayletPublicDetailService.selectPlayletPublicDetailList(playletPublicDetail);
        list.forEach(model->{
            model.setPlayletPublicAccount(iPlayletPublicAccountService.selectPlayletPublicAccountById(model.getPublicId()));
        });
        return PageInfo.of(list);
    }

    @Override
    public List<PlayletPublicDetail> getTopList(PlayletPublicDetail playletPublicDetail) {
        List<PlayletPublicDetail> list =  iPlayletPublicDetailService.selectPlayletPublicDetailList(playletPublicDetail);
        list.forEach(model->{
            model.setPlayletPublicAccount(iPlayletPublicAccountService.selectPlayletPublicAccountById(model.getPublicId()));
        });
        return list;
    }

    @Override
    public PlayletPublicDetail getById(Long id) {
        PlayletPublicDetail model = iPlayletPublicDetailService.getById(id);
        model.setPlayletPublicAccount(iPlayletPublicAccountService.selectPlayletPublicAccountById(model.getPublicId()));
        // 调用接口查询二维码信息 -- 富文本格式
        if("01".equals(model.getType()) && model.getItemId() != null){
            PlayletItem item = iPlayletItemService.getById(model.getItemId());
            fillQrcode(item.getItemId(), model);
        }
        return model;
    }

    protected void fillQrcode(String itemNum, PlayletPublicDetail detail){
        if(detail.getItemOne() != null){
            detail.setItemOneUrl(getQrcode(itemNum, detail.getItemOne().toString()));
        }
        if(detail.getItemTwo() != null){
            detail.setItemTwoUrl(getQrcode(itemNum, detail.getItemTwo().toString()));
        }
        if(detail.getItemThree() != null){
            detail.setItemThreeUrl(getQrcode(itemNum, detail.getItemThree().toString()));
        }
        if(detail.getItemFour() != null){
            detail.setItemFourUrl(getQrcode(itemNum, detail.getItemFour().toString()));
        }
        if(detail.getItemFive() != null){
            detail.setItemFiveUrl(getQrcode(itemNum, detail.getItemFive().toString()));
        }

    }

    private String getQrcode(String itemId, String itemNum){
        String token = "zlwaiSrEM9KEbilu5f";
        String timestamp = String.valueOf((int)(System.currentTimeMillis()/1000));
        String clientId = "10005693";
        String channelId = "34662";
        String signKey = MD5.create().digestHex(clientId + token + timestamp);
        JSONObject body = new JSONObject();
        body.put("clientId", clientId);
        body.put("channelId", channelId);
        body.put("bookId", itemId);
        body.put("chapterId",itemNum);
        body.put("signKey", signKey);
        body.put("timestamp", timestamp);
        body.put("fromDrId", "69104793291");
        body.put("referralId", "69104793291");
        // 调用推广api返回链接
        String url = "https://video.wqxsw.com/api/v1/channelapi/getminiqrcode";
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Content-Type", "application/json");
        HttpResponse result = HttpUtil.createPost(url).addHeaders(headerMap).body(JSONObject.toJSONString(body)).execute();
//        String result = HttpUtil.post("https://video.wqxsw.com/api/v1/channelapi/getminiqrcode", body);
        log.info("请求入参:{}, 地址:{}, 返回:{}", body, url, result.body());
        JSONObject json = JSONObject.parseObject(result.body());
        if("1".equals(json.getString("code"))){
            return json.getString("data");
        }else {
            return null;
        }
    }

}
