<?php

require(get_template_directory() . "/dist/wp_bootstrap_navwalker.php"); //  bootstrap navwalker for nav

/** CONFIGURATION THINGS **/

add_theme_support('post-thumbnails');
set_post_thumbnail_size(150, 150, true);
register_nav_menu("primary", "Top Menu");

/** STYLES **/

function ams_enqueue_styles() {
  wp_enqueue_style('bootstrap', get_template_directory_uri() . '/dist/bootstrap/bootstrap.css', array(), '4.0.0-3');
  wp_enqueue_style('slick', get_template_directory_uri() . '/dist/slick/slick.css', array(), '1.6.0');
  wp_enqueue_style('slick-theme', get_template_directory_uri() . '/dist/slick/slick-theme.css', array("slick"), '1.6.0');
  wp_enqueue_style('ams', get_template_directory_uri() . '/style.css', array("slick", "slick-theme", "bootstrap"), '1.0.0');
}

function ams_enqueue_scripts() {
  wp_enqueue_script('bootstrap', get_template_directory_uri() . '/dist/bootstrap/bootstrap.js', array("jquery"), '4.0.0-3');
  wp_enqueue_script('stickyfill', get_template_directory_uri() . '/dist/stickyfill/stickyfill.js', array("jquery"), '1.1.4');
  wp_enqueue_script('slick', get_template_directory_uri() . '/dist/slick/slick.js', array("jquery"), '1.6.0');
}

add_action('wp_enqueue_scripts', 'ams_enqueue_styles');
add_action('wp_enqueue_scripts', 'ams_enqueue_scripts');

/** MCE BUTTON COLUMN **/

function ams_quicktag() {
  if(wp_script_is('quicktags')) { ?>
    <script type="text/javascript">
      QTags.addButton( 'eg_ams_column', 'column', '<!--column-->', '', '', 'New column tag');
    </script><?php
  }
}
add_action( 'admin_print_footer_scripts', 'ams_quicktag' );

/** CUSTOM POST TYPES **/

add_action('init', 'create_post_types');
function create_post_types() {
  register_post_type( 'partners',
    array(
      'labels' => array(
        'name' => __( 'Initiativpartner' ),
        'singular_name' => __( 'Partner' )
      ),
      'public' => true,
      'has_archive' => true,
      'menu_icon' => 'dashicons-admin-users',
      'supports' => array('title', 'editor', 'thumbnail'),
      'exlude_from_search' => true
    )
  );

  register_post_type( 'tdas',
    array(
      'labels' => array(
        'name' => __( 'TdA' ),
        'singular_name' => __( 'TdA' )
      ),
      'public' => true,
      'has_archive' => true,
      'menu_icon' => 'dashicons-calendar-alt',
      'supports' => array('title', 'editor', 'thumbnail'),
    )
  );

  register_post_type( 'projects',
    array(
      'labels' => array(
        'name' => __( 'Projekte' ),
        'singular_name' => __( 'Projekt' )
      ),
      'public' => true,
      'has_archive' => true,
      'menu_icon' => 'dashicons-calendar-alt',
      'supports' => array('title', 'editor', 'thumbnail'),
      'rewrite' => array('slug' => 'projekte'),
    )
  );
}

/** POST META BOX **/

add_action( 'load-post.php', 'ams_post_box_setup' );
add_action( 'load-post-new.php', 'ams_post_box_setup' );

function ams_post_box_setup() {
  add_action( 'add_meta_boxes', 'ams_post_box_add' );
  add_action( 'save_post', 'ams_post_box_save', 10, 2 );
}

function ams_post_box_add() {
  add_meta_box(
    'ams_post_box',      // Unique ID
    esc_html__( 'Post type'),    // Title
    'ams_post_box',   // Callback function
    'post',         // Admin page (or post type)
    'side',         // Context
    'default'         // Priority
  );
}

function ams_post_box( $object, $box ) { ?>
  <?php wp_nonce_field( basename( __FILE__ ), 'ams_post_box_nonce' ); ?>
  <p>
    <label for="ams_dual_columns_post">2 Spalten</label>
    <input type="checkbox" name="ams_dual_columns_post" id="ams_dual_columns_post" style="float: right;" <?php if(get_post_meta($object->ID, 'ams_dual_columns_post', true)) { echo "checked"; } ?> />
    <br />
    <label for="ams_tda_gallery">TdA Gallery</label>
    <input type="checkbox" name="ams_tda_gallery" id="ams_tda_gallery" style="float: right;" <?php if(get_post_meta($object->ID, 'ams_tda_gallery', true)) { echo "checked"; } ?> />
    <br />
    <label for="ams_partners">Partner</label>
    <input type="checkbox" name="ams_partners" id="ams_partners" style="float: right;" <?php if(get_post_meta($object->ID, 'ams_partners', true)) { echo "checked"; } ?> />
    <br />
    <label for="ams_projects">Projekte</label>
    <input type="checkbox" name="ams_projects" id="ams_projects" style="float: right;" <?php if(get_post_meta($object->ID, 'ams_projects', true)) { echo "checked"; } ?> />
  </p><?php
}

