<?php

namespace App\Http\Controllers;

use App\Good;
use App\Log;
use App\Trace;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Vinkla\Hashids\Facades\Hashids;
use Zhuzhichao\IpLocationZh\Ip;

class HomeController extends Controller
{
    public function key(Request $request, $key)
    {
        $ip = '';
        if ($this->is_weixin()) {
            $ip = $this->get_client_ip();
        } else {
            $ip = $request->getClientIp();
        }
        $location = '';
        $trace = Trace::where('slug', $key)->firstOrFail();
        if (is_null($trace->look_time)) {
            $trace->look_time = Carbon::now();
        }

        $trace->look_count += 1;
        $trace->save();

        Log::create([
            'gid' => $trace->gid,
            'tid' => $trace->id,
            'ip' => $request->getClientIp()
        ]);

        $good = Good::find($trace->gid);

        $templat = collect(Good::$templetOption)->where('id', $good->templet)->first()['value'] ?? 'key';

        return view("home.{$templat}", compact('good', 'trace', 'location', 'ip'));
    }

    public function test(Request $request)
    {
        $ip = $request->get('ip');

        $data = json_decode(file_get_contents('https://apis.map.qq.com/ws/location/v1/ip?ip=' . $ip . '&key=NLWBZ-PZPKX-4S54G-TMF4X-NHJA6-PDBRP'), true);
        if ($data['status'] !== 0) return '';
        $data = $data['result']['ad_info'];
        $location = $data['nation'] . $data['province'] . $data['city'];

        return response()->json($location);
    }


    public function is_weixin()
    {
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
            return true;
        }
        return false;

    }

    public function get_client_ip()
    {
        $ip = '';
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        $ip_arr = explode(',', $ip);
        return $ip_arr[0];
    }


    public function getIpLocation($ip)
    {

        $url = "https://apis.map.qq.com/ws/location/v1/ip";
        $params = array(
            "ip" => $ip,
            'key' => 'NLWBZ-PZPKX-4S54G-TMF4X-NHJA6-PDBRP'
        );
        $paramstring = http_build_query($params);
        $content = $this->juheCurl($url, $paramstring);
        $result = json_decode($content, true);

        if ($result['status'] !== 0) return '';
        $data = $result['result']['ad_info'];
        $location = $data['nation'] . $data['province'] . $data['city'];
        return $location;
    }

    public function juheCurl($url, $params = false, $ispost = 0)
    {
        $httpInfo = array();
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'JuheData');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        if ($ispost) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt($ch, CURLOPT_URL, $url);
        } else {
            if ($params) {
                curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
            } else {
                curl_setopt($ch, CURLOPT_URL, $url);
            }
        }
        $response = curl_exec($ch);
        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        curl_close($ch);
        return $response;
    }
}
