/*
 * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.micode.notes.gtask.data;

import android.database.Cursor;
import android.text.TextUtils;
import android.util.Log;

import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.DataColumns;
import net.micode.notes.data.Notes.DataConstants;
import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.gtask.exception.ActionFailureException;
import net.micode.notes.tool.GTaskStringUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/*
 * Task���ʾһ�����񣬼̳���Node�ࡣ
 * �������������ĸ������ԺͲ����������紴�������¡��������ݵȡ�
 */
public class Task extends Node {
    private static final String TAG = Task.class.getSimpleName();   

    private boolean mCompleted;  // �����Ƿ������

    private String mNotes;   // ����ı�ע

    private JSONObject mMetaInfo;   // �����Ԫ������Ϣ

    private Task mPriorSibling;  // ǰһ���ֵ�����

    private TaskList mParent;   // �������б�
/*
 * ���캯������ʼ���������
 * ��ʼ����������״̬����ע��ǰһ���ֵ����񡢸������б���Ԫ������Ϣ��
 */
    public Task() {
        super();  // ���ø���Node�Ĺ��캯��
        mCompleted = false;  // ��ʼ������Ϊδ���״̬
        mNotes = null;  // ��ʼ����עΪ��
        mPriorSibling = null;   // ��ʼ��ǰһ���ֵ�����Ϊ��
        mParent = null;  // ��ʼ���������б�Ϊ��
        mMetaInfo = null;  // ��ʼ��Ԫ������ϢΪ��
    }
/*
 *���ɴ��������JSON����
 * @param actionId ����ID
 * @return ��������������Ϣ��JSONObject
 */
    public JSONObject getCreateAction(int actionId) {
        JSONObject js = new JSONObject();

        try {
            // action_type
        	// ���ò�������Ϊ����
            js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
                    GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE);

            // action_id
            // ����ID
            js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);

            // index
            // ���������ڸ������б��е�����
            js.put(GTaskStringUtils.GTASK_JSON_INDEX, mParent.getChildTaskIndex(this));

            // entity_delta
            // ���������ʵ����Ϣ
            JSONObject entity = new JSONObject();
            entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());
            entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null");
            entity.put(GTaskStringUtils.GTASK_JSON_ENTITY_TYPE,
                    GTaskStringUtils.GTASK_JSON_TYPE_TASK);
            if (getNotes() != null) {
                entity.put(GTaskStringUtils.GTASK_JSON_NOTES, getNotes());
            }
            js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);

            // parent_id
            // ���ø������б���ID
            js.put(GTaskStringUtils.GTASK_JSON_PARENT_ID, mParent.getGid());

            // dest_parent_type
            // ����Ŀ�길����
            js.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT_TYPE,
                    GTaskStringUtils.GTASK_JSON_TYPE_GROUP);

            // list_id
            // �����������ID
            js.put(GTaskStringUtils.GTASK_JSON_LIST_ID, mParent.getGid());

            // prior_sibling_id
            // ����ǰһ���ֵ������ID
            if (mPriorSibling != null) {
                js.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, mPriorSibling.getGid());
            }

        } catch (JSONException e) {
        	// ����JSON�쳣����¼������־
            Log.e(TAG, e.toString());
            e.printStackTrace();
            // �׳��Զ����쳣����ʾ����JSON����ʧ��
            throw new ActionFailureException("fail to generate task-create jsonobject");
        }

        return js;  // ���ذ�������������Ϣ��JSONObject
    }
/*
 *  ���ɸ��������JSON����
 * @param actionId ����ID
 * @return ��������������Ϣ��JSONObject
 */
    public JSONObject getUpdateAction(int actionId) {
        JSONObject js = new JSONObject();

        try {
            // action_type
        	// ���ò�������Ϊ����
            js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
                    GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE);

            // action_id
            // ����ID
            js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);

            // id
            // ���������ID
            js.put(GTaskStringUtils.GTASK_JSON_ID, getGid());

            // entity_delta
            // ���������ʵ����Ϣ
            JSONObject entity = new JSONObject();
            entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());
            if (getNotes() != null) {
                entity.put(GTaskStringUtils.GTASK_JSON_NOTES, getNotes());
            }
            entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted());
            js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);

        } catch (JSONException e) {
        	// ����JSON�쳣����¼������־
            Log.e(TAG, e.toString());
            e.printStackTrace();
            // �׳��Զ����쳣����ʾ����JSON����ʧ��
            throw new ActionFailureException("fail to generate task-update jsonobject");
        }

        return js;  // ���ذ�������������Ϣ��JSONObject
    }
