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


import com.alibaba.fastjson.JSON;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.HttpClientUtil;
import com.slodon.b2b2c.live.pojo.LiveBroadcast;
import com.slodon.b2b2c.model.system.BillModel;
import com.slodon.b2b2c.model.video.AdminVideoSettingModel;
import com.slodon.b2b2c.video.pojo.VideoSetting;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
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.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

@Api(tags = "admin-直播管理")
@RestController
@Slf4j
@RequestMapping("v3/video/admin/video/setting")
public class AdminVideoSettingController extends BaseController {

    @Resource
    private AdminVideoSettingModel adminVideoSettingModel;
    @Resource
    private BillModel billModel;

    @ApiOperation("直播设置列表")
    @GetMapping("getLiveSettingList")
    public JsonResult<List<VideoSetting>> getLiveSettingList() {
        billModel.billJobSchedule();

//        List<VideoSetting> list = adminVideoSettingModel.getLiveSettingList();
//        AssertUtil.notNull(list, "请输入正确的参数");
        return SldResponse.success();
    }

    public static void main(String[] args) throws Exception {
        final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=";//获取access_token地址
        String access_token_resp = HttpClientUtil.httpGet(ACCESS_TOKEN_URL + "wx43aba958092afe0c" + "&secret=" + "647d35cc24fc7996003168d44ea91a43");
        String access_token = JSON.parseObject(access_token_resp).getString("access_token");
        AssertUtil.notEmpty(access_token, "微信认证失败");
        System.out.println("结果access_token: "+access_token);
        String url = "https://api.weixin.qq.com/wxaapi/broadcast/room/create?access_token="+access_token;
        LiveBroadcast t = new LiveBroadcast();
        t.setName("测试直播间");
        t.setCoverImg("Oftl3yAWV5lJIHHMpprnVi5Ad8nnQtGmkbYgspS1mJxMqPSzJlV3cZPOq-V6_yo8");
        t.setStartTime(System.currentTimeMillis()/1000+700);
        t.setEndTime(System.currentTimeMillis()/1000+5000);
        t.setAnchorName("主播昵称");
        t.setAnchorWechat("feiskyend");
        t.setShareImg("Oftl3yAWV5lJIHHMpprnVlJvIjvSoldaVmDE5-hzb46CdTAEO3GhyvoJZsGGkA6z");
        t.setFeedsImg("Oftl3yAWV5lJIHHMpprnVgmkSTWNVnNTCCM6FvfZQqG8nlAiiSSY4ssBzLFXro4E");
        t.setType(1);
        t.setCloseLike(0);
        t.setCloseGoods(0);
        t.setCloseComment(0);
        System.out.println("json: "+JSON.toJSONString(t));

        String reStr = HttpClientUtil.sendJsonPostNew(url,JSON.toJSONString(t));
        System.out.println("结果： "+reStr);
        /*System.out.println(System.currentTimeMillis()+1800000);
        System.out.println(System.currentTimeMillis()+3600000);*/
       /* //定义接收数据
        JSONObject result = new JSONObject();

        HttpPost httpPost = new HttpPost(url);
        CloseableHttpClient client = HttpClients.createDefault();
//请求参数转JOSN字符串
        StringEntity entity = new StringEntity(t.toString(), "UTF-8");
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        try {
            HttpResponse response = client.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == 200) {
                result = JSON.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
            }
        } catch (IOException e) {
            e.printStackTrace();
            result.put("error", "连接错误！");
        }
        System.out.println(result.toJSONString());*/
    }
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    private BufferedImage get(String jsonStr) throws Exception {
        final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=";//获取access_token地址
        String appId = stringRedisTemplate.opsForValue().get("login_wx_mini_appid");
        String secret = stringRedisTemplate.opsForValue().get("login_wx_mini_appsecret");

        //获取小程序 access_token
        String access_token_resp = HttpClientUtil.httpGet(ACCESS_TOKEN_URL + appId + "&secret=" + secret);
        String access_token = JSON.parseObject(access_token_resp).getString("access_token");
        AssertUtil.notEmpty(access_token, "微信认证失败");

        URL url = new URL("https://api.weixin.qq.com/wxaapi/broadcast/room/create?access_token=" + access_token);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");// 提交模式
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
        //构造json参数
        printWriter.write(jsonStr);
        printWriter.flush();

        return ImageIO.read(httpURLConnection.getInputStream());
    }
}
