var pkg = require('../package.json');
var txtdmn = pkg.theme_infos.textdomain;



var setupindexphp = ['<?php',
    '/**',
    '* The main app file',
    '*',
    '@package '+pkg.name,
    '@since '+pkg.version,
    '*/',
    'get_header();',
    '?>',
    '',
    '',
    '<!-- Example Top navbar fixed-->',
    '<header>',
    '   <div class="navbar-fixed">',
    '       <nav>',
    '           <div class="nav-wrapper">',
    '               <a href="#!" class="brand-logo">Logo</a>',
    '               <ul class="right hide-on-med-and-down">',
    '                   <li><a href="#">Link1</a></li>',
    '                   <li><a href="#">Link2</a></li>',
    '               </ul>',
    '           </div>',
    '       </nav>',
    '   </div>',
    '   <h1 class="center">'+pkg.name+'</h1>',
    '</header>',
    '',
    '',
    '',
    '<main class="row">',
    '   <router-view></router-view>',
    '</main>',
    '',
    '',
    '<?php get_footer();',
    '']
    .join('\n');
exports.setupindexphp = setupindexphp;



var setupheaderphp = ['<?php',
    '/**',
    '* Header file',
    '*',
    '@package '+pkg.name,
    '@since '+pkg.version,
    '*/',
    '?>',
    '<!DOCTYPE html>',
    '<html <?php language_attributes(); ?>>',
    '   <head>',
    '       <meta name="viewport" content="width=device-width, initial-scale=1" />',
    '       <?php wp_head(); ?>',
    '   </head>',
    '   <body>',
    '']
    .join('\n');
exports.setupheaderphp = setupheaderphp;



var setupfooterphp = ['<?php',
    '/**',
    '* Footer file',
    '*',
    '@package '+pkg.name,
    '@since '+pkg.version,
    '*/',
    '?>',
    '   <footer>',
    '   </footer>',
    '   <script src="https://code.createjs.com/preloadjs-0.6.2.min.js"></script>',
'       <?php wp_footer(); ?>',
    '</body>',
    '']
    .join('\n');
exports.setupfooterphp = setupfooterphp;



