<?php

use WoWPress\Models\User;

function set_roles()
{
    remove_role('editor');
    remove_role('contributor');
    remove_role('author');
    remove_role('subscriber');

    $admin = get_role('administrator');

    add_role('gildenmeister', __('Gildenmeister'), ['read' => true]);
    $gm = get_role('gildenmeister');

    add_role('offizier', __('Offizier'), ['read' => true]);
    $offi = get_role('offizier');

    add_role('raidlead', __('Raidlead'), ['read' => true]);
    $rl = get_role('raidlead');

    add_role('raider', __('Raider'), ['read' => true]);
    $raider = get_role('raider');

    add_role('novize', __('Novize'), ['read' => true]);
    $novize = get_role('novize');

    add_role('f_f', __('F&F'), ['read' => true]);
    $ff = get_role('f_f');

    add_role('nobody', __('Niemand'));

    update_option('default_role', 'nobody');

    $admin->add_cap('wowpress_edit_site', true);
    $admin->add_cap('wowpress_edit_raids', true);
    $admin->add_cap('wowpress_edit_characters', true);
    $admin->add_cap('wowpress_view_raids');
    $admin->add_cap('wowpress_view_teamspeak');
    $admin->add_cap('wowpress_view_birthdays');
    $admin->add_cap('wowpress_view_complaints');
    $admin->add_cap('wowpress_edit_complaints');



    foreach (get_role('administrator')->capabilities as $cap => $status) {
        $gm->add_cap($cap, $status);
        $offi->add_cap($cap, $status);
    }

    $rl->add_cap('wowpress_edit_raids', true);
    $rl->add_cap('wowpress_edit_characters', true);

    $rl->add_cap('wowpress_view_raids');
    $raider->add_cap('wowpress_view_raids');
    $novize->add_cap('wowpress_view_raids');
    $ff->add_cap('wowpress_view_raids');

    $rl->add_cap('wowpress_view_teamspeak');
    $raider->add_cap('wowpress_view_teamspeak');
    $novize->add_cap('wowpress_view_teamspeak');
    $ff->add_cap('wowpress_view_teamspeak');

    $rl->add_cap('wowpress_view_birthdays');
    $raider->add_cap('wowpress_view_birthdays');
    $novize->add_cap('wowpress_view_birthdays');
    $ff->add_cap('wowpress_view_birthdays');


    $rl->add_cap('wowpress_view_complaints');
    $raider->add_cap('wowpress_view_complaints');
    $novize->add_cap('wowpress_view_complaints');
    $ff->add_cap('wowpress_view_complaints');

}


function isAllowed($capability)
{
    if (!current_user_can($capability)) :
        include plugin_dir_path(__FILE__) . '../pages/404.php';
        die;
    endif;
}


function setGlobalUser(){
    global $user;
    $user = get_current_user_id();
    if ($user) {
        $user = User::find($user);
    } else {
        $user = new User();
        $user->ID  = 0;
        $user->login = "Gast";
    }
}

add_action( 'show_user_profile', 'wowpress_extra_user_profile_fields' );
add_action( 'edit_user_profile', 'wowpress_extra_user_profile_fields' );
function wowpress_extra_user_profile_fields( $user ) {
?>
  <h3><?php _e("Nette Zusatzinfos", "blank"); ?></h3>
  <table class="form-table">
    <tr>
      <th><label for="birthday"><?php _e("Geburtstag"); ?></label></th>
      <td>
        <input type="date" name="birthday" id="birthday" class="regular-text" 
            value="<?php echo esc_attr( get_the_author_meta( 'birthday', $user->ID ) ); ?>" /><br />
        <span class="description"><?php _e("Bitte gib doch dein Geburtsdatum ein :)"); ?></span>
    </td>
    </tr>
    <tr>
      <th><label for="btag"><?php _e("Battle-Tag"); ?></label></th>
      <td>
        <input type="text" name="btag" id="btag" class="regular-text" 
            value="<?php echo esc_attr( get_the_author_meta( 'btag', $user->ID ) ); ?>" /><br />
        <span class="description"><?php _e("Nur wenn du willst :D"); ?></span>
    </td>
    </tr>
  </table>
<?php
}

add_action( 'personal_options_update', 'wowpress_save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'wowpress_save_extra_user_profile_fields' );
function wowpress_save_extra_user_profile_fields( $user_id ) {
  $saved = false;
  if ( current_user_can( 'edit_user', $user_id ) ) {
    update_user_meta( $user_id, 'birthday', $_POST['birthday'] );
    update_user_meta( $user_id, 'btag', $_POST['btag'] );
    $saved = true;
  }
  return true;
}