<?php

namespace App\TelegramBot;

use App\Models\users;
use App\TelegramBot\Handlers\MainHandler;
use PhpParser\Node\Expr\Cast\Object_;

class CallbackHandler
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    private $api;

    public function __construct()
    {

    }

    public function handle($update, $token)
    {


        $chat_id = $update->from->id;
        $data = $update->data;

        $user = users::where('chat_id', $chat_id)->first();

        $data = explode(":",$data);
        $handler = $data[0];
        $action = $data[1];
        $params = array_slice($data,2);

        $handler = "App\TelegramBot\Handlers\\" . ucfirst($handler) . "Handler";
        $handler = new $handler($user, $token);


        return $handler->{$action}([
            'data'=> $params,
            "message_id" => $update->message->message_id,
            "callback_id" => $update->id
        ]);
    }
}
