<?php

$template_name = "TEST";
$template_code = "TEST";
$template_layout = "layout_4";

/**
 * Creating the template_category
 **/
$categories = array("Health & Beauty");

foreach($categories as $category_name) {
    $category_data = array();
    $category_data['name'] = $category_name;
    $category_data['code'] = preg_replace('/[&\s]+/', "_", strtolower($category_name));

    $category = new Template_Model_Category();

    $category->find($category_data['code'], "code");

    if(!$category->getId()) {
        $category->setData($category_data)
            ->save()
        ;
    }
}

/**
 * Creating the template_design
 **/
// Listing layouts
$layout = new Application_Model_Layout_Homepage();
$layout->find($template_layout, "code");

$design_codes = array(
    "layout_id" => $layout->getId(),
    "name" => $template_name,
    "overview" => "/$template_code/overview.png",
    "background_image" => "/../../images/templates/$template_code/640x1136.jpg",
    "background_image_hd" => "/../../images/templates/$template_code/1242x2208.jpg",
    "background_image_tablet" => "/../../images/templates/$template_code/1536x2048.jpg",
    "icon" => "/../../images/templates/$template_code/180x180.png",
    "startup_image" => "/../../images/templates/$template_code/640x960.png",
    "startup_image_retina" => "/../../images/templates/$template_code/640x1136.jpg",
    "startup_image_iphone_6" => "/../../images/templates/$template_code/750x1334.png",
    "startup_image_iphone_6_plus" => "/../../images/templates/$template_code/1242x2208.jpg",
    "startup_image_ipad_retina" => "/../../images/templates/$template_code/1536x2048.jpg",
);

$design = new Template_Model_Design();
$design->find($template_code, "code");
$design->setData($design_codes)
    ->setCode($template_code)
    ->save()
;

/**
 * Adding the template_design_block
 **/
$design_blocks = array(
    "header" => array(
        "color" => "#ffffff",
        "background_color" => "#30a6d8"
    ),
    "subheader" => array(
        "color" => "#30a6d8",
        "background_color" => "#f5f3f2"
    ),
    "connect_button" => array(
        "color" => "#0a0a0a",
        "background_color" => "#30a6d8"
    ),
    "background" => array(
        "color" => "#30a6d8",
        "background_color" => "#f5f3f2"
    ),
    "discount" => array(
        "color" => "#ffffff",
        "background_color" => "#30a6d8"
    ),
    "button" => array(
        "color" => "#ffffff",
        "background_color" => "#30a6d8"
    ),
    "news" => array(
        "color" => "#30a6d8",
        "background_color" => "#ffffff"
    ),
    "comments" => array(
        "color" => "#0a0a0a",
        "background_color" => "#f5f3f2"
    ),
    "tabbar" => array(
        "color" => "#f5f3f2",
        "background_color" => "#f5f3f2",
        "image_color" => "#30a6d8"
    )
);

// Listings blocks
$block_ids = array();
$block = new Template_Model_Block();
foreach($block->findAll(array("type_id = ?" => 1)) as $block) {
    $block_ids[$block->getCode()] = $block->getId();
}

foreach($design_blocks as $block_code => $block_data) {
    $design_block = new Template_Model_Design_Block();
    $design_block->find(array("block_id" => $block_ids[$block_code], "design_id" => $design->getId()));

    if($design_block->getId()) {
        $design_block->addData($block_data)
            ->save()
        ;
    } else {
        $block_data["design_id"] = $design->getId();
        $block_data["block_id"] = $block_ids[$block_code];
        $this->_db->insert("template_design_block", $block_data);
    }
}

