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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.playlet.common.json.JSONObject;
import com.playlet.common.utils.security.Md5Utils;
import com.playlet.system.domain.PlayletItem;
import com.playlet.system.domain.PlayletItemType;
import com.playlet.system.domain.PlayletUserItem;
import com.playlet.system.service.IPlayletItemService;
import com.playlet.system.service.IPlayletItemTypeService;
import com.playlet.system.service.IPlayletUserItemService;
import com.playlet.web.service.app.PlayletUserItemAppService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PlayletUserItemAppServiceImpl implements PlayletUserItemAppService {

    private final IPlayletUserItemService iPlayletUserItemService;

    private final IPlayletItemService iPlayletItemService;

    private final IPlayletItemTypeService iPlayletItemTypeService;

    @Override
    public String addUserItem(PlayletUserItem playletUserItem) {
        PlayletUserItem model = iPlayletUserItemService.lambdaQuery()
                .eq(PlayletUserItem::getItemId, playletUserItem.getItemId())
                .eq(PlayletUserItem::getUserId, playletUserItem.getUserId())
                .one();
        if(model == null){
            playletUserItem.setCreateTime(new Date());
            iPlayletUserItemService.save(playletUserItem);
            model = playletUserItem;
        }
//        JSONObject body = new JSONObject();
        return this.getPlayletUrl();
    }

    @Override
    public PageInfo<PlayletUserItem> getUserItemPage(PlayletUserItem playletUserItem, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PlayletUserItem> list = iPlayletUserItemService.selectPlayletUserItemList(playletUserItem);
        list.forEach(model->{
            PlayletItem item = iPlayletItemService.getById(model.getItemId());
            if(ObjectUtil.isNotNull(item.getItemType())){
                PlayletItemType itemType = iPlayletItemTypeService.selectPlayletItemTypeById(Long.valueOf(item.getItemType()));
                item.setItemTypeName(itemType.getName());
            }
            model.setPlayletItem(item);
        });
        return PageInfo.of(list);
    }

    private String getPlayletUrl(){
        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","41000103084");
        body.put("chapterIdx","1");
        body.put("name","zhongcaodaren");
        body.put("mediaSource","ks");
        body.put("signKey", signKey);
        body.put("timestamp", timestamp);
        body.put("fromDrId", "69104793291");
        // 调用推广api返回链接
        String url = "https://video.wqxsw.com/api/channelapi/referralsave";
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Content-Type", "application/json");
        HttpResponse result = HttpUtil.createPost(url).addHeaders(headerMap).body(com.alibaba.fastjson.JSONObject.toJSONString(body)).execute();
//        String result = HttpUtil.post("https://video.wqxsw.com/api/v1/channelapi/getminiqrcode", body);
        log.info("请求入参:{}, 地址:{}, 返回:{}", body, url, result.body());
        com.alibaba.fastjson.JSONObject json = com.alibaba.fastjson.JSONObject.parseObject(result.body());
        if("1".equals(json.getString("code"))){
            return json.getJSONObject("data").getString("monitor");
        }else {
            return null;
        }
    }
}
