<?php

namespace HaoZiTeam\IPUA;

use Exception;
use WP_Http;
use HaoZiTeam\IPUA\Service\IP as IP;
use HaoZiTeam\IPUA\Service\UA as UA;

function request( $url, $method = 'GET' ) {
	$http = new WP_Http();

	// 统一去除字符串转义，否则数据无法被 JSON 解析
	foreach ( $method == 'post' ?: array() as $k => $v ) {
		$post[ $k ] = stripcslashes( $v );
	}

	return $http->request(
		$url,
		array(
			'method'      => $method,
			'body'        => isset( $post ) ? $post : null,
			'timeout'     => 20,
			'httpversion' => '1.1',
			'sslverify'   => false
		)
	)['body'];
}

function get_ip_address( $ip = "" ) {
	if ( empty( $ip ) ) {
		try {
			global $comment;
			if ( ! isset( $comment->comment_author_IP ) ) {
				return '自动获取IP失败';
			}
			$ip = $comment->comment_author_IP;
		} catch ( Exception $e ) {
			return '自动获取IP失败';
		}
	}

	$ips     = new IP( $ip );
	$ip_info = $ips->get_ip_info();
	$setting = is_multisite() ? get_site_option( 'wp_ipua_setting' ) : get_option( 'wp_ipua_setting' );
	if ( ! is_array( $setting ) || empty( $setting ) ) {
		return '你需要先在后台配置插件';
	}

	if ( isset( $setting['from'] ) && $setting['from'] == 'qqwry' ) {
		return $ip_info['country'] . ' ' . $ip_info['area'];
	}

	$ip_info['nation']   = isset( $ip_info['nation'] ) ? $ip_info['nation'] : '';
	$ip_info['province'] = isset( $ip_info['province'] ) ? $ip_info['province'] : '';
	$ip_info['city']     = isset( $ip_info['city'] ) ? $ip_info['city'] : '';

	switch ( $setting['ip_format'] ) {
		case 'n-p-c':
			return $ip_info['nation'] . ' ' . $ip_info['province'] . ' ' . $ip_info['city'];
		case 'np':
			return $ip_info['nation'] . $ip_info['province'];
		case 'np2':
			return ( $ip_info['nation'] == '中国' ? '' : $ip_info['nation'] ) . $ip_info['province'];
		case 'n-p  ':
			return $ip_info['nation'] . ' ' . $ip_info['province'];
		case 'pc':
			return $ip_info['province'] . $ip_info['city'];
		case 'p-c':
			return $ip_info['province'] . ' ' . $ip_info['city'];
		case 'n':
			return $ip_info['nation'];
		case 'p':
			return $ip_info['province'];
		case 'c':
			return $ip_info['city'];
		default:
			return $ip_info['nation'] . $ip_info['province'] . $ip_info['city'];
	}
}

function get_ua_info( $ua = "" ) {
	if ( empty( $ua ) ) {
		try {
			global $comment;
			if ( ! isset( $comment->comment_agent ) ) {
				return '自动获取UA失败';
			}
			$ua = $comment->comment_agent;
		} catch ( Exception $e ) {
			return '自动获取UA失败';
		}
	}

	$uas     = new UA( $ua );
	$ua_info = $uas->get_ua_info();
	$setting = is_multisite() ? get_site_option( 'wp_ipua_setting' ) : get_option( 'wp_ipua_setting' );
	if ( ! is_array( $setting ) || empty( $setting ) ) {
		return '你需要先在后台配置插件';
	}

	$ua_info['platform'] = isset( $ua_info['platform'] ) ? $ua_info['platform'] : '';
	$ua_info['browser']  = isset( $ua_info['browser'] ) ? $ua_info['browser'] : '';
	$ua_info['version']  = isset( $ua_info['version'] ) ? $ua_info['version'] : '';

	switch ( $setting['ua_format'] ) {
		case 'pbv':
			return $ua_info['platform'] . ' ' . $ua_info['browser'] . ' ' . $ua_info['version'];
		case 'p':
			return $ua_info['platform'];
		case 'b':
			return $ua_info['browser'];
		case 'bv':
			return $ua_info['browser'] . ' ' . $ua_info['version'];
		default:
			return $ua_info['platform'] . ' ' . $ua_info['browser'];
	}
}