/*
 * ����Զ��JSON����������������ݡ�
 * @param js ����������Ϣ��Զ��JSON����
 */
    public void setContentByRemoteJSON(JSONObject js) {
        if (js != null) {
            try {
                // id
            	// ���JSON�����а���ID�ֶΣ������������ID
                if (js.has(GTaskStringUtils.GTASK_JSON_ID)) {
                    setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID));
                }

                // last_modified
                // ���JSON�����а�������޸�ʱ���ֶΣ����������������޸�ʱ��
                if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) {
                    setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED));
                }

                // name
                // ���JSON�����а��������ֶΣ����������������
                if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) {
                    setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME));
                }

                // notes
                // ���JSON�����а�����ע�ֶΣ�����������ı�ע
                if (js.has(GTaskStringUtils.GTASK_JSON_NOTES)) {
                    setNotes(js.getString(GTaskStringUtils.GTASK_JSON_NOTES));
                }

                // deleted
                // ���JSON�����а���ɾ��״̬�ֶΣ������������ɾ��״̬
                if (js.has(GTaskStringUtils.GTASK_JSON_DELETED)) {
                    setDeleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_DELETED));
                }

                // completed
                // ���JSON�����а������״̬�ֶΣ���������������״̬
                if (js.has(GTaskStringUtils.GTASK_JSON_COMPLETED)) {
                    setCompleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_COMPLETED));
                }
            } catch (JSONException e) {
            	 // ����JSON�쳣����¼������־
                Log.e(TAG, e.toString());
                e.printStackTrace();
                // �׳��Զ����쳣����ʾ��JSON�����ȡ��������ʧ��
                throw new ActionFailureException("fail to get task content from jsonobject");
            }
        }
    }
/*
 *  ���ݱ���JSON����������������ݡ�
 * @param js ����������Ϣ�ı���JSON����
 */
    public void setContentByLocalJSON(JSONObject js) {
    	// ���JSON�����Ƿ�Ϊ�ջ��Ƿ������Ҫ���ֶ�
        if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)
                || !js.has(GTaskStringUtils.META_HEAD_DATA)) {
            Log.w(TAG, "setContentByLocalJSON: nothing is avaiable");
        }

        try {
        	// ��ȡ�ʼǵ�Ԫ����
            JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
            // ��ȡ��������
            JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
            // ���ʼǵ������Ƿ�Ϊ��ͨ�ʼ�����
            if (note.getInt(NoteColumns.TYPE) != Notes.TYPE_NOTE) {
                Log.e(TAG, "invalid type");
                return;
            }
            // �����������飬����MIME����ΪDataConstants.NOTE������
            for (int i = 0; i < dataArray.length(); i++) {
                JSONObject data = dataArray.getJSONObject(i);
                if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) {
                	// �������������
                	setName(data.getString(DataColumns.CONTENT));
                    break;
                }
            }

        } catch (JSONException e) {
        	 // ����JSON�쳣����¼������־
            Log.e(TAG, e.toString());
            e.printStackTrace();
        }
    }
/*
 *  ���������������ɱ���JSON����
 * @return �����������ݵı���JSON����������������򷵻�null
 */
    public JSONObject getLocalJSONFromContent() {
        String name = getName();
        try {
            if (mMetaInfo == null) {
                // new task created from web
            	// ����Ǵ���ҳ������������
                if (name == null) {
                    Log.w(TAG, "the note seems to be an empty one");
                    return null;
                }
                // ����һ���µ�JSON����
                JSONObject js = new JSONObject();
                JSONObject note = new JSONObject();
                JSONArray dataArray = new JSONArray();
                JSONObject data = new JSONObject();
                data.put(DataColumns.CONTENT, name);
                dataArray.put(data);
                js.put(GTaskStringUtils.META_HEAD_DATA, dataArray);
                note.put(NoteColumns.TYPE, Notes.TYPE_NOTE);
                js.put(GTaskStringUtils.META_HEAD_NOTE, note);
                return js;
            } else {
                // synced task
            	// �������ͬ��������
                JSONObject note = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
                JSONArray dataArray = mMetaInfo.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
                // �����������飬������������
                for (int i = 0; i < dataArray.length(); i++) {
                    JSONObject data = dataArray.getJSONObject(i);
                    if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) {
                        data.put(DataColumns.CONTENT, getName());
                        break;
                    }
                }

                note.put(NoteColumns.TYPE, Notes.TYPE_NOTE);
                return mMetaInfo;
            }
        } catch (JSONException e) {
        	// ����JSON�쳣����¼������־
            Log.e(TAG, e.toString());
            e.printStackTrace();
            return null;
        }
    }
/*
 * ���������Ԫ������Ϣ��
 * @param metaData ����Ԫ������Ϣ��MetaData����
 */
    public void setMetaInfo(MetaData metaData) {
    	 // ���MetaData�����Ƿ�Ϊ�գ������Ƿ�����ʼ���Ϣ
        if (metaData != null && metaData.getNotes() != null) {
            try {
            	// ���ʼ���Ϣת��ΪJSONObject����
                mMetaInfo = new JSONObject(metaData.getNotes());
            } catch (JSONException e) {
                // ����JSON�쳣����¼������־
                Log.w(TAG, e.toString());
                mMetaInfo = null;
                // ���ת��ʧ�ܣ���mMetaInfo����Ϊnull
            }
        }
    }
