package cn.skcks.docking.gb28181.wvp.test;

import cn.hutool.core.io.IoUtil;
import cn.hutool.crypto.digest.MD5;
import cn.skcks.docking.gb28181.common.json.JsonResponse;
import cn.skcks.docking.gb28181.wvp.WvpProxyTestApplication;
import cn.skcks.docking.gb28181.wvp.config.WvpProxyConfig;
import cn.skcks.docking.gb28181.wvp.dto.device.GetDeviceChannelsReq;
import cn.skcks.docking.gb28181.wvp.dto.device.GetDeviceChannelsResp;
import cn.skcks.docking.gb28181.wvp.dto.device.GetDeviceTreeReq;
import cn.skcks.docking.gb28181.wvp.dto.device.GetDeviceTreeResp;
import cn.skcks.docking.gb28181.wvp.dto.login.WvpLoginReq;
import cn.skcks.docking.gb28181.wvp.dto.login.WvpLoginResp;
import cn.skcks.docking.gb28181.wvp.proxy.WvpProxyClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.io.CloseMode;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;

@Slf4j
@SpringBootTest(classes = WvpProxyTestApplication.class)
@ExtendWith(SpringExtension.class)
public class WvpProxyTest {
    @Autowired
    private WvpProxyClient wvpProxyClient;

    @Autowired
    private WvpProxyConfig config;

    @Test
    void test(){
        String passwdMd5 = MD5.create().digestHex(config.getPasswd());
        WvpLoginReq loginReq = WvpLoginReq.builder()
                .username(config.getUser())
                .password(passwdMd5)
                .build();
        log.info("{}", loginReq);
        JsonResponse<WvpLoginResp> loginResp = wvpProxyClient.login(loginReq);
        log.info("{}", loginResp);

        String token = loginResp.getData().getAccessToken();
        String deviceId = "44050100002000000001";
        GetDeviceTreeReq getDeviceTreeReq = GetDeviceTreeReq.builder().build();
        JsonResponse<GetDeviceTreeResp> deviceTreeResp = wvpProxyClient.getDeviceTree(token, deviceId, getDeviceTreeReq);
        log.info("{}", deviceTreeResp);

        GetDeviceChannelsReq getDeviceChannelsReq = GetDeviceChannelsReq.builder().build();
        JsonResponse<GetDeviceChannelsResp> getDeviceChannelsResp = wvpProxyClient.getDeviceChannels(token, deviceId, getDeviceChannelsReq);
        log.info("{}", getDeviceChannelsResp);

        getDeviceChannelsResp.getData().getList().forEach(item -> {
            log.info("{}", item);
        });
    }

    @Test
    void downloadTest() throws IOException {
        final CloseableHttpClient client = HttpClients.custom()
                .build();

        String tmpDir = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath();
        File file = new File(Paths.get(tmpDir,"test.mp4").toUri());
        log.info("临时文件 路径 => {}",file.getAbsolutePath());
        FileOutputStream outputStream = new FileOutputStream(file);

        String url = "http://192.168.1.241:18979/download/recordTemp/0490d767d94ce20aedce57c862b6bfe9/rtp/59777645.mp4";
        HttpGet httpGet = new HttpGet(url);
        client.execute(httpGet, response -> {
            InputStream stream = response.getEntity().getContent();
            IoUtil.copy(stream,outputStream);
            return stream;
        });
        client.close(CloseMode.GRACEFUL);
    }
}
