<?php

namespace App\Game\Services;

use App\Game\Telegram\TelegramUser;

class TelegramAppService
{
    protected $client;
    protected $baseUrl;
    public $secretKey;

    public $token;

    public $salt;

    public function __construct()
    {

    }

    /**
     * @param string $hash
     * @return TelegramUser
     */
    public static function decodeHash(string $hash){
        $data=[];
        parse_str(urldecode($hash),$data);
        if(isset($data['user'])){
            $user=json_decode($data['user'],true);
            $user['chat_instance']=$data['chat_instance'];
            $user['chat_type']=$data['chat_type'];

            $teleUser=TelegramUser::query()->where('id',$user['id'])->first();
            if(!$teleUser)$teleUser=TelegramUser::create($user);
            $teleUser->increment('enter_times',1);
//            dd($teleUser);
            return $teleUser;
        }
        dd($data);
    }
}
