<?php

$dir = dirname(__FILE__);
$fixtures = $dir . '/../fixtures';
require $dir . '/../../lib/php/model/sfFilesystem.class.php';
require $dir . '/../../lib/php/model/iFilemanager.class.php';
require $dir . '/../../lib/php/model/image.class.php';
$fs = new iFilesystem();
$fm = iFilemanager::create()->setFilesystem($fs);
$fs->mkdirs($fixtures . '/filemanager');
$files = $fs->readDir($fixtures . '/image', 'r');
$fm->import($files)->paste($fixtures . '/filemanager', array('override' => true, 'chmod' => 0644, 'image' => array('width' => 300, 'height' => 200, 'type' => 'height', 'crop' => 'left-top'), 'thumbs' => array('tmb_' => array('width' => 100, 'height' => 100, 'type' => 'height', 'crop' => 'left-top'))))->clear();
logSection(is_dir($fixtures . '/filemanager'), 'paste all files from fixtures to fixtures/filemanager, make thumbs for images');
$fm->import($fs->readDir($fixtures . '/filemanager', 'r'))->compress($fixtures . '/filemanager/copressed.zip');
logSection(is_file($fixtures . '/filemanager/copressed.zip'), 'Compress dir fixtures/filemanger');
echo "remove created files? (yes/no)\n";
$result = fgets(STDIN);
$result = trim($result);
if (is_dir($fixtures . '/filemanager') && $result == 'yes') {
    $fm->exec(array($fs, 'remove'));
    logSection(!is_file($fixtures . '/filemanager/copressed.zip'), 'remove files through exec method');
}
$image->mark($waterMark, array('x' => 500, 'y' => 500))->save($file);
$images[] = $file;
logSection(file_exists($file), 'is created file exists', 'mark');
$data = $image->getPathInfo('/тестовый/путь/к.jpg');
logSection(is_array($data), 'is array result', 'pathinfo');
foreach ($data as $k => &$part) {
    logSection($part, '"' . $k . '"', 'pathinfo');
}
@mkdir($fixtures . '/mcr', 0777);
$thumbs = $image->createThumbs($fixtures . '/FordMustang.jpg', array('mcr/' => array('width' => 50, 'height' => 50, 'type' => 'width', 'crop' => 'left-top'), 'thumb_' => array('width' => 100, 'height' => 100, 'type' => 'height', 'crop' => 'left-top')));
$images = array_merge($thumbs, $images);
foreach ($thumbs as $tmb) {
    logSection(file_exists($tmb), 'is file exists', 'thumbs');
}
try {
    $file = $fixtures . '/FordMustang.rotate90.jpg';
    $oldWidth = $image->getData('width');
    $oldHeight = $image->getData('height');
    $image->rotate(90)->save($file);
    logSection($oldWidth == $image->getData('height') && $oldHeight == $image->getData('width'), 'is rotated', 'rotate');
    $images[] = $file;
} catch (Exception $e) {
    logSection(false, $e->getMessage(), 'error');
}
$file = $fixtures . '/FordMustang.polygonMask.jpg';
$w = $image->getData('width');
$h = $image->getData('height');
$image->mask('polygon', array(0, 100, 100, 0, $w - 100, 0, $w, 100, $w, $h - 100, $w - 100, $h, 100, $h, 0, $h - 100), 100, array(0, 255, 0))->save($file);
$images[] = $image->getSavedName();
logSection(file_exists($file), 'image was masked with #0f0 color');
removeCreated($images);
$count = count($dir);
logSection($count, '(recursive) number of items in fixtures dir = ' . $count);
$dir = $fs->readDir($fixtures, 'r', array('flags' => 0));
$count = count($dir);
logSection($count, '(recursive) number of files in fixtures dir = ' . $count);
$dir = $fs->readDir($fixtures, 'r', array('skip' => '/jpe?g$/i'));
$count = count($dir);
logSection($count, '(skip jpeg) number of items in fixtures dir = ' . $count);
$result = $fs->stat($file);
logSection(is_array($result), 'stat for file "' . basename($file) . '"');
$result = $fs->dynamicFileName($file);
logSection(basename($file), sprintf('dynamic file name "%s"', basename($result)));
$file = $fixtures . '/test_file_(3)';
$fs->touch($file);
$result = $fs->dynamicFileName($file);
logSection(basename($file), sprintf('dynamic file name "%s"', basename($result)));
$files[] = $file;
$fs->mirror($fixtures, $fixtures . '/mirror', array('image' => array('width' => 300, 'height' => 300, 'type' => 'width', 'crop' => 'left-top')));
logSection(is_dir($fixtures . '/mirror'), 'mirror fixtures directory');
echo "remove created dir? (yes/no)\n";
$result = fgets(STDIN);
$result = trim($result);
if (is_dir($fixtures . '/mirror') && $result == 'yes') {
    $fs->remove($fs->readDir($fixtures . '/mirror', 'r', array('sort' => true)));
}
$pathinfo = '/бла/путь/к/файлу.бах';
logSection(true, 'Check path info for utf8 characters');
foreach ($fs->getPathInfo($pathinfo) as $k => $part) {
    logSection($k, $part);
}
removeCreated($files);