-------------------------------------------------------------------
-- uid:  
--    unique id of app, e.g. app_gallery, app_music etc.
-- app_args: 
--    default arguments for app
-- name: 
--    name for app which is not null
-- type:
--    app type, 0: build-in, 1: page-clipping
-------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `metadata` (
	`id`           INTEGER PRIMARY KEY AUTOINCREMENT,
	`key`          VARCHAR(20) NOT NULL,
	`value`        VARCHAR(20) NOT NULL,
	UNIQUE (`key`)
);

CREATE TABLE IF NOT EXISTS `apps` (
	`id`           INTEGER PRIMARY KEY AUTOINCREMENT,
	`uid`      VARCHAR(38)   NOT NULL,
	`name`     VARCHAR(128)  NOT NULL,
	`type`         INTEGER DEFAULT 0,
	`description`  VARCHAR(1024),
	`bookmark`     INTEGER DEFAULT 0,
	`icon_url`     VARCHAR(1024) DEFAULT NULL,
	`create_time`  INTEGER,
	`app_args`     TEXT DEFAULT NULL,
	`is_local`     INTEGER DEFAULT 0,
	`position`     INTEGER DEFAULT 999,
	`icon_data_url` TEXT,
	`visits`       INTEGER DEFAULT 0,
	`small_icon`   VARCHAR(1024) DEFAULT NULL,
	UNIQUE (`uid`)
);

CREATE TABLE IF NOT EXISTS `app_info` (
	`id`       INTEGER PRIMARY KEY AUTOINCREMENT,
	`app_id`   INTEGER,
	`app_args` VARCHAR(1024) DEFAULT NULL,
	`screen_x` INTEGER DEFAULT 0,
	`screen_y` INTEGER DEFAULT 0,
	`auto_start`    INTEGER DEFAULT 0,
	`last_opened`   INTEGER,
	`last_modified` INTEGER,
	`app_status`    TEXT DEFAULT NULL,
	`thumbnail`     TEXT DEFAULT NULL,
	FOREIGN KEY (`app_id`) REFERENCES apps(`id`) ON UPDATE CASCADE ON DELETE CASCADE
);

-- Should be the same as 'extensions.livemargins@mozillaonline.com.sqlversion'
INSERT OR REPLACE INTO `metadata` (`key`, `value`) VALUES ('version', 3);

-- gallery
INSERT INTO `apps` 
	(`uid`, `name`, `app_args`, `type`, `description`, `bookmark`, `icon_url`, `small_icon`, `create_time`, `position`)
VALUES
	('app_gallery', '图片收藏', '', 0, '通过拖拽的方式将图片收藏至本地，方便快捷！', 1, 'chrome://livemargins/skin/apps/gallery/pic-icon.png', 'chrome://livemargins/skin/images/icons/gallery.png', strftime('%s','now') * 1000, 1);
-- Music
INSERT INTO `apps` 
	(`uid`, `name`, `app_args`, `type`, `description`, `bookmark`, `icon_url`, `small_icon`, `create_time`, `position`)
VALUES
	('app_music', '新浪音乐', '', 0, '新浪音乐播放盒，搜罗天下最美的音乐！还可以在火狐主页上直接点击音乐播放！', 1, 'chrome://livemargins/skin/apps/music/music-icon.png', 'chrome://livemargins/skin/images/icons/sina.ico', strftime('%s','now') * 1000, 2);
-- Radio
INSERT INTO `apps` 
	(`uid`, `name`, `app_args`, `type`, `description`, `bookmark`, `icon_url`, `small_icon`, `create_time`, `position`)
VALUES
	('app_radio', '豆瓣电台', '{"location": "http://api.app.mozilla.com.cn/center/uid/app_radio/", "width": 420, "height": 186}', 2, '豆瓣音乐电台，收听最优美的音乐', 1, 'http://api.app.mozilla.com.cn/static/images/appcenter/douban-radio.jpg', 'chrome://livemargins/skin/images/icons/douban.ico', strftime('%s','now') * 1000, 3);
