package com.yutou.biliapi;

import com.alibaba.fastjson2.JSONObject;
import com.yutou.biliapi.bean.live.SpiBean;
import com.yutou.biliapi.bean.login.LoginCookieDatabaseBean;
import com.yutou.biliapi.bean.login.LoginUserDatabaseBean;
import com.yutou.biliapi.bean.user.UserInfoBean;
import com.yutou.biliapi.net.BiliLoginNetApiManager;
import com.yutou.biliapi.databases.BiliBiliLoginDatabase;
import com.yutou.biliapi.net.BiliUserNetApiManager;
import com.yutou.common.okhttp.HttpBody;
import com.yutou.common.okhttp.HttpCallback;
import com.yutou.common.utils.Log;
import jakarta.xml.bind.DatatypeConverter;
import okhttp3.Headers;
import retrofit2.Response;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class Main {
    public static void main(String[] args) {
      //  HttpLoggingInterceptor.setLog(false);
      //  HttpLoggingInterceptor.setLog(true);
        login();
    }


    public static void testSocket(SpiBean spi) {
        try {
            JSONObject json = new JSONObject();
            // json.put("roomid", "32805602");
            json.put("roomid", "855204");
            json.put("protover", "3");
            json.put("platform", "web");
            json.put("type", 2);
            json.put("buvid", spi.getB_3());
            json.put("key", "aaaabbb");
            Log.i(json);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            // outputStream.write(toLH(json.toString().length() + 16));
            outputStream.write(new byte[]{0, 0, 1, 68, 0, 16, 0, 1, 0, 0, 0, 7, 0, 0, 0, 1});
            outputStream.write(json.toJSONString().getBytes(StandardCharsets.UTF_8));
            outputStream.flush();
            Log.i("\n\n\n");
            String str = DatatypeConverter.printHexBinary(outputStream.toByteArray());
            for (int i = 0; i < str.length(); i = i + 4) {
                if (i % 32 == 0 && i != 0) {
                    Log.i();
                }
                if (str.length() - i > 4) {
                    System.out.print(str.substring(i, i + 4) + "  ");
                } else {
                    Log.i(str.substring(i));
                }
            }
            Log.i("\n\n\n");

        } catch (Exception e) {
            Log.e(e);
        }
    }

    public static void getPlayUrl() {
//        BiliLiveNetApiManager
//                .getInstance()
//                .getApi(new IHttpApiCheckCallback<LiveApi>() {
//                    @Override
//                    public void onSuccess(LiveApi api) {
//                        String roomId = "32805602";
//                        String mid = "68057278";
//
//                        //  roomId="42062";
//
//                        api.getLiveRoomPlayInfo(
//                                roomId,
//                                LiveProtocol.getAll(),
//                                LiveVideoFormat.getAll(),
//                                LiveVideoCodec.getAll(),
//                                LiveVideoDefinition.ORIGINAL.getValue()
//                        ).enqueue(new HttpCallback<>() {
//                            @Override
//                            public void onResponse(Headers headers, int code, String status, LiveRoomPlayInfo response, String rawResponse) {
//                                LiveRoomPlayInfo.Codec codec = response.getPlayurlInfo().getPlayurl().getStream().get(0).getFormat().get(0).getCodec().get(0);
//
//                                LiveRoomConfig config = new LiveRoomConfig();
//                                config.setUid("0");
//                                config.setRoomId(roomId);
//                                config.setMid(mid);
//                                config.setLogin(false);
//                                WebSocketManager.getInstance().addRoom(config);
//
//                        /*String url = codec.getUrlInfo().get(0).getHost() + codec.getBaseUrl() + codec.getUrlInfo().get(0).getExtra();
//                        Log.i("下载url = " + url);
//                        api.downloadLive(url).enqueue(new FileCallback<>(response) {
//                            @Override
//                            public void onStart(LiveRoomPlayInfo bean) {
//                                Log.i("开始下载");
//                            }
//
//                            @Override
//                            public boolean onDownload(Headers headers, LiveRoomPlayInfo bean, long len, long total) {
//                                Log.i("下载中:"+len+"|"+total);
//                                return true;
//                            }
//
//                            @Override
//                            public void onFinish(LiveRoomPlayInfo bean) {
//                                Log.i("下载结束");
//                            }
//
//                            @Override
//                            public void onFailure(LiveRoomPlayInfo bean, Throwable throwable) {
//                                Log.i("下载失败");
//                            }
//                        });*/
//
//                            }
//
//                            @Override
//                            public void onFailure(Throwable throwable) {
//
//                            }
//                        });
//                    }
//
//                    @Override
//                    public void onError(int code, String error) {
//
//                    }
//                });
    }

    public static void login() {
        BiliLoginNetApiManager.getInstance().login(new HttpCallback<LoginCookieDatabaseBean>() {
            @Override
            public void onResponse(Headers headers, int code, String status, LoginCookieDatabaseBean response, String rawResponse) {
                Log.i("二维码地址:  "+rawResponse);
                if (code == BiliLoginNetApiManager.LOGIN_SUCCESS) {
                    Response<HttpBody<UserInfoBean>> execute = null;
                    try {
                        execute = BiliUserNetApiManager.getInstance().getUserApi(response).getUserInfo().execute();
                        if (execute.isSuccessful()) {
                            if (execute.body() != null) {
                                UserInfoBean data = execute.body().getData();
                                LoginUserDatabaseBean userBean=new LoginUserDatabaseBean(data);
                                BiliBiliLoginDatabase.getInstance().initData(response,userBean).close();
                            }
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }

                }
            }

            @Override
            public void onFailure(Throwable throwable) {

            }
        });
    }
}