function ams_post_box_save( $post_id, $post ) {
  $inputs = array(
    "ams_dual_columns_post",
    "ams_tda_gallery",
    "ams_partners",
    "ams_projects"
  );

  if(!isset( $_POST['ams_post_box_nonce']) || !wp_verify_nonce($_POST['ams_post_box_nonce'], basename(__FILE__))) {
    return $post_id;
  }

  $post_type = get_post_type_object( $post->post_type );

  if(!current_user_can( $post_type->cap->edit_post, $post_id)) {
    return $post_id;
  }

  foreach($inputs as $input) {
    if(isset($_POST[$input]) && !get_post_meta( $post_id, $input, true )) {
      add_post_meta( $post_id, $input, true, true );
    } else if(!isset($_POST[$input]) && get_post_meta( $post_id, $input, true )) {
      delete_post_meta( $post_id, $input);
    }
  }
}

/** TdA Slider **/

global $tdaSlider; // TODO: remove this

$tdaSlider = array(
  "TdA2016" => array(
    "url" => "http://www.architekturmachtschule.de/wp-content/uploads/2015/11/",
    "tag-der-architektur-2015-01.jpg",
    "tag-der-architektur-2015-02.jpg",
    "tag-der-architektur-2015-03.jpg",
    "tag-der-architektur-2015-04.jpg",
    "tag-der-architektur-2015-05.jpg",
    "tag-der-architektur-2015-06.jpg",
    "tag-der-architektur-2015-07.jpg",
    "tag-der-architektur-2015-08.jpg",
    "tag-der-architektur-2015-09.jpg",
    "tag-der-architektur-2015-10.jpg",
    "tag-der-architektur-2015-11.jpg",
    "tag-der-architektur-2015-12.jpg",
    "tag-der-architektur-2015-13.jpg",
    "tag-der-architektur-2015-14.jpg",
    "tag-der-architektur-2015-15.jpg",
    "tag-der-architektur-2015-16.jpg",
    "tag-der-architektur-2015-17.jpg",
    "tag-der-architektur-2015-18.jpg",
    "tag-der-architektur-2015-19.jpg",
    "tag-der-architektur-2015-20.jpg",
    "tag-der-architektur-2015-21.jpg",
    "tag-der-architektur-2015-22.jpg",
    "tag-der-architektur-2015-23.jpg",
    "tag-der-architektur-2015-24.jpg",
    "tag-der-architektur-2015-25.jpg",
    "tag-der-architektur-2015-26.jpg",
    "tag-der-architektur-2015-27.jpg",
    "tag-der-architektur-2015-28.jpg",
    "tag-der-architektur-2015-29.jpg",
    "tag-der-architektur-2015-30.jpg",
    "tag-der-architektur-2015-31.jpg",
    "tag-der-architektur-2015-32.jpg",
    "tag-der-architektur-2015-33.jpg",
    "tag-der-architektur-2015-34.jpg",
    "tag-der-architektur-2015-35.jpg",
    "tag-der-architektur-2015-36.jpg",
    "tag-der-architektur-2015-37.jpg",
    "tag-der-architektur-2015-38.jpg",
    "tag-der-architektur-2015-39.jpg",
    "tag-der-architektur-2015-40.jpg",
    "tag-der-architektur-2015-41.jpg",
    "tag-der-architektur-2015-42.jpg",
    "tag-der-architektur-2015-43.jpg"
  ),

);

// [carousel id="TdA2016"]
function carousel_shortcode_func($atts) {
  global $tdaSlider;
  $a = shortcode_atts( array(
      'id' => NULL
  ), $atts );

  if($a['id'] != NULL && array_key_exists($a['id'], $tdaSlider)) {
    $slider = $tdaSlider[$a['id']];

    $output = '<div class="owl-carousel">';
    foreach($slider as $key => $value) {
      if($key == "url") {
        continue;
      }
      $output .= '<div class="item" id="item-' . $key . '"><img id="img-' . $key . '" data-lazy="' . $slider['url'] . $value . '" style="height: 300px; width: auto;"></div>' . PHP_EOL;
    }
    $output .= '</div>';

    return $output;
  } else {
    return "<span style=\"color: red;\">Wrong Slider ID!</span>";
  }
}
add_shortcode('carousel', 'carousel_shortcode_func');

/* TITLE STUFF */

function ams_modify_titles($title, $id = null) {
  if(get_post_type() == "tdas") {
    return "Tag der Architektur" . preg_replace("/&#?[a-z0-9]{2,8};/i", "", $title) . " &raquo; " . get_bloginfo("name");
  }

  if(is_front_page()) {
    return get_bloginfo("name");
  }

  return ltrim(preg_replace("/&#?[a-z0-9]{2,8};/i","", $title)) . " &raquo; " . get_bloginfo("name");
}
add_filter( 'wp_title', 'ams_modify_titles', 10, 2 );

?>