<?php

namespace Bdp\Modules\EventParticipants\Controllers;

use Bdp\Libs\FileAccess;
use SearchMemberRequest;


class MainController
{
    public const KOMPASS_EVENTS_EVENTS = 'kompass_veranstaltungen_index';
	public const KOMPASS_EVENTS_PARTICIPANTS = 'kompass_veranstaltungen_teilis';

	public static function setup()
    {
	    global $wpdb;

        $charset = $wpdb->get_charset_collate();
        $fileReader = new FileAccess();
        foreach ([self::KOMPASS_EVENTS_EVENTS, self::KOMPASS_EVENTS_PARTICIPANTS] as $table) {
	        $sqlTable = $wpdb->prefix . $table;
	        $sql      = "SHOW TABLES LIKE '$sqlTable'";

	        $result = $wpdb->get_var( $sql );
	        if ( $result == $sqlTable ) {
		        continue;
	        }

	        $sqlSetup = str_replace(
		        '%tablename%',
		        $sqlTable,
		        $fileReader->get_contents( WP_PLUGIN_DIR . '/' . BDP_LV_PLUGIN_SLUG . '/lib/database/' . $table . '.sql' ) );

	        $sqlSetup = str_replace('%charset%', $charset, $sqlSetup);
	        $sqlSetup = str_replace('%prefix%', $wpdb->prefix, $sqlSetup);

	        dbDelta( $sqlSetup );
        }

    }
    public function __construct()
    {
		global $dbHandler;

	    add_menu_page(
		    __('Events', BDP_LV_PLUGIN_SLUG),
		    __('Events', BDP_LV_PLUGIN_SLUG),
		    'send_mails',
		    'kompass-events',
		    [$this, 'router'],
		    'dashicons-tickets-alt',
		    3
	    );

		foreach ($dbHandler->readFromDb( self::KOMPASS_EVENTS_EVENTS, ['archived' => false]) as $currentEvent) {
			add_submenu_page(
				'kompass-events',
				$currentEvent->event_name,
				$currentEvent->event_name,
				'show_groups',
				'kompass-events&action=show-event&event-id=' . $currentEvent->id,
				[ $this, 'router' ]);
		}

	    add_submenu_page(
		    'kompass-events',
		    __('New Event', BDP_LV_PLUGIN_SLUG),
		    __('New Event', BDP_LV_PLUGIN_SLUG),
		    'show_groups',
		    'kompass-events&action=new-event',
		    [ $this, 'router' ]);
    }


    public function router()
    {
		if (isset($_REQUEST['action'])) {
            switch ($_REQUEST['action']) {
	            case 'show-event':
					new ListGroupsAndMembersController();
					break;

	            case 'show-participant':
					if (isset($_REQUEST['save'])) {
						new UpdateMemberDetailsController();
					}

					new MemberDetailsController();
					break;

	            case 'delete-participant':
					new DeleteMemberController();
					break;

	            case 'update-participant-amount':
		            new UpdateMemberAmountController();
		            break;

	            case 'open-event':
		            new OpenEvent();
		            break;

	            case 'close-event':
		            new CloseEvent();
		            break;

	            case 'archive-event':
		            new ArchiveEvent();
		            break;

                case 'edit-event':
                    new EditEvent();
                    break;

                case 'update-event':
                    new UpdateEvent();
                    break;

                case 'new-event':
                    new NewEvent();
                    break;

                case 'create-event':
                    new CreateEvent();
                    break;

                case 'kompass_print_participant_mail_form':
                    new MemberMailFormController();
                    break;

                case 'send-mail-to-all':
                    new EventMailFormController();
                    break;

                case 'send-mail-to-group':
                    new EventGroupMailFormController();
                    break;

                case 'send-mail-to-local-group':
                    new EventLocalGroupMailFormController();
                    break;

                case 'searchmember':
					$memberList = SearchMemberRequest::listByName($_POST['member_name']);
					new PrintMemberListController($memberList);
	                break;

	            case 'create_group_form':
		            new CreateGroupController();
		            break;

	            case 'update-group':
					\UpdateGroupAction::execute(['gruppen_name' => $_REQUEST['kompass_groups_group_name']],
						(int)$_REQUEST['group_id']);

					new PrintGroupsController();
					break;

	            case 'create-group':
					$data = ['gruppen_name' => $_REQUEST['kompass_groups_group_name']];
					\CreateGroupAction::execute($data);
					new PrintGroupsController();
					break;

	            case 'new-member':
		            new CreateMemberController();
		            break;

	            case 'create-member':
					$userData = \CreateGroupMemberDataAction::execute($_REQUEST);
					if (count($userData['errors']) === 0) {
						$memberId = \CreateGroupMemberAction::execute( $userData['data'] );
						new PrintMemberController($memberId);
					} else {
						kompass_print_message_box(implode('<br />', $userData['errors']), 'error');
						new CreateMemberController($userData['rawData']);
						exit;
					}
		            new PrintGroupsController();
		            break;

	            case 'show-members':
					$memberList = \ListMemberRequest::listForGroup((int)$_REQUEST['group-id']);
		            new PrintMemberListController($memberList);
					break;

	            case 'show-member':
		            new PrintMemberController((int)$_REQUEST['member-id']);
		            break;

	            case 'compose-mail':
					current_user_can('send_mails');
		            break;

	            case 'delete-group':
	            if (current_user_can('delete_groups')) {
	            }
					break;
	            case 'edit-group':
		            if (current_user_can('edit_groups')) {
						kompass_edit_group_form($_REQUEST['group-id']);
		            }
					break;

	            default:
	                new ListEventsController();
            }
        } else {

	        new ListEventsController();
        }
    }

	private function listMembers()
	{

	}

    private function printMembers(array $memberList)
    {

    }

}