/*
 * ��ȡͬ���������͡�
 * @param c �������رʼ���Ϣ��Cursor����
 * @return ͬ����������
 */
    public int getSyncAction(Cursor c) {
        try {
            JSONObject noteInfo = null;
            // ���Ԫ������Ϣ��Ϊ���Ұ����ʼ���Ϣ�����ȡ�ʼ���Ϣ
            if (mMetaInfo != null && mMetaInfo.has(GTaskStringUtils.META_HEAD_NOTE)) {
                noteInfo = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
            }
            // ����ʼ���ϢΪ�գ���ʾ�ʼ�Ԫ�����ѱ�ɾ��
            if (noteInfo == null) {
                Log.w(TAG, "it seems that note meta has been deleted");
                return SYNC_ACTION_UPDATE_REMOTE;
            }
            // ����ʼ���Ϣ�в�����ID�ֶΣ���ʾԶ�̱ʼ�ID�ѱ�ɾ��
            if (!noteInfo.has(NoteColumns.ID)) {
                Log.w(TAG, "remote note id seems to be deleted");
                return SYNC_ACTION_UPDATE_LOCAL;
            }

            // validate the note id now
            // ��֤�ʼ�ID�Ƿ�ƥ��
            if (c.getLong(SqlNote.ID_COLUMN) != noteInfo.getLong(NoteColumns.ID)) {
                Log.w(TAG, "note id doesn't match");
                return SYNC_ACTION_UPDATE_LOCAL;
            }

            if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) {
                // there is no local update
                // �������û���޸�
                if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) {
                    // no update both side
                	// ���û�и��£�����SYNC_ACTION_NONE
                    return SYNC_ACTION_NONE;
                } else {
                    // apply remote to local
                	// Ӧ��Զ�̸��µ�����
                    return SYNC_ACTION_UPDATE_LOCAL;
                }
            } else {
                // validate gtask id
            	// ��֤Google����ID�Ƿ�ƥ��
                if (!c.getString(SqlNote.GTASK_ID_COLUMN).equals(getGid())) {
                    Log.e(TAG, "gtask id doesn't match");
                    return SYNC_ACTION_ERROR;
                }
                // ���ͬ��ID������޸�ʱ��ƥ�䣬��ʾֻ�б����޸�
                if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) {
                    // local modification only
                    return SYNC_ACTION_UPDATE_REMOTE;
                } else {
                	// ���س�ͻ����
                    return SYNC_ACTION_UPDATE_CONFLICT;
                }
            }
        } catch (Exception e) {
        	// �����쳣����¼������־
            Log.e(TAG, e.toString());
            e.printStackTrace();
        }

        return SYNC_ACTION_ERROR;  // Ĭ�Ϸ���SYNC_ACTION_ERROR
    }
/*
 *  �ж������Ƿ�ֵ�ñ��档
 * @return �������ֵ�ñ��棬����true�����򷵻�false
 */
    public boolean isWorthSaving() {
    	// ���Ԫ������Ϣ�Ƿ���ڣ������������ƺͱ�ע�Ƿ�Ϊ���Ҳ�Ϊ�հ��ַ���
        return mMetaInfo != null || (getName() != null && getName().trim().length() > 0)
                || (getNotes() != null && getNotes().trim().length() > 0);
    }
/*
 *  ������������״̬��
 * @param completed ��������״̬��true��ʾ����ɣ�false��ʾδ���
 */
    public void setCompleted(boolean completed) {
        this.mCompleted = completed;
    }
/*
 * ��������ı�ע��
 * @param notes ����ı�ע
 */
    public void setNotes(String notes) {
        this.mNotes = notes;
    }
/*
 * ����ǰһ���ֵ�����
 * @param priorSibling ǰһ���ֵ�����
 */
    public void setPriorSibling(Task priorSibling) {
        this.mPriorSibling = priorSibling;
    }
/*
 *  ���ø������б���
 * @param parent �������б�
 */
    public void setParent(TaskList parent) {
        this.mParent = parent;
    }
/*
 * ��ȡ��������״̬��
 * @return ��������״̬��true��ʾ����ɣ�false��ʾδ���
 */
    public boolean getCompleted() {
        return this.mCompleted;
    }
/*
 * ��ȡ����ı�ע��
 * @return ����ı�ע
 */
    public String getNotes() {
        return this.mNotes;
    }
/*
 *  ��ȡǰһ���ֵ�����
 * @return ǰһ���ֵ�����
 */
    public Task getPriorSibling() {
        return this.mPriorSibling;
    }
/*
 * ��ȡ�������б���
 * @return �������б�
 */
    public TaskList getParent() {
        return this.mParent;
    }

}