//------------------------------------------------------------------------------------------------------------
// Inserting designs with blocks
$ionic_design_blocks = array(
    "header" => array(
        "color" => "#f5f3f2",
        "background_color" => "#30a6d8"
    ),
    "background" => array(
        "background_color" => "#f5f3f2"
    ),
    "homepage" => array(
        "color" => "#30a6d8",
        "background_color" => "#f5f3f2",
        "border_color" => "#f5f3f2",
        "image_color" => "#30a6d8"
    ),
    "list_item_divider" => array(
        "color" => "#545454",
        "background_color" => "#f5f3f2"
    ),
    "list_item" => array(
        "color" => "#f5f3f2",
        "background_color" => "#30a6d8"
    ),
    "card_item_divider" => array(
        "color" => "#f5f3f2",
        "background_color" => "#30a6d8"
    ),
    "card_item" => array(
        "color" => "#30a6d8",
        "background_color" => "#ffffff"
    ),
    "buttons" => array(
        "color" => "#f5f3f2",
        "background_color" => "#30a6d8",
        "border_color" => "#b2b2b2"
    ),
    "checkbox_on" => array(
        "color" => "#f5f3f2",
        "background_color" => "#30a6d8"
    ),
    "checkbox_off" => array(
        "background_color" => "#ffffff"
    ),
    "checkbox_general" => array(
        "color" => "#30a6d8",
        "background_color" => "#ffffff"
    ),
    "radio" => array(
        "color" => "#30a6d8",
        "background_color" => "#ffffff"
    ),
    "toggle_on" => array(
        "background_color" => "#30a6d8"
    ),
    "toggle_general" => array(
        "color" => "#30a6d8",
        "background_color" => "#ffffff"
    ),
    "toggle_off" => array(
        "background_color" => "#ffffff",
        "border_color" => "#e6e6e6"
    ),
    "toggle_handle_on" => array(
        "background_color" => "#66caf2"
    ),
    "toggle_handle_off" => array(
        "background_color" => "#ffffff"
    ),
    "tooltip" => array(
        "color" => "#f5f3f2",
        "background_color" => "#30a6d8"
    ),
    "spinner_ios_text" => array(
        "background_color" => "#30a6d8"
    ),
    "spinner_android_text" => array(
        "background_color" => "#30a6d8"
    )
);

// Listings blocks
$block_ids = array();
$block = new Template_Model_Block();
foreach($block->findAll(array("type_id = ?" => 3)) as $block) {
    $block_ids[$block->getCode()] = $block->getId();
    foreach($block->getChildren() as $child) {
        $block_ids[$child->getCode()] = $child->getId();
    }
}

foreach($ionic_design_blocks as $block_code => $block_data) {
    $design_block = new Template_Model_Design_Block();
    $design_block->find(array("block_id" => $block_ids[$block_code], "design_id" => $design->getId()));

    if($design_block->getId()) {
        $design_block->addData($block_data)
            ->save()
        ;
    } else {
        $block_data["design_id"] = $design->getId();
        $block_data["block_id"] = $block_ids[$block_code];
        $this->_db->insert("template_design_block", $block_data);
    }
}

//------------------------------------------------------------------------------------------------------------
/** Linking categories to the design **/
$categories_designs = array(
    "health_beauty" => $template_code
);

// Listing design ids
$design_ids = array();
$designs = new Template_Model_Design();
foreach ($designs->findAll() as $design_data) {
    $design_ids[$design_data->getCode()] = $design_data->getId();
}

// Listing category ids
$category_ids = array();
$category = new Template_Model_Category();
foreach ($category->findAll() as $category_data) {
    $category_ids[$category_data->getCode()] = $category_data->getId();
}

foreach ($categories_designs as $category_code => $design_code) {
    $category_design = new Template_Model_Design_Category();
    $category_design->find(array("category_id" => $category_ids[$category_code], "design_id" => $design_ids[$design_code]));

    if(!$category_design->getId()) {
        $categories_designs_data = array(
            "category_id" => $category_ids[$category_code],
            "design_id" => $design_ids[$design_code]
        );

        $this->_db->insert("template_design_category", $categories_designs_data);
    }
}


