<?php
class router {
    public function __construct() {
        $router = [
            '/' => ['index', 1],
            '/book' => ['book', 0],
            '/user' => ['user', 0],
            '/admin' => ['admin', 0],
            '/lists' => ['lists', 1],
            '/search' => ['search', 1]
        ];
        
        $to = ''; $uri = explode('?', $_SERVER['REQUEST_URI'])[0];
        foreach ($router as $k => $v)
            if ($k == ($v[1] ? $uri : substr($uri, 0, strlen($k)))) $to = $v[0];
        
        switch ($to) {
            case 'user':
                $controller = new user_controller();
                switch (explode('/', $uri)[2] ?? null) {
                    case 'login': $controller->login(); break;
                    case 'register': $controller->register(); break;
                    case 'info': $this->check_login(); $controller->info(); break;
                    case 'record': $this->check_login(); $controller->record(); break;
                    case 'update': $this->check_login(); $controller->update(); break;
                    case 'update-stock': $this->check_login(); $controller->update_stock(); break;
                    case 'update-record': $this->check_login(); $controller->update_record(); break;
                    case 'do': switch (explode('/', $uri)[3] ?? null) {
                        case 'login': $controller->do_login(); break;
                        case 'register': $controller->do_register(); break;
                        case 'logout': $this->check_login(); $controller->do_logout(); break;
                        case 'update': $this->check_login(); $controller->do_update(); break;
                        default: break;
                    } break;
                    default:
                        header('Location: /user/'.($this->check_login(0)?'info':'login')); break;
                } break;
            
            case 'admin':
                if (isset($_SESSION['user']) && $_SESSION['user']['type'] == 1) {
                    $controller = new admin_controller();
                    switch (explode('/', $uri)[2] ?? null) {
                        
                        case 'users': $controller->users(); break;
                        case 'books': $controller->books(); break;
                        case 'stock': $controller->stock(); break;
                        case 'categories': $controller->categories(); break;
                        
                        case 'add-user': $controller->add_user(); break;
                        case 'update-user': $controller->update_user(); break;
                        case 'delete-user': $controller->delete_user(); break;
                        
                        case 'add-book': $controller->add_book(); break;
                        case 'update-book': $controller->update_book(); break;
                        case 'delete-book': $controller->delete_book(); break;
                        
                        case 'add-stock': $controller->add_stock(); break;
                        case 'update-stock': $controller->update_stock(); break;
                        case 'delete-stock': $controller->delete_stock(); break;
                        
                        case 'add-category': $controller->add_category(); break;
                        case 'update-category': $controller->update_category(); break;
                        case 'delete-category': $controller->delete_category(); break;
                        
                        case 'do': switch (explode('/', $uri)[3] ?? null) {
                            
                            case 'add-user': $controller->do_add_user(); break;
                            case 'update-user': $controller->do_update_user(); break;
                            case 'delete-user': $controller->do_delete_user(); break;
                            
                            case 'add-book': $controller->do_add_book(); break;
                            case 'update-book': $controller->do_update_book(); break;
                            case 'delete-book': $controller->do_delete_book(); break;
                            
                            case 'add-stock': $controller->do_add_stock(); break;
                            case 'update-stock': $controller->do_update_stock(); break;
                            case 'delete-stock': $controller->do_delete_stock(); break;
                            
                            case 'add-category': $controller->do_add_category(); break;
                            case 'update-category': $controller->do_update_category(); break;
                            case 'delete-category': $controller->do_delete_category(); break;
                            
                            default: break;
                        } break;
                        
                        default: header('Location: '.($this->check_login(0)?'/admin/users':'/user/login')); break;
                    }
                } else header('Location: /user'); break;
            
            default:
                $controller = new default_controller();
                switch ($to) {
                    
                    case 'index': $controller->index(); break;
                    case 'lists': $controller->lists(); break;
                    case 'search': $controller->search(); break;
                    
                    case 'book': $controller->book((int)(explode('/', $uri)[2] ?? 0)); break;
                    
                    default: http_response_code(404); break;
                } break;
        }
    }
    
    private function check_login($jump = true) {
        return ($login = !isset($_SESSION['user'])) && $jump ? exit(header('Location: /user/login')) : !$login;
    }
    
}