package com.qmth.exam.reserve.test;

import com.qmth.boot.tools.signature.SignatureEntity;
import com.qmth.boot.tools.signature.SignatureType;
import com.qmth.exam.reserve.bean.login.LoginUser;
import com.qmth.exam.reserve.util.HttpClientBuilder;
import com.qmth.exam.reserve.util.JsonHelper;
import okhttp3.*;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class AuthTest {

    private final static String SERVER_URL = "http://192.168.10.41:8080";

    @Test
    public void demo() throws Exception {
        // LoginUser loginUser = this.loginTest("1500010120019", "123456");
        // this.apiTest(loginUser.getSessionId(), loginUser.getToken());
        this.apiTest("xxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    }

    private LoginUser loginTest(String account, String password) {
        String apiUrl = "/api/student/login";
        Map<String, Object> params = new HashMap<>();
        params.put("account", account);
        params.put("password", password);

        String jsonParams = JsonHelper.toJson(params);
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json;charset=UTF-8"), jsonParams);
        Request.Builder builder = new Request.Builder().post(requestBody).url(SERVER_URL + apiUrl);
        Request request = builder.build();

        try (Response response = HttpClientBuilder.getClient().newCall(request).execute();
             ResponseBody body = response.body();) {
            String bodyStr = body.string();
            System.out.printf("Response:%s %s \n\n", response.code(), bodyStr);

            if (response.isSuccessful()) {
                return JsonHelper.toObj(bodyStr, LoginUser.class);
            }
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }

        throw new RuntimeException("登录失败！");
    }

    private void apiTest(String sessionId, String token) {
        // String apiUrl = "/api/student/logout";
        String apiUrl = "/api/student/info";

        long timestamp = System.currentTimeMillis();
        String signature = SignatureEntity.build(SignatureType.TOKEN, "POST", apiUrl,
                timestamp, sessionId, token);

        Map<String, String> headers = new HashMap<>();
        headers.put("authorization", signature);
        headers.put("time", String.valueOf(timestamp));
        System.out.println(headers);

        RequestBody requestBody = FormBody.create(MediaType.parse("application/json;charset=UTF-8"), "{}");
        Request.Builder builder = new Request.Builder().post(requestBody).url(SERVER_URL + apiUrl);
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            builder.addHeader(entry.getKey(), entry.getValue());
        }
        Request request = builder.build();

        try (Response response = HttpClientBuilder.getClient().newCall(request).execute();
             ResponseBody body = response.body();) {
            String bodyStr = body.string();
            System.out.printf("Response:%s %s \n\n", response.code(), bodyStr);
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
    }

}