-- Search
INSERT INTO `apps` 
	(`uid`, `name`, `app_args`, `type`, `description`, `bookmark`, `icon_url`, `small_icon`, `create_time`, `position`)
VALUES
	('app_search', '多重搜索', '', 0, '多重搜索，拖拽选中文字，即可得到多个垂直搜索引擎的结果！', 1, 'chrome://livemargins/skin/apps/search/search-icon.png', 'chrome://livemargins/skin/images/icons/search.png', strftime('%s','now') * 1000, 4);
-- Stock
INSERT INTO `apps` 
	(`uid`, `name`, `app_args`, `type`, `description`, `bookmark`, `icon_url`, `small_icon`, `create_time`, `position`)
VALUES
	('app_stock', '和讯财经', '{"location": "http://api.app.mozilla.com.cn/center/uid/app_stock/", "width": 297, "height": 383}', 2, '提供股票、基金等在线实时报价', 1, 'http://api.app.mozilla.com.cn/static/images/appcenter/icons/hexun/logo.png', 'chrome://livemargins/skin/images/icons/stock.png', strftime('%s','now') * 1000, 5);
-- TV
INSERT INTO `apps` 
	(`uid`, `name`, `app_args`, `type`, `description`, `bookmark`, `icon_url`, `small_icon`, `create_time`, `position`)
VALUES
	('app_tv', '东方宽频', '{"location": "http://api.app.mozilla.com.cn/center/uid/app_tv/", "width": 320, "height": 270}', 2, '在线收看电视节目', 1, 'http://api.app.mozilla.com.cn/static/images/appcenter/app-tv.png', 'chrome://livemargins/skin/images/icons/tv.png', strftime('%s','now') * 1000, 7);
-- Weather
INSERT INTO `apps` 
	(`uid`, `name`, `app_args`, `type`, `description`, `bookmark`, `icon_url`, `small_icon`, `create_time`, `position`)
VALUES
	('app_weather', '天气预报', '', 0, '实时查看本地天气', 1, 'chrome://livemargins/skin/apps/weather/weather-icon.png', 'chrome://livemargins/skin/images/icons/weather.png', strftime('%s','now') * 1000, 8);
-- Focus
INSERT INTO `apps` 
	(`uid`, `name`, `app_args`, `type`, `description`, `bookmark`, `icon_url`, `small_icon`, `create_time`, `position`)
VALUES
	('app_focus', '焦点新闻', '{"location": "http://api.app.mozilla.com.cn/center/uid/app_focus/", "width": 310, "height": 546}', 2, '焦点新闻', 1, 'http://api.app.mozilla.com.cn/static/images/appcenter/icons/ff/focus.png', 'chrome://livemargins/skin/images/icons/focus.png', strftime('%s','now') * 1000, 9);
-- Taobao
INSERT INTO `apps` 
	(`uid`, `name`, `app_args`, `type`, `description`, `bookmark`, `icon_url`, `small_icon`, `create_time`, `position`)
VALUES
	('app_shopping', '时尚消费', '{"location": "http://api.app.mozilla.com.cn/center/uid/app_shopping/", "width": 640, "height": 394}', 2, '时尚消费', 1, 'http://api.app.mozilla.com.cn/static/images/appcenter/icons/ff/shopping.png', 'chrome://livemargins/skin/images/icons/shopping.png', strftime('%s','now') * 1000, 10);
	
-- Create indexes
CREATE INDEX IF NOT EXISTS `apps_type`     ON `apps`(`type`);
CREATE INDEX IF NOT EXISTS `apps_bookmark` ON `apps`(`bookmark`);
CREATE INDEX IF NOT EXISTS `apps_position` ON `apps`(`position`);
CREATE INDEX IF NOT EXISTS `apps_visits`   ON `apps`(`visits`);

CREATE INDEX IF NOT EXISTS `app_info_auto_start`  ON `app_info`(`auto_start`);
CREATE INDEX IF NOT EXISTS `app_info_app_id`      ON `app_info`(`app_id`);
CREATE INDEX IF NOT EXISTS `app_info_last_opened` ON `app_info`(`last_opened`);
