<project name="x-slice-impl">

    <!--
    Uses the compiler to generate a special theme-only scss file containing
    rules for all framework / package / app components.  This is then used
    by the slicer example page to capture theme sprites
    -->
    <target name="-compile-slicer-sass" depends="-init-compiler">
        <local name="package.example.scss.tmp"/>
        <property name="package.example.scss.tmp" value="${package.example.scss}.tmp"/>
        <x-normalize-path
                path="${build.out.resources.dir}"
                property="image.search.path"/>

        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
                restore
                    package-${package.name}-all
                and
                include
                    -all
                and
                sass
                    +class-name-vars
                    +etc
                    +vars
                    +rules
                    -variable=$image-search-path:'${image.search.path}'
                    -variable=$theme-name: '${app.theme}' !default
                    -output=${package.example.scss.tmp}
                and
                restore
                    package-${package.name}-all
                and
                sass
                    +ruby
                    -output=${package.example.out.ruby}
            ]]>
        </x-compile>

        <if>
            <not>
                <filesmatch file1="${package.example.scss.tmp}" file2="${package.example.scss}"/>
            </not>
            <then>
                <copy file="${package.example.scss.tmp}" tofile="${package.example.scss}" overwrite="true"/>
            </then>
        </if>

    </target>

    <!--
    Compiles the scss file for the theme slicer page
    -->
    <target name="-compass-compile-slicer-css" depends="-compile-slicer-sass">
        <x-compass-compile
                dir="${package.example.build.dir}"
                trace="${compass.compile.trace}"
                boring="${compass.compile.boring}"
                force="${compass.compile.force}"
                sassdir="${package.example.build.dir}"
                cssdir="${package.example.build.dir}"
                config="${package.example.compass.config}"/>
    </target>


    <target name="-generate-slicer-manifest" depends="-compass-compile-slicer-css" if="framework.isV5">
        <x-compile refid="${compiler.ref.id}">
            <![CDATA[
                slicer-manifest
                    -cssFile=${package.example.css}
                    -basePath=${bootstrap.base.path}
                    -out=${bootstrap.example.json}
                and
                microload
                    -operation=microloader
                    -microloaderPath=${build.slicer.microloader.file}
                    -bootPath=${build.boot.file}
                    -out=${bootstrap.example.js}
            ]]>
        </x-compile>
    </target>
 
    <target name="-generate-slicer-bootstrap" depends="-init-compiler" unless="framework.isV5">

        <local name="relpath"/>
        <x-get-relative-path from="${bootstrap.base.path}"
                             to="${framework.packages.dir}"
                             property="relpath"/>
    
        <local name="override.tpl"/>
        <local name="override.tpl.type"/>

        <property name="override.tpl.type" value="tpl"/>
        <property name="override.tpl" value='Ext.Loader.loadScriptFile("{0}", Ext.emptyFn);'/>
        
        <x-bootstrap file="${bootstrap.example.js}"
                     basedir="${bootstrap.base.path}"
                     includeBoot="true"
                     includeCoreFiles="true"
                     overrideTpl="${override.tpl}"
                     overrideTplType="${override.tpl.type}"
                     overrideExcludeTags="">
<![CDATA[
Ext.Boot.loadSync([
    "render.js",
    "${relpath}/theme-base/sass/example/manifest.js",
    "${relpath}/theme-base/sass/example/shortcuts.js",
    "custom.js"
]);
]]>
        </x-bootstrap>
    </target>
    
    <target name="-update-slicer-css">
        <x-get-relative-path
            from="${package.example.dir}"
            to="${package.example.css}"
            property="package.example.css.path"
            />
        
<!--update the app's example to point to the build output-->
<echo file="${package.example.dir}/example.css">
/*
 * This file is generated by Sencha Cmd and should NOT be edited. It redirects
 * to the most recently built CSS file for the application to allow theme.html
 * to load properly for image slicing (required to support non-CSS3 browsers
 * such as IE9 and below).
 */
@import '${package.example.css.path}';
</echo>
    </target>
 
 
    <target name="-capture-theme-image"
            depends="-generate-slicer-bootstrap,-generate-slicer-manifest,-update-slicer-css">
        <echo>Capture theme image to ${build.dir}/theme-capture.png</echo>
        <x-sencha-command dir="${package.dir}">
            <![CDATA[
            theme
                capture
                    -page=${package.example.theme.html}
                    -image=${build.capture.png}
                    -manifest=${build.capture.json}
            ]]>
        </x-sencha-command>
    </target>
    
    <target name="-slice-theme-images" depends="-capture-theme-image">
        <echo>Slicing theme images to ${build.resources.dir}</echo>
        <x-sencha-command dir="${package.dir}">
            <![CDATA[
            fs
                slice
                    ${build.slice.options}
                    -image=${build.capture.png}
                    -manifest=${build.capture.json}
                    -out=${build.resources.dir}
            ]]>
        </x-sencha-command>
    </target>
 
    <target name="-before-slice"/>
    <target name="-after-slice"/>
    <target name="-slice" 
            depends="-slice-theme-images"/>

</project>