var setupfuncphp = ['<?php',
    '// Useful global constants',
    '',
    'define( "'+ txtdmn.toUpperCase()+'_VERSION","0.1.0" );',
    'define( "'+ txtdmn.toUpperCase()+'_URL",get_stylesheet_directory_uri() );',
    'define( "'+ txtdmn.toUpperCase()+'_TEMPLATE_URL", get_template_directory_uri() );',
    'define( "'+ txtdmn.toUpperCase()+'_PATH",get_template_directory() . "/" );',
    'define( "'+ txtdmn.toUpperCase()+'_INC",'+ txtdmn.toUpperCase()+'_PATH . "includes/" );',
    '',
    '',
    '/*************** ADD WORDPRESS SUPPORT', 
    '***************************/',
    'add_theme_support("post-thumbnails");',
    '',
    '',
    'if( function_exists("register_api_field") ):',
    'add_action( "init", "better_rest_api_featured_images_init", 12 );',
    '/**',
    '* Register our enhanced better_featured_image field to all public post types',
    '* that support post thumbnails.',
    '*',
    '* @since  1.0.0',
    '*/',
    'function better_rest_api_featured_images_init() {',
    '   $post_types = get_post_types( array( "public" => true ), "objects" );',
    '   foreach ( $post_types as $post_type ) {',
    '       $post_type_name     = $post_type->name;',
    '       $show_in_rest       = ( isset( $post_type->show_in_rest ) && $post_type->show_in_rest ) ? true : false;',
    '       $supports_thumbnail = post_type_supports( $post_type_name, "thumbnail" );',
    '       // Only proceed if the post type is set to be accessible over the REST API',
    '       // and supports featured images.',
    '       if ( $show_in_rest && $supports_thumbnail ) {',
    '           // Compatibility with the REST API v2 beta 9+',
    '           if ( function_exists( "register_rest_field" ) ) {',
    '              register_rest_field( $post_type_name,',
    '                   "post_images",',
    '                   array(',
    '                       "get_callback" => "better_rest_api_featured_images_get_field",',
    '                       "schema"       => null,',
    '                   )',
    '               );',
    '           } elseif ( function_exists( "register_api_field" ) ) {',
    '               register_api_field( $post_type_name,',
    '                   "post_images",',
    '                   array(',
    '                       "get_callback" => "better_rest_api_featured_images_get_field",',
    '                       "schema"       => null,',
    '                   )',
    '               );',
    '           }',
    '       }',
    '   }',
    '}',
    '/**',
     '* Return the better_featured_image field.',
     '*',
     '* @since   1.0.0',
     '*',
     '* @return  object|0',
     '*/',
    'function better_rest_api_featured_images_get_field( $object, $field_name, $request ) {',
    '   // Only proceed if the post has a featured image.',
    '   if ( $object["featured_media"] ) {',
    '       $image_id = (int)$object["featured_media"];',
    '   } else {',
    '       return null;',
    '   }',
    '   $image = get_post( $image_id );',
    '   if ( ! $image ) {',
    '       return null;',
    '   }',
    '   // This is taken from WP_REST_Attachments_Controller::prepare_item_for_response()',
    '   $featured_image["id"]            = $image_id;',
    '   $featured_image["alt_text"]      = get_post_meta( $image_id, "_wp_attachment_image_alt", true );',
    '   $featured_image["caption"]       = $image->post_excerpt;',
    '   $featured_image["description"]   = $image->post_content;',
    '   $featured_image["media_type"]    = wp_attachment_is_image( $image_id ) ? "image" : "file";',
    '   $featured_image["media_details"] = wp_get_attachment_metadata( $image_id );',
    '   $featured_image["post"]          = ! empty( $image->post_parent ) ? (int) $image->post_parent : null;',
    '   $featured_image["source_url"]    = wp_get_attachment_url( $image_id );',
    '   if ( empty( $featured_image["media_details"] ) ) {',
    '       $featured_image["media_details"] = new stdClass;',
    '   } elseif ( ! empty( $featured_image["media_details"]["sizes"] ) ) {',
    '       $img_url_basename = wp_basename( $featured_image["source_url"] );',
    '       foreach ( $featured_image["media_details"]["sizes"] as $size => &$size_data ) {',
    '           $image_src = wp_get_attachment_image_src( $image_id, $size );',
    '           if ( ! $image_src ) {',
    '               continue;',
    '           }',
    '           $size_data["source_url"] = $image_src[0];',
    '       }',
    '   } else {',
    '       $featured_image["media_details"]["sizes"] = new stdClass;',
    '   }',
    '   return $featured_image;',
    '}',
    'endif;',
    '',
    '',
    '/**',
    '* Register an event example post type, with REST API support',
    '*',
    '* Based on example at: http://codex.wordpress.org/Function_Reference/register_post_type',
    '*/',
      'add_action( "init", "my_event_cpt" );',
      'function my_event_cpt() {',
        '$labels = array(',
            '"name"               => _x( "events", "post type general name", "'+txtdmn+'" ),',
            '"singular_name"      => _x( "event", "post type singular name", "'+txtdmn+'" ),',
            '"menu_name"          => _x( "events", "admin menu", "'+txtdmn+'" ),',
            '"name_admin_bar"     => _x( "event", "add new on admin bar", "'+txtdmn+'" ),',
            '"add_new"            => _x( "Add New", "event", "'+txtdmn+'" ),',
            '"add_new_item"       => __( "Add New event", "'+txtdmn+'" ),',
            '"new_item"           => __( "New event", "'+txtdmn+'" ),',
            '"edit_item"          => __( "Edit event", "'+txtdmn+'" ),',
            '"view_item"          => __( "View event", "'+txtdmn+'" ),',
            '"all_items"          => __( "All events", "'+txtdmn+'" ),',
            '"search_items"       => __( "Search events", "'+txtdmn+'" ),',
            '"parent_item_colon"  => __( "Parent events:", "'+txtdmn+'" ),',
            '"not_found"          => __( "No events found.", "'+txtdmn+'" ),',
            '"not_found_in_trash" => __( "No events found in Trash.", "'+txtdmn+'" )',
        ');',
        '$args = array(',
            '"labels"             => $labels,',
            '"description"        => __( "Description.", "'+txtdmn+'" ),',
            '"public"             => true,',
            '"publicly_queryable" => true,',
            '"show_ui"            => true,',
            '"show_in_menu"       => true,',
            '"query_var"          => true,',
            '"rewrite"            => array( "slug" => "event" ),',
            '"capability_type"    => "post",',
            '"has_archive"        => true,',
            '"hierarchical"       => false,',
            '"menu_position"      => null,',
            '"show_in_rest"       => true,',
            '"rest_base"          => "events-api",',
            '"rest_controller_class" => "WP_REST_Posts_Controller",',
            '"supports"           => array( "title", "editor", "author", "thumbnail", "excerpt", "comments" )',
        ');',
        'register_post_type( "event", $args );',
    '}',
    '',
    '',
    '/*************** CUSTOM IMAGES SIZES',
    '***************************/',
    'add_image_size("bgImage", 1440, 9999, false);',
    'add_image_size("cardImage", 768,480, true);',
    '',
    '',
    '// Include compartmentalized functions',
    'require_once '+ txtdmn.toUpperCase()+'_INC . "functions/core.php";',
    ''].join('\n');
