/*
 * The MIT License (MIT)
 * Copyright (c) 2014 longkai
 * The software shall be used for good, not evil.
 */
package org.catnut.plugin.zhihu;

import android.content.ContentValues;
import android.provider.BaseColumns;
import org.catnut.core.CatnutMetadata;
import org.json.JSONArray;

/**
 * 知乎的一条精选回答条目
 *
 * @author longkai
 */
public class Zhihu implements CatnutMetadata<JSONArray, ContentValues> {

	public static final String TABLE = "zhihu";
	public static final String SINGLE = "zhihu";
	public static final String MULTIPLE = "zhihus";

	// 问题相关
	/** id */
	public static final String QUESTION_ID = "question_id";
	/** 标题 */
	public static final String TITLE = "title";
	/** 描述 */
	public static final String DESCRIPTION = "description";

	// 回答相关
	/** id */
	public static final String ANSWER_ID = "answer_id";
	/** 回答内容 */
	public static final String ANSWER = "answer";
	/** 答案最后更新时间戳(需要*1000) */
	public static final String LAST_ALTER_DATE = "last_alter_date";

	// 回答者相关
	/** id */
	public static final String UID = "uid";
	/** 昵称 */
	public static final String NICK = "nick";
	/** 个性描述 */
	public static final String STATUS = "status";
	/** 头像 */
	public static final String AVATAR = "avatar";

	@Override
	public String ddl() {
		StringBuilder sql = new StringBuilder();
		// 阅读项目数据表
		// _id 仅做为本地的一个标识与列表顺序比较的时间戳，实际的主键还是答案的id
		sql.append("CREATE TABLE ").append(TABLE).append("(")
				.append(BaseColumns._ID).append(" int,")
				.append(QUESTION_ID).append(" int PRIMARY KEY,")
				.append(TITLE).append(" text NOT NULL,")
				.append(DESCRIPTION).append(" text,")

				.append(ANSWER_ID).append(" int NOT NULL,")
				.append(ANSWER).append(" text,")
				.append(LAST_ALTER_DATE).append(" int,")

				.append(UID).append(" text,")
				.append(NICK).append(" text,")
				.append(STATUS).append(" text,")
				.append(AVATAR).append(" text")
				.append(")");
		return sql.toString();
	}

	@Override
	public ContentValues convert(JSONArray array) {
		long now = System.currentTimeMillis();

		ContentValues item = new ContentValues();

		// 用当前时间戳来作为item在本地的标识与排序，降序，时间戳越大代表越新
		item.put(BaseColumns._ID, now);

		// 答案相关
		item.put(STATUS, array.optString(1));
		item.put(ANSWER, array.optString(2));
		item.put(LAST_ALTER_DATE, array.optLong(4) * 1000);
		item.put(ANSWER_ID, array.optLong(5));

		// 答主相关
		JSONArray user = array.optJSONArray(6);
		if (user != null) {
			item.put(NICK, user.optString(0));
			item.put(UID, user.optString(1));
			item.put(AVATAR, user.optInt(2));
		}

		// 问题相关
		JSONArray question = array.optJSONArray(7);
		if (question != null) {
			item.put(TITLE, question.optString(1, null));
			item.put(DESCRIPTION, question.optString(2));
			item.put(QUESTION_ID, question.optLong(3));
		}

		return item;
	}
}

@@ -177,16 +177,48 @@
 Values;%0A
+import android.content.Context;%0A
 import a
@@ -285,16 +285,95 @@
 tadata;%0A
+import org.catnut.core.CatnutProcessor;%0Aimport org.catnut.core.CatnutProvider;%0A
 import o
@@ -648,24 +648,96 @@
  %22zhihus%22;%0A%0A
+%09public static final Zhihu METADATA = new Zhihu();%0A%09private Zhihu() %7B%7D%0A%0A
 %09// %E9%97%AE%E9%A2%98%E7%9B%B8%E5%85%B3%0A%09/*
@@ -2932,11 +2932,700 @@
 item;%0A%09%7D
+%0A%0A%09/**%0A%09 * %E6%AF%8F%E6%97%A5%E7%B2%BE%E9%80%89%E5%A4%84%E7%90%86%E5%99%A8%0A%09 *%0A%09 * @author longkai%0A%09 */%0A%09public static class ZhihuProcessor implements CatnutProcessor%3CJSONArray%3E %7B%0A%0A%09%09private static ZhihuProcessor processor;%0A%0A%09%09private ZhihuProcessor()%7B%7D%0A%0A%09%09public static ZhihuProcessor getProcessor() %7B%0A%09%09%09if (processor == null) %7B%0A%09%09%09%09processor = new ZhihuProcessor();%0A%09%09%09%7D%0A%09%09%09return processor;%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public void asyncProcess(Context context, JSONArray data) throws Exception %7B%0A%09%09%09ContentValues%5B%5D items = new ContentValues%5Bdata.length()%5D;%0A%09%09%09for (int i = 0; i %3C items.length; i++) %7B%0A%09%09%09%09items%5Bi%5D = METADATA.convert(data.optJSONArray(i));%0A%09%09%09%7D%0A%09%09%09context.getContentResolver().bulkInsert(CatnutProvider.parse(MULTIPLE), items);%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

add zhihu items processor.