<?php

namespace App\Services;

use App\Facades\CommonService;
use GuzzleHttp\Client;
use App\Models\WangYiYun;
use cccdl\yunxin_sdk\Im\User as ImUser;
use Illuminate\Support\Facades\Log;

class ImService
{

    public function updateImUser($user_id,$data){
        //更新用户信息
        $wangYiUser = WangYiYun::where('user_id',$user_id)->first();

        $imUser = new ImUser(config('chat.im.app_id'), config('chat.im.app_secret'));
        $ac_cid = make_wangyiyun_accid($user_id);

        $token = '';

        //更新网易用户数据前先查看用户是否存在网易云账号,不然这里会报错
        $exists = $this->getImUser($user_id);

        if ($exists) {
            Log::info("updateImUser user_id:{$user_id} 111");
            if($wangYiUser){
                Log::info("updateImUser user_id:{$user_id} 222");
                $token = $wangYiUser->token;
                $update = [];
                $wangYiUser->icon = $update['icon'] = $data->avatar??'';
                $wangYiUser->name = $update['name'] = $data->name??'';
                $wangYiUser->gender = $update['gender'] = $data->sex??0;
                $wangYiUser->birth = $update['birth'] = $data->birthday??'';

                $wangYiUser->save();
                $imUser->updateUserInfo($ac_cid,$update);
            }
        }else{
            Log::info("updateImUser user_id:{$user_id} 333");
            $insert = $yun = [];
            $insert['user_id'] = $user_id;
            $insert['accid'] = $ac_cid;
            $insert['name'] = $yun['name'] = $data->name??'';
            $insert['icon'] = $yun['icon'] = $data->avatar??'';
            $insert['email'] = $yun['email'] = $data->email??'';
            $insert['birth'] = $yun['birth'] = $data->birthday??'';
            $insert['gender'] = $yun['gender'] = $data->sex ?? 0;
            $imUserData = $imUser->create($ac_cid, $yun);
            $token = $imUserData['info']['token'];

            $insert['token'] = $token;
            //插入表数据数据
            if(!$wangYiUser){
                Log::info("updateImUser user_id:{$user_id} 444");
                WangYiYun::create($insert);
            }
        }

        if (empty($token)) {
            $token = CommonService::random(32);
            $imUser->update($ac_cid, $token);
            WangYiYun::where('accid', $ac_cid)->update(['token'=>$token]);
        }
    }

    public function getImUser($user_id){
        //如果网易云信存在用户则修改，不存在则创建
        $ac_cids = [make_wangyiyun_accid($user_id)];
        $appSecret = config('chat.im.app_secret');
        $time = time();
        $nonce = uniqid();
        $client = new Client();
        $response = $client->post('https://api.netease.im/nimserver/user/getUinfos.action', [
            'headers' => [
                'AppKey' => config('chat.im.app_id'),
                'Nonce' => $nonce,
                'CurTime' => $time,
                'CheckSum' => sha1($appSecret . $nonce . $time),
            ],
            'form_params' => [
                'accids' => json_encode($ac_cids),
            ],
        ]);
        // 处理响应
        $body = json_decode($response->getBody(), true);
        $flag = false;
        if ($body['code'] == 200) {
            $flag = true;
        }
        return $flag;
    }

    public function getImUserV2($user_id){
        //如果网易云信存在用户则修改，不存在则创建
        $ac_cids = [make_wangyiyun_accid($user_id)];
        $appSecret = config('chat.im.app_secret');
        $time = time();
        $nonce = uniqid();
        $client = new Client();
        $response = $client->post('https://api.netease.im/nimserver/user/getUinfos.action', [
            'headers' => [
                'AppKey' => config('chat.im.app_id'),
                'Nonce' => $nonce,
                'CurTime' => $time,
                'CheckSum' => sha1($appSecret . $nonce . $time),
            ],
            'form_params' => [
                'accids' => json_encode($ac_cids),
            ],
        ]);
        // 处理响应
        $body = json_decode($response->getBody(), true);

        return $body;
    }
}