package com.red.circle.auth.common;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

public class LoginAccessValidationServiceTest {

  @Test
  public void normalizeIpSbMainlandChina() {
    JSONObject data = LoginAccessValidationService.normalizeIpData(
        "ip.sb",
        JSON.parseObject("""
            {"country":"China","country_code":"CN","timezone":"Asia/Shanghai"}
            """)
    );

    assertEquals("CN", data.getString("country_id"));
    assertEquals("China", data.getString("country"));
    assertTrue(LoginAccessValidationService.isMainlandChina(data));
  }

  @Test
  public void normalizeFreeIpApiMainlandChina() {
    JSONObject data = LoginAccessValidationService.normalizeIpData(
        "freeipapi",
        JSON.parseObject("""
            {"countryName":"China","countryCode":"CN","regionName":"Shandong"}
            """)
    );

    assertEquals("CN", data.getString("country_id"));
    assertEquals("Shandong", data.getString("region"));
    assertTrue(LoginAccessValidationService.isMainlandChina(data));
  }

  @Test
  public void normalizeIpWhoisUnitedStates() {
    JSONObject data = LoginAccessValidationService.normalizeIpData(
        "ipwhois",
        JSON.parseObject("""
            {"success":true,"country":"United States","country_code":"US","region":"California"}
            """)
    );

    assertEquals("US", data.getString("country_id"));
    assertFalse(LoginAccessValidationService.isMainlandChina(data));
  }

  @Test
  public void normalizeIpNcGyMainlandChina() {
    JSONObject data = LoginAccessValidationService.normalizeIpData(
        "ip.nc.gy",
        JSON.parseObject("""
            {
              "country":{"iso_code":"CN","names":{"en":"China","zh-CN":"中国"}},
              "subdivisions":[{"names":{"en":"Jiangsu","zh-CN":"江苏"}}]
            }
            """)
    );

    assertEquals("CN", data.getString("country_id"));
    assertEquals("中国", data.getString("country"));
    assertEquals("江苏", data.getString("region"));
    assertTrue(LoginAccessValidationService.isMainlandChina(data));
  }

  @Test
  public void blockHongKongRegion() {
    JSONObject data = JSON.parseObject("""
        {"country_id":"CN","country":"中国","region":"香港"}
        """);

    assertFalse(LoginAccessValidationService.isMainlandChina(data));
    assertTrue(LoginAccessValidationService.isBlockedIpRegion(data));
  }

  @Test
  public void blockHongKongCountryCode() {
    JSONObject data = JSON.parseObject("""
        {"country_id":"HK","country":"Hong Kong","region":"Kwai Tsing District"}
        """);

    assertTrue(LoginAccessValidationService.isBlockedIpRegion(data));
  }

  @Test
  public void blockMacauCountryCode() {
    JSONObject data = JSON.parseObject("""
        {"country_id":"MO","country":"Macao","region":""}
        """);

    assertTrue(LoginAccessValidationService.isBlockedIpRegion(data));
  }

  @Test
  public void blockSingaporeCountryCode() {
    JSONObject data = JSON.parseObject("""
        {"country_id":"SG","country":"Singapore","region":""}
        """);

    assertTrue(LoginAccessValidationService.isBlockedIpRegion(data));
  }

  @Test
  public void blockTaiwanRegion() {
    JSONObject data = JSON.parseObject("""
        {"country_id":"CN","country":"中国","region":"台湾"}
        """);

    assertFalse(LoginAccessValidationService.isMainlandChina(data));
    assertTrue(LoginAccessValidationService.isBlockedIpRegion(data));
  }

  @Test
  public void blockTaiwanCountryCode() {
    JSONObject data = JSON.parseObject("""
        {"country_id":"TW","country":"Taiwan","region":""}
        """);

    assertTrue(LoginAccessValidationService.isBlockedIpRegion(data));
  }
}
