#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2014 uralbash <root@uralbash.ru>
#
# Distributed under terms of the MIT license.

"""
Assets
"""
import os
from shutil import copyfile

from webassets import Bundle


def webassets_init(config):
    curdir = os.path.dirname(os.path.abspath(__file__))
    settings = config.registry.settings
    settings["webassets.base_dir"] = os.path.join(curdir, 'static')
    settings["webassets.base_url"] = "/%s/sa_static" % config.route_prefix
    settings["webassets.debug"] = "True"
    settings["webassets.updater"] = "timestamp"
    settings["webassets.jst_compiler"] = "Handlebars.compile"
    settings["webassets.url_expire"] = "False"
    settings["webassets.static_view"] = "True"
    settings["webassets.cache_max_age"] = 3600

    config.include('pyramid_webassets')

    config.add_jinja2_extension('webassets.ext.jinja2.AssetsExtension')
    assets_env = config.get_webassets_env()
    jinja2_env = config.get_jinja2_environment()
    jinja2_env.assets_environment = assets_env


def add_css_assets(config):
    settings = config.registry.settings
    css_file = os.path.join(
        settings["webassets.base_dir"], 'css', '__main.css')
    css_bundle = Bundle(
        'css/*.css',
        'css/**/*.css',
        filters='cssmin', output=css_file)
    # if settings.get('sacrud.debug_css', False):
    #     css_bundle = Bundle(css_bundle,                   # pragma: no cover
    #                         Bundle(
    #                             'css/*.css',
    #                             'css/**/*.css',
    #                             filters=['cssmin'], output=css_file),
    #                         )
    config.add_webasset('sa_css', css_bundle)


def add_js_assets(config):                                  # pragma: no cover
    settings = config.registry.settings
    if not settings.get('sacrud.debug_js', False):
        return
    js_folder = os.path.join(settings["webassets.base_dir"], 'js')

    bower = ["jquery/dist/jquery.min.js",
             "chosen/public/chosen.jquery.min.js",
             "jquery-ui/ui/minified/jquery-ui.min.js",
             "speakingurl/speakingurl.min.js",
             "jqueryui-timepicker-addon/src/jquery-ui-timepicker-addon.js",
             "requirejs/require.js",
             "elfinder/src/elfinder/js/elfinder.js",
             "jquery-maskedinput/dist/jquery.maskedinput.min.js",
             "modernizr/modernizr.js",
             "pickadate/lib/compressed/picker.js",
             "pickadate/lib/compressed/picker.date.js",
             "pickadate/lib/compressed/picker.time.js",
             ]
    for f in bower:
        src = os.path.join(js_folder, 'bower_components', f)
        dst = os.path.join(js_folder, 'lib', f.split('/')[-1])
        copyfile(src, dst)


def includeme(config):
    config.include(webassets_init)
    config.include(add_css_assets)
    config.include(add_js_assets)

@@ -1220,123 +1220,8 @@
 %0A   
- css_bundle = Bundle(%0A        'css/*.css',%0A        'css/**/*.css',%0A        filters='cssmin', output=css_file)%0A    #
  if 
@@ -1260,26 +1260,24 @@
  False):%0A   
- #
      css_bun
@@ -1293,19 +1293,20 @@
 dle(
-css_bundle,
+            
     
@@ -1312,32 +1312,33 @@
                 
+ 
 # pragma: no cov
@@ -1344,68 +1344,8 @@
 ver%0A
-    #                         Bundle(%0A    #                 
     
@@ -1365,38 +1365,16 @@
 *.css',%0A
-    #                 
         
@@ -1393,38 +1393,16 @@
 *.css',%0A
-    #                 
         
@@ -1417,17 +1417,16 @@
 ers=
-%5B
 'cssmin'
 %5D, o
@@ -1421,17 +1421,16 @@
 'cssmin'
-%5D
 , output
@@ -1443,41 +1443,8 @@
 ile)
-,%0A    #                         )
 %0A   

fix assets