<?php
require_once('vendor/autoload.php');

use Illuminate\Support\Facades\Redirect;
use WoWPress\Models\Character;
use WoWPress\Models\Complaint;
use WoWPress\Models\Log;
use WoWPress\Models\Notification;
use WoWPress\Models\Raid;
use WoWPress\Models\Signup;
use WoWPress\Models\SKS;
use WoWPress\Models\User;

if (empty($_POST['action'])) {
    exit(404);
}

if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php')) {
    /** Loads the WordPress Environment and Template */
    require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
}


switch ($_POST['action']) {
    case 'addCharacter':
        isAllowed('wowpress_edit_characters');
        if (isset($_POST['addCharacter_nonce']) && wp_verify_nonce($_POST['addCharacter_nonce'], 'addCharacter')) {
            if (isset($_POST['name']) && isset($_POST['realm'])) {
                $char = Character::whereName($_POST['name'])->whereRealm($_POST['realm'])->first();
                if (empty($char->ID)) {
                    $char = new Character();
                    $char->name = $_POST['name'];
                    $char->realm = $_POST['realm'];
                    $char->updateFromAPI();
                    $char->updateMedia();
                }
            }
        }
        break;
    case 'deleteCharacter':
        isAllowed('wowpress_edit_characters');
        if (isset($_POST['deleteCharacter_nonce']) && wp_verify_nonce($_POST['deleteCharacter_nonce'], 'deleteCharacter')) {
            if (isset($_POST['id'])) {
                $char = Character::find($_POST['id']);
                if ($char->ID) {
                    $char->delete();
                }
            }
        }
        break;
    case 'updateCharacter':
        isAllowed('wowpress_edit_characters');
        if (isset($_POST['updateCharacter_nonce']) && wp_verify_nonce($_POST['updateCharacter_nonce'], 'updateCharacter')) {
            if (isset($_POST['id'])) {
                $char = Character::find($_POST['id']);
                if ($char->ID) {
                    $char->updateFromAPI();
                    $char->updateMedia();
                }
            }
        }
        break;
    case 'toggleRaidchar':
        isAllowed('wowpress_edit_characters');
        if (isset($_POST['toggleRaidchar_nonce']) && wp_verify_nonce($_POST['toggleRaidchar_nonce'], 'toggleRaidchar')) {
            if (isset($_POST['id'])) {
                $char = Character::find($_POST['id']);
                if ($char->ID) {
                    $char->raidchar = !$char->raidchar;
                    $char->save();
                }
            }
        }
        break;
    case 'changeUser':
        isAllowed('wowpress_edit_characters');
        if (isset($_POST['changeUser_nonce']) && wp_verify_nonce($_POST['changeUser_nonce'], 'changeUser')) {
            if (isset($_POST['char_id']) && isset($_POST['uid'])) {
                $char = Character::find($_POST['char_id']);
                $user = User::find($_POST['uid']);
                if (!empty($char->ID)) {
                    if (empty($user->ID)) {
                        $char->user_id = null;
                        $char->save();
                    } else
                        $char->user_id = $user->ID;
                    $char->save();
                }
            }
        }
        break;
    case 'importRaid':
        isAllowed('wowpress_edit_raids');
        if (isset($_POST['importRaid_nonce']) && wp_verify_nonce($_POST['importRaid_nonce'], 'importRaid')) {
            if (isset($_POST['id_wowaudit'])) {

                if (!empty(Raid::where('id_wowaudit', $_POST['id_wowaudit'])->first())) {
                    break;
                }
                $raid = new Raid();
                $raid->id_wowaudit = $_POST['id_wowaudit'];

                $raid->sync();

                if (!empty($raid->title)) {
                    $raid->save();
                }
            }
        }
        break;
    case 'signupRaid':
        isAllowed('wowpress_view_raids');
        if (isset($_POST['signupRaid_nonce']) && wp_verify_nonce($_POST['signupRaid_nonce'], 'signupRaid')) {

            if (isset($_POST['raid_id']) && isset($_POST['raid_character']) && isset($_POST['raid_status']) && isset($_POST['raid_role']) && isset($_POST['comment'])) {
                $char = Character::find($_POST['raid_character']);
                $raid = Raid::find($_POST['raid_id']);

                if ($char->can_edit) {
                    if (!$char->id_wowaudit) {
                        $cw = $char->user->characters()->whereNot('id_wowaudit', 'NULL')->first();

                        if (empty($cw->ID)) {
                            break;
                        }
                    } else {
                        $cw = $char;
                    }

                    $comment = htmlentities2($_POST['comment']);
                    $signup = new Signup();
                    $signup->raid_id = $raid->ID;
                    $signup->character_id = $cw->ID;
                    $signup->setStatus($_POST['raid_status']);
                    $signup->setRole($_POST['raid_role']);
                    $signup->comment = $comment;
                    $sup = $signup->updateAPI($char);

                    if (!empty($sup['error'])) {
                        dd($sup);
                    }

                    $raid->sync(true);
                    $raid->save();
                }
            }
        }
        break;
    case 'addSKS':
        isAllowed('wowpress_edit_raids');
        if (isset($_POST['addSKS_nonce']) && wp_verify_nonce($_POST['addSKS_nonce'], 'addSKS')) {
            if (isset($_POST['name']) && isset($_POST['realm']) && isset($_POST['list_name'])) {
                $char = Character::whereName($_POST['name'])->whereRealm($_POST['realm'])->first();
                if (empty($char->ID)) {
                    $char = new Character();
                    $char->name = $_POST['name'];
                    $char->realm = $_POST['realm'];
                }



                $sks = new SKS();
                $sks->char_name = $char->name;
                $sks->realm_name = $char->realm;
                $sks->list_name = $_POST['list_name'];
                $sks->active = true;
                $sks_last = SKS::where('list_name', $sks->list_name)->orderBy('rank', 'desc')->first();

                $sks->rank = !empty($sks_last->rank) ? $sks_last->rank + 1 : 1;

                $sks->save();
            }
        }
        break;
    case 'toggleSKS':
        isAllowed('wowpress_edit_raids');
        if (isset($_POST['toggleSKS_nonce']) && wp_verify_nonce($_POST['toggleSKS_nonce'], 'toggleSKS')) {
            if (isset($_POST['sks_id'])) {
                $sks = SKS::find($_POST['sks_id']);
                if ($sks->ID) {
                    $sks->active = !$sks->active;
                    $sks->save();
                }
            }
        }
        break;
    case 'deleteSKS':
        isAllowed('wowpress_edit_raids');
        if (isset($_POST['deleteSKS_nonce']) && wp_verify_nonce($_POST['deleteSKS_nonce'], 'deleteSKS')) {
            if (isset($_POST['sks_id'])) {
                $sks = SKS::find($_POST['sks_id']);
                if ($sks->ID) {
                    $after = $sks->getAfter(false);
                    foreach ($after as $sk) {
                        $sk->rank = $sk->rank - 1;
                        $sk->save();
                    }

                    $sks->delete();
                }
            }
        }
        break;
    case 'lootSKS':
        isAllowed('wowpress_edit_raids');
        if (isset($_POST['lootSKS_nonce']) && wp_verify_nonce($_POST['lootSKS_nonce'], 'lootSKS')) {
            if (isset($_POST['sks_id'])) {
                $sks = SKS::find($_POST['sks_id']);
                if ($sks->ID) {
                    $sks->loot();
                }
            }
        }
        break;
    case 'rollSKS':
        isAllowed('wowpress_edit_raids');
        if (isset($_POST['rollSKS_nonce']) && wp_verify_nonce($_POST['rollSKS_nonce'], 'rollSKS')) {
            if (isset($_POST['list_name'])) {
                $sks = SKS::where('list_name', $_POST['list_name'])->first();
                if (!empty($sks->ID)) {
                    $sks->roll();
                }
            }
        }
        break;
    case 'fixSKS':
        isAllowed('wowpress_edit_raids');
        if (isset($_POST['fixSKS_nonce']) && wp_verify_nonce($_POST['fixSKS_nonce'], 'fixSKS')) {
            if (isset($_POST['sks_id']) && isset($_POST['rank'])) {
                $sks = SKS::find($_POST['sks_id']);
                if ($sks->ID) {

                    $sks->rank = intval($_POST['rank']);
                    $sks->save();
                }
            }
        }
        break;
    case 'addComplaint':
        isAllowed('wowpress_view_complaints');
        if (isset($_POST['addComplaint_nonce']) && wp_verify_nonce($_POST['addComplaint_nonce'], 'addComplaint')) {
            if (isset($_POST['complaint'])) {
                $c = new Complaint();
                $c->user_id = get_current_user_id();
                $c->complaint = $_POST['complaint'];
                $c->anonymous = !empty($_POST['hide']);
                $c->save();
            }
        }
        break;
    case 'commentComplaint':
        isAllowed('wowpress_edit_complaints');
        if (isset($_POST['commentComplaint_nonce']) && wp_verify_nonce($_POST['commentComplaint_nonce'], 'commentComplaint')) {
            if (isset($_POST['complaint_id']) && isset($_POST['user_comment'])) {
                $c = Complaint::find($_POST['complaint_id']);
                if ($c) {
                    $c->user_comment = $_POST['user_comment'];
                    $c->save();
                    Notification::addNotification($c->user_id, "Neuer Kommentar im Kummerkasten!");
                }
            }
        }
        break;
    case 'unhideComplaint':
        isAllowed('wowpress_view_complaints');
        if (isset($_POST['unhideComplaint_nonce']) && wp_verify_nonce($_POST['unhideComplaint_nonce'], 'unhideComplaint')) {
            if (isset($_POST['complaint_id'])) {
                $c = Complaint::find($_POST['complaint_id']);
                if ($c && $c->user_id == get_current_user_id()) {
                    $c->anonymous = false;
                    $c->save();
                }
            }
        }
        break;
    case 'slanderComplaint':
        isAllowed('wowpress_edit_complaints');
        if (isset($_POST['slanderComplaint_nonce']) && wp_verify_nonce($_POST['slanderComplaint_nonce'], 'slanderComplaint')) {
            if (isset($_POST['complaint_id']) && isset($_POST['admin_comment'])) {
                $c = Complaint::find($_POST['complaint_id']);
                if ($c) {
                    $c->admin_comment = $_POST['admin_comment'];
                    $c->save();
                }
            }
        }
        break;
    case 'trashComplaint':
        isAllowed('wowpress_view_complaints');
        if (isset($_POST['trashComplaint_nonce']) && wp_verify_nonce($_POST['trashComplaint_nonce'], 'trashComplaint')) {
            if (isset($_POST['complaint_id'])) {
                $c = Complaint::find($_POST['complaint_id']);
                if ($c && $c->canTrash()) {
                    $c->trash();
                }
            }
        }
        break;
    case 'restoreComplaint':
        isAllowed('wowpress_edit_complaints');
        if (isset($_POST['restoreComplaint_nonce']) && wp_verify_nonce($_POST['restoreComplaint_nonce'], 'restoreComplaint')) {
            if (isset($_POST['complaint_id'])) {
                $c = Complaint::find($_POST['complaint_id']);
                if ($c) {
                    $c->restore();
                }
            }
        }
        break;
    case 'deleteComplaint':
        isAllowed('wowpress_edit_complaints');
        if (isset($_POST['deleteComplaint_nonce']) && wp_verify_nonce($_POST['deleteComplaint_nonce'], 'deleteComplaint')) {
            if (isset($_POST['complaint_id'])) {
                $c = Complaint::find($_POST['complaint_id']);
                if ($c) {
                    Log::write(json_encode(['user' => get_current_user_id(), 'complaint_by' => $c->user_id]), Log::LOG_COMPLAINT_DELETE);
                    $c->delete();
                }
            }
        }
        break;
    case 'notificationSeen':
        if (is_user_logged_in()) {
            if (isset($_POST['notificationSeen_nonce']) && wp_verify_nonce($_POST['notificationSeen_nonce'], 'notificationSeen')) {
                if (isset($_POST['notification_id'])) {
                    $note = Notification::find($_POST['notification_id']);
                    if(!empty($note->ID && $note->user_id == get_current_user_id())){
                        $note->delete();
                    }
                }
            }
        }
        break;
}

header('Location: ' . $_SERVER['HTTP_REFERER']);