exports.setupfuncphp = setupfuncphp ;



var setupcorephp = ['<?php',
    '/**',
    '* Enqueue scripts for front-end.',
    '*',
    '* @uses wp_enqueue_script() to load front end scripts.',
    '*',
    '*',
    '* @param bool $debug Whether to enable loading uncompressed/debugging assets. Default true.',
    '* @return void',
    '*/',
    'add_action( "wp_enqueue_scripts", "scripts");',
	'add_action( "wp_enqueue_scripts", "styles" );',
    'function scripts() {',
    '   $debug = true;',
    '   $min="";',
    '   if(!$debug){',
    '       $min =".min";',
    '   }',
    '   //    We unregister Wordpress jquery script, because we need Jquery 2.x (included in our bundle.js) for materialize to work properly.',   
    '   wp_deregister_script( "jquery" );',
                  '',
    '   wp_enqueue_script(',
    '   "'+txtdmn+'",',
    '   '+ txtdmn.toUpperCase()+'_TEMPLATE_URL . "/assets/js/'+txtdmn+'{$min}.js",',
    '   array(),',
    '   '+txtdmn.toUpperCase()+'_VERSION,',
    '   true',
    '   );',
                  '',
    '   // pass Ajax Url to script.js',
    '   wp_localize_script("'+txtdmn+'", "ajaxurl", admin_url( "admin-ajax.php" ) );',
    '}',
    '/**',
    '* Enqueue styles for front-end.',
    '*',
    '* @uses wp_enqueue_script() to load front end scripts.',
    '*',
    '* @param bool $debug Whether to enable loading uncompressed/debugging assets. Default true.',
    '* @return void',
    '*/',
    'function styles() {',
    '   $debug = true;',
    '   $min="";',
    '   if(!$debug){',
    '       $min =".min";',
    '   }',
    '   wp_enqueue_style(',
    '   "'+txtdmn+'css",',
    '   '+ txtdmn.toUpperCase()+'_TEMPLATE_URL . "/assets/css/'+txtdmn+'{$min}.css",',
    '   array(),',
    '   '+txtdmn.toUpperCase()+'_VERSION',
    '   );',
                  '',
    '}',
    ''].join('\n');
exports.setupcorephp = setupcorephp;



var setupmainscss = ['/**',
    '* The main scss file',
    '*',
    '@package '+pkg.name,
    '@since '+pkg.version,
    '*/',
    '',
    '@import "../../../node_modules/bourbon/app/assets/stylesheets/bourbon";',
    '@import "../../../node_modules/materialize-css/sass/materialize";',
    '@import "scss/variables";',
    '@import "scss/mixins";',
    '@import "scss/global";',
    '']
    .join('\n');
exports.setupmainscss = setupmainscss;



var setupmainjs = ['/**',
    '* The main js file',
    '*',
    '@package '+pkg.name,
    '@since '+pkg.version,
    '*/',
    '',
    'var $ = require("jquery");',
    'require("materialize");',
    'var Vue = require("vue");',
    'window.Vue = Vue;',
    'VueResource = require("vue-resource");',
    'VueRouter = require("vue-router");',
    'Vue.use(VueResource);',
    'Vue.use(VueRouter);',
    '',
    '',
    '',
    '//get our vue components',
    'var postsPage = require("../../vues/page-home.vue");',
    '// configure router',
    'var router = new VueRouter({',
    '    history:true,',
    '    root: "/wordpress",',
    '    hashbang :false',
    '});',
    '',
    '',
    'router.map({',
    '   "/": {',
    '        component: postsPage,',
    '    },',
    '});',
    '',
    '',
    '$(document).ready(function(){',
    '   // start app',
    '   var App = Vue.extend({});',
    '   router.start(App, "html");',
    '});',
    .join('\n');
exports.setupmainjs = setupmainjs;
    '']