package jj.tech.paolu.biz.webadmin.controller;

import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

import java.util.HashMap;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.JsonNode;
import com.microsoft.playwright.APIResponse;
import com.microsoft.playwright.FrameLocator;
import com.microsoft.playwright.Response;
import com.microsoft.playwright.options.RequestOptions;

import io.swagger.v3.oas.annotations.Operation;
import jj.tech.paolu.biz.webadmin.component.Hgt8Component;
import jj.tech.paolu.biz.webadmin.vo.YmCodeVo;
import jj.tech.paolu.utils.R;
import jj.tech.paolu.utils.YMHttp;

@RestController
@RequestMapping(value = "/op/hgt8",name = "导航到hgt8")
public class Hgt8Controller {
	@Autowired
	public Hgt8Component hgt8Component;
	
	
	@Operation(summary = "test", description = "test")
	@PostMapping("/test")
	public Object login(@Validated @RequestBody YmCodeVo prame) {
//		hgt8Component.page.navigate("https://sol8.idc9998.com/search.aspx");
//		hgt8Component.page.locator("xpath=//iframe[@id=lineCode]").fill("bk7897");
//		hgt8Component.page.getByTestId("lineCode").fill("bk7897");
		
		hgt8Component.page.navigate("https://sol8.idc9998.com");
		hgt8Component.page.frameLocator("#ifr_main")
		.getByTestId("lineCode")
			.fill("339hp");
		
		hgt8Component.page.frameLocator("#ifr_main")
		.locator("xpath=//input[@class='btn']")
		.click();
		
		Response response = hgt8Component.page.waitForResponse("**/captcha_image/2*", () -> {
			  
		});
		byte[] image_bytes = response.body();
		
		JsonNode root = 
		YMHttp.customApi(Base64.encodeBase64String(image_bytes));
		JsonNode code_num = root.get("data").get("data");
		
		//System.err.println(code_num.asText());
		FrameLocator ifr_main = hgt8Component.page.frameLocator("#ifr_main");
		
		
		ifr_main.getByTestId("userName")
		.fill("bk7897");
		
		ifr_main.getByTestId("password")
		.fill("asas12123");
		
		ifr_main.getByTestId("code")
		.fill(code_num.asText());
		
		ifr_main.getByTestId("btnSubmit")
		.click();
		
		
		
//		assertThat(hgt8Component.page.frameLocator("#ifr_main")
//				.locator("xpath=//input[@class='protocol-btn-y']")).is;
		
		//同意
		hgt8Component.page.frameLocator("#ifr_main")
		.locator("xpath=//input[@class='protocol-btn-y']")
		.click();
		
//		ifr_main.getByTestId("tc_Close")
//		.click();
		ifr_main.getByText("历史开奖")
		.click();
//		Assert.assertEquals(apiPostResponse.status(), 201);
		
//		{"gameID":"G_24","currtentPage":1,"asDate":"2024-09-23","issuet":""}
		HashMap<String, String> pram = new HashMap<String, String>();
		pram.put("gameID", "G_24");
		pram.put("currtentPage","1");
		pram.put("asDate","2024-09-23");
		pram.put("issuet", "");
		
		RequestOptions  requestOptions  =
		RequestOptions.create().setHeader("Content-Type", "application/json")
        .setData(pram);
		
		APIResponse addNewPet =
		hgt8Component.aPIRequestContext.post("https://sol8.idc9998.com/wb2/jg0001/s001s", requestOptions);
		
		System.err.println(addNewPet.text());
		
		
		
		//ifr_main.getByTestId("captcha");
		
		return R.SUCCESS("ok");
	}
}
