<?php

namespace Api\PhpServices\Login;

use Api\PhpUtils\Common\TimeOut;
use Api\PhpUtils\Http\Request;
use App\Exception\custom\CodeSpecialException;

class Login
{
    /**
     * 访客内网登录
     *
     * @doc http://ydwiki.yidian-inc.com/pages/viewpage.action?pageId=59676495
     * @param array $user
     * @param string $partner 调用方(包括一点资讯yidian，VPN项目hubble，地铁metro）
     * @return array
     */
    public static function guest(array $user, string $key, string $partner){
        $url =  config("interface","service.login.guest");
        if(!$url){
            throw new CodeSpecialException("failed");
        }

        $user["partner"]    = $partner;
        $user["key"]        = $key;

        $res = (new TimeOut())->runGet($url, $user);
        if(!$res){
            throw new CodeSpecialException("timeout");
        }

        if(!isset($res["code"])){
            throw new CodeSpecialException("user_login");
        }

        if($res["code"] == 0){
            unset($res["code"]);
            unset($res["status"]);
            return $res;
        }

        switch ($res["code"]/1){
            case 10:
                throw new CodeSpecialException("param");
            case 14:
                throw new CodeSpecialException("permission_denied");
            case 15:
                throw new CodeSpecialException("sign");
            case 21:
                throw new CodeSpecialException("mysql");
            default:
                throw new CodeSpecialException("user_login");
        }
    }


    /**
     * 手机号内网登录
     *
     * @doc http://ydwiki.yidian-inc.com/pages/viewpage.action?pageId=57606923
     */
    public static function mobile(array $user, string $key, string $partner){
        $url =  config("interface","service.login.mobile");
        if(!$url){
            throw new CodeSpecialException("failed");
        }

        $user["partner"]    = $partner;
        $user["key"]        = $key;

        $res = (new TimeOut())->runGet($url, $user);
        if(!$res){
            throw new CodeSpecialException("timeout");
        }

        if(!isset($res["code"])){
            throw new CodeSpecialException("user_login");
        }

        if($res["code"] == 0){
            unset($res["code"]);
            unset($res["status"]);
            return $res;
        }

        switch ($res["code"]/1){
            case 10:
                throw new CodeSpecialException("param");
            case 14:
                throw new CodeSpecialException("permission_denied");
            case 21:
                throw new CodeSpecialException("user_mobile_error");
            case 32:
                throw new CodeSpecialException("user_mobile_registed");
            default:
                throw new CodeSpecialException("user_login");
        }
    }


    /**
     * 发送短信验证码服务
     * http://ydwiki.yidian-inc.com/pages/viewpage.action?pageId=58693127
     * @param array $data
     * @param string $template 模板
     * @param string $partner 调用方(包括一点资讯yidian，VPN项目hubble，地铁metro等）
     * @param int $code_lenght 验证码长度，默认4位
     * @param int $code_expire 验证码有效期，默认300秒（5分钟）
     * @param int $resend_times 验证周期内验证码最大发送次数，默认10次
     * @param int $retry_times 验证周期内每个验证码最大可校验次数，默认10次
     * @param int $ticket_expire 验证码验证周期，默认86400秒（1天）
     * @throws CodeSpecialException
     * @return true
     */
    public static function sendSmsCode(array $data, int $template, string $key, string $partner, $code_lenght =4, $code_expire=300, $resend_times=10, $retry_times=10, $ticket_expire=86400){
        $url =  config("interface","service.login.send_sms_code");
        if(!$url){
            throw new CodeSpecialException("failed");
        }

        $data = array_merge($data,[
            "key"           => $key,
            "partner"       => $partner,
            "template"      => $template,
            "code_lenght"   => $code_lenght,
            "ticket_expire" => $ticket_expire,
            "code_expire"   => $code_expire,
            "resend_times"  => $resend_times,
            "retry_times"   => $retry_times,
        ]);

        $res = (new Request())->get($url, $data, 60000);
        if(!$res){
            throw new CodeSpecialException("timeout");
        }

        if(!isset($res["code"])){
            throw new CodeSpecialException("failed");
        }

        if($res["code"] == 0){
            return true;
        }

        switch ($res["code"]/1){
            case 10:
                throw new CodeSpecialException("param");
            case 14:
                throw new CodeSpecialException("permission_denied");
            case 221:
                throw new CodeSpecialException("captcha_sent");
            case 30:
                throw new CodeSpecialException("mobile_error");
            case 23:
                throw new CodeSpecialException("memcache");
            case 224:
                throw new CodeSpecialException("captcha_delay_retry");
            default:
                throw new CodeSpecialException("failed");
        }
    }


    /**
     * 校验短信验证码
     * @param array $data
     * @param string $partner
     * @return true
     * @throws CodeSpecialException
     */
    public static function checkSmsCode(array $data, string $key, string $partner){
        $url =  config("interface","service.login.check_sms_code");
        if(!$url){
            throw new CodeSpecialException("failed");
        }

        $data = array_merge($data,[
            "partner"       => $partner,
            "key"           => $key,
        ]);

        $res = (new TimeOut())->runGet($url, $data);
        if(!$res){
            throw new CodeSpecialException("timeout");
        }

        if(!isset($res["code"])){
            throw new CodeSpecialException("failed");
        }

        if($res["code"] == 0){
            return true;
        }

        switch ($res["code"]/1){
            case 10:
                throw new CodeSpecialException("param");
            case 14:
                throw new CodeSpecialException("permission_denied");
            case 220:
                $reasion = $res["reason"] ?? "";
                throw new CodeSpecialException("captcha", $reasion);
            case 222:
                throw new CodeSpecialException("captcha_timeout");
            case 223:
                throw new CodeSpecialException("captcha_forbidden");
            default:
                throw new CodeSpecialException("failed");
        }
    }
}