/*
 * 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.remote;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerFuture;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import net.micode.notes.gtask.data.Node;
import net.micode.notes.gtask.data.Task;
import net.micode.notes.gtask.data.TaskList;
import net.micode.notes.gtask.exception.ActionFailureException;
import net.micode.notes.gtask.exception.NetworkFailureException;
import net.micode.notes.tool.GTaskStringUtils;
import net.micode.notes.ui.NotesPreferenceActivity;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;

/*
 *  GTaskClient����һ��������Google���������н����Ŀͻ����ࡣ
 * �����ṩ�˵�¼���������񡢴��������б������������ƶ�����ɾ������ȹ��ܡ�
 */
public class GTaskClient {
    private static final String TAG = GTaskClient.class.getSimpleName();

    private static final String GTASK_URL = "https://mail.google.com/tasks/";

    private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig";

    private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig";

    private static GTaskClient mInstance = null;

    private DefaultHttpClient mHttpClient;

    private String mGetUrl;

    private String mPostUrl;

    private long mClientVersion;

    private boolean mLoggedin;

    private long mLastLoginTime;

    private int mActionId;

    private Account mAccount;

    private JSONArray mUpdateArray;
/*
 * ˽�й��캯������ʼ����Ա������
 */
    private GTaskClient() {
        mHttpClient = null;    // ��ʼ��HttpClientΪnull
        mGetUrl = GTASK_GET_URL;    // ��ʼ��GET��POST�����URL
        mPostUrl = GTASK_POST_URL;
        mClientVersion = -1;    // ��ʼ���ͻ��˰汾��Ϊ-1
        mLoggedin = false;    // ��ʼ����¼״̬Ϊfalse
        mLastLoginTime = 0;  // ��ʼ�����һ�ε�¼ʱ��Ϊ0
        mActionId = 1;  // ��ʼ������IDΪ1
        mAccount = null;   // ��ʼ���˻�Ϊnull
        mUpdateArray = null;  // ��ʼ����������Ϊnull
    }
/*
 * ��ȡGTaskClient�ĵ���ʵ����
 * @return GTaskClient�ĵ���ʵ��
 */
    public static synchronized GTaskClient getInstance() {
    	 // ���mInstanceΪnull������һ���µ�GTaskClientʵ��
        if (mInstance == null) {
            mInstance = new GTaskClient();
        }
        // ����GTaskClient�ĵ���ʵ��
        return mInstance;
    }
/*
 *  ��¼Google�������
 * @param activity ��ǰ�
 * @return �Ƿ��¼�ɹ�
 */
    public boolean login(Activity activity) {
        // we suppose that the cookie would expire after 5 minutes
    	// ����cookie��5���Ӻ���ڣ���Ҫ���µ�¼
        // then we need to re-login
        final long interval = 1000 * 60 * 5;
        if (mLastLoginTime + interval < System.currentTimeMillis()) {
            mLoggedin = false;
        }

        // need to re-login after account switch
        // ��Ҫ���µ�¼���л��˻�
        if (mLoggedin
                && !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity
                        .getSyncAccountName(activity))) {
            mLoggedin = false;
        }
        // ����Ѿ���¼��ֱ�ӷ���true
        if (mLoggedin) {
            Log.d(TAG, "already logged in");
            return true;
        }
        // �������һ�ε�¼ʱ��
        mLastLoginTime = System.currentTimeMillis();
        // ��¼Google�˻�
        String authToken = loginGoogleAccount(activity, false);
        if (authToken == null) {
            Log.e(TAG, "login google account failed");
            return false;
        }

        // login with custom domain if necessary
        // ����˻�����gmail.com��googlemail.com������ʹ���Զ���������¼
        if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase()
                .endsWith("googlemail.com"))) {
            StringBuilder url = new StringBuilder(GTASK_URL).append("a/");
            int index = mAccount.name.indexOf('@') + 1;
            String suffix = mAccount.name.substring(index);
            url.append(suffix + "/");
            mGetUrl = url.toString() + "ig";
            mPostUrl = url.toString() + "r/ig";

            if (tryToLoginGtask(activity, authToken)) {
                mLoggedin = true;
            }
        }

        // try to login with google official url
        // ����ʹ��Google�ٷ�URL��¼
        if (!mLoggedin) {
            mGetUrl = GTASK_GET_URL;
            mPostUrl = GTASK_POST_URL;
            if (!tryToLoginGtask(activity, authToken)) {
                return false;
            }
        }
        // ��¼�ɹ�
        mLoggedin = true;
        return true;
    }
/*
 * ��¼Google�˻�����ȡ��Ȩ���ơ�
 * @param activity       ��ǰ�
 * @param invalidateToken �Ƿ�ʹ��Ȩ����ʧЧ
 * @return ��Ȩ���ƣ������¼ʧ���򷵻�null
 */
    private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
        String authToken;
        // ��ȡAccountManagerʵ��
        AccountManager accountManager = AccountManager.get(activity);
        // ��ȡ����Google�˻�
        Account[] accounts = accountManager.getAccountsByType("com.google");
        // ���û�п��õ�Google�˻�����¼������־������null
        if (accounts.length == 0) {
            Log.e(TAG, "there is no available google account");
            return null;
        }
        // ��ȡ�����е�ͬ���˻�����
        String accountName = NotesPreferenceActivity.getSyncAccountName(activity);
        Account account = null;
        // ��������Google�˻���������������ͬ���˻�����ƥ����˻�
        for (Account a : accounts) {
            if (a.name.equals(accountName)) {
                account = a;
                break;
            }
        }
        // ����ҵ�ƥ����˻������丳ֵ��mAccount
        if (account != null) {
            mAccount = account;
        } else {
        	 // ���û���ҵ�ƥ����˻�����¼������־������null
            Log.e(TAG, "unable to get an account with the same name in the settings");
            return null;
        }

        // get the token now
        // ��ȡ��Ȩ����
        AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account,
                "goanna_mobile", null, activity, null, null);
        try {
        	// ��ȡ��Ȩ���Ƶ�Bundle
            Bundle authTokenBundle = accountManagerFuture.getResult();
            // ��Bundle�л�ȡ��Ȩ����
            authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
            // �����Ҫʹ��Ȩ����ʧЧ��ʹ����ʧЧ�����»�ȡ
            if (invalidateToken) {
                accountManager.invalidateAuthToken("com.google", authToken);
                loginGoogleAccount(activity, false);
            }
        } catch (Exception e) {
        	 // �����ȡ��Ȩ����ʧ�ܣ���¼������־������null
            Log.e(TAG, "get auth token failed");
            authToken = null;
        }

        return authToken;    // ������Ȩ����
    }
/*
 * ���Ե�¼Google�������
 * @param activity  ��ǰ�
 * @param authToken ��Ȩ����
 * @return �Ƿ��¼�ɹ�
 */
    private boolean tryToLoginGtask(Activity activity, String authToken) {
    	 // ����ʹ�õ�ǰ��Ȩ���Ƶ�¼Google�������
        if (!loginGtask(authToken)) {
            // maybe the auth token is out of date, now let's invalidate the
            // token and try again
        	 // �����¼ʧ�ܣ���������Ȩ�����ѹ��ڣ�ʹ����ʧЧ�����»�ȡ
            authToken = loginGoogleAccount(activity, true);
            if (authToken == null) {
                Log.e(TAG, "login google account failed");
                return false;
            }
            // ʹ���µ���Ȩ�����ٴγ��Ե�¼Google�������
            if (!loginGtask(authToken)) {
                Log.e(TAG, "login gtask failed");
                return false;
            }
        }
        return true;
    }
/*
 * ʹ����Ȩ���Ƶ�¼Google�������
 * @param authToken ��Ȩ����
 * @return �Ƿ��¼�ɹ�
 */
    private boolean loginGtask(String authToken) {
    	// �������ӳ�ʱ���׽��ֳ�ʱ
        int timeoutConnection = 10000;
        int timeoutSocket = 15000;
        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        // ����DefaultHttpClientʵ��
        mHttpClient = new DefaultHttpClient(httpParameters);
        // ����BasicCookieStoreʵ�������õ�HttpClient��
        BasicCookieStore localBasicCookieStore = new BasicCookieStore();
        mHttpClient.setCookieStore(localBasicCookieStore);
        // ����HttpClient��ʹ��Expect-Continue
        HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);

        // login gtask
        // ��¼Google�������
        try {
        	// ������¼URL
            String loginUrl = mGetUrl + "?auth=" + authToken;
            HttpGet httpGet = new HttpGet(loginUrl);
            HttpResponse response = null;
            response = mHttpClient.execute(httpGet);

            // get the cookie now
            // ��ȡcookie
            List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
            boolean hasAuthCookie = false;
            for (Cookie cookie : cookies) {
                if (cookie.getName().contains("GTL")) {
                    hasAuthCookie = true;
                }
            }
            if (!hasAuthCookie) {
                Log.w(TAG, "it seems that there is no auth cookie");
            }

            // get the client version
            // ��ȡ�ͻ��˰汾
            String resString = getResponseContent(response.getEntity());
            String jsBegin = "_setup(";
            String jsEnd = ")}</script>";
            int begin = resString.indexOf(jsBegin);
            int end = resString.lastIndexOf(jsEnd);
            String jsString = null;
            if (begin != -1 && end != -1 && begin < end) {
                jsString = resString.substring(begin + jsBegin.length(), end);
            }
            JSONObject js = new JSONObject(jsString);
            mClientVersion = js.getLong("v");
        } catch (JSONException e) {
            Log.e(TAG, e.toString());
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            // simply catch all exceptions
            // ���������쳣
            Log.e(TAG, "httpget gtask_url failed");
            return false;
        }

        return true;
    }
/*
 * ��ȡ����ID��������
 * @return ��ǰ����ID
 */
    private int getActionId() {
    	 // ���ص�ǰ����ID������
        return mActionId++;
    }
/*
 * ����HttpPost����
 * @return ������HttpPost����
 */
    private HttpPost createHttpPost() {
    	 // ����HttpPost������������URL
        HttpPost httpPost = new HttpPost(mPostUrl);
        // ��������ͷ��ָ��Content-TypeΪapplication/x-www-form-urlencoded���ַ���ΪUTF-8
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        httpPost.setHeader("AT", "1");
        // ��������ͷ��ָ��ATΪ1
        return httpPost;
        // ���ش�����HttpPost����
    }
/*
 * ��HttpEntity�л�ȡ��Ӧ���ݲ������ַ�����ʽ��
 * ֧�ִ���gzip��deflateѹ�����롣
 * @param entity HTTP��Ӧʵ��
 * @return ��Ӧ���ݵ��ַ�����ʽ
 * @throws IOException �����ȡ������ʱ��������
 */
    private String getResponseContent(HttpEntity entity) throws IOException {
        String contentEncoding = null;
        // ���HttpEntity�Ƿ����Content-Encodingͷ��
        if (entity.getContentEncoding() != null) {
        	 // ��ȡContent-Encoding��ֵ
            contentEncoding = entity.getContentEncoding().getValue();
            // ��¼��־����ʾ��ǰ��Content-Encoding
            Log.d(TAG, "encoding: " + contentEncoding);
        }
        // ��ȡHttpEntity��ԭʼ������
        InputStream input = entity.getContent();
        // ���Content-Encoding��gzip����ʹ��GZIPInputStream��ѹ��
        if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
            input = new GZIPInputStream(entity.getContent());
        } else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
        	 // ���Content-Encoding��deflate����ʹ��InflaterInputStream��ѹ��
        	Inflater inflater = new Inflater(true);
            input = new InflaterInputStream(entity.getContent(), inflater);
        }

        try {
        	// ����InputStreamReader�����ڽ��ֽ���ת��Ϊ�ַ���
            InputStreamReader isr = new InputStreamReader(input);
            // ����BufferedReader���������ж�ȡ�ַ���
            BufferedReader br = new BufferedReader(isr);
            // ����StringBuilder�����ڴ洢��ȡ��������
            StringBuilder sb = new StringBuilder();
            // ѭ����ȡ�������е�ÿһ��
            while (true) {
                String buff = br.readLine();
                // �����ȡ��null����ʾ�Ѿ���������ĩβ������StringBuilder�е�����
                if (buff == null) {
                    return sb.toString();
                }
                // ����ȡ������׷�ӵ�StringBuilder��
                sb = sb.append(buff);
            }
        } finally {
        	 // ȷ���������ڷ�������ʱ���ر�
            input.close();
        }
    }
/*
 * ����POST���󲢷�����Ӧ��JSONObject��
 * @param js Ҫ���͵��������ݣ���JSONObject��ʽ��ʾ
 * @return ��������Ӧ��JSONObject
 * @throws NetworkFailureException �����������ʧ��
 * @throws ActionFailureException �������ʧ�ܣ�����δ��¼����Ӧ�����޷�ת��ΪJSONObject��
 */
    private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
    	// ����û��Ƿ��ѵ�¼�����δ��¼���׳��쳣
        if (!mLoggedin) {
            Log.e(TAG, "please login first");
            throw new ActionFailureException("not logged in");
        }
        // ����HttpPost����
        HttpPost httpPost = createHttpPost();
        try {
        	 // ����һ��LinkedList���洢�������
            LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
            // ���������ݣ�JSONObject��ת��Ϊ�ַ����������ӵ���������б���
            list.add(new BasicNameValuePair("r", js.toString()));
            // ����UrlEncodedFormEntity�������ڽ������������Ϊ������ʽ
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
            // ������ʵ�����õ�HttpPost������
            httpPost.setEntity(entity);            
            // execute the post
            // ִ��POST����
            HttpResponse response = mHttpClient.execute(httpPost);
            // ��ȡ��Ӧ���ݲ�ת��Ϊ�ַ���
            String jsString = getResponseContent(response.getEntity());
            // ����Ӧ�����ַ���ת��ΪJSONObject������
            return new JSONObject(jsString);

        } catch (ClientProtocolException e) {
        	 // ���񲢼�¼HTTPЭ���쳣
            Log.e(TAG, e.toString());
            e.printStackTrace();
            throw new NetworkFailureException("postRequest failed");
        } catch (IOException e) {
        	 // ���񲢼�¼IO�쳣
            Log.e(TAG, e.toString());
            e.printStackTrace();
            throw new NetworkFailureException("postRequest failed");
        } catch (JSONException e) {
        	// ���񲢼�¼JSON�����쳣
            Log.e(TAG, e.toString());
            e.printStackTrace();
            throw new ActionFailureException("unable to convert response content to jsonobject");
        } catch (Exception e) {
        	 // ���񲢼�¼�����쳣
            Log.e(TAG, e.toString());
            e.printStackTrace();
            throw new ActionFailureException("error occurs when posting request");
        }
    }
/*
 * ����һ���µ����񣬲������ϴ�����������
 * @param task Ҫ�������������
 * @throws NetworkFailureException �����������ʧ��
 * @throws ActionFailureException �������ʧ�ܣ�����JSON����ʧ�ܣ�
 */
    public void createTask(Task task) throws NetworkFailureException {
    	// �ύ���²���
        commitUpdate();
        try {
        	// ����һ���µ�JSONObject�����ڴ洢POST���������
            JSONObject jsPost = new JSONObject();
            // ����һ��JSONArray�����ڴ洢����Ĵ�������
            JSONArray actionList = new JSONArray();

            // action_list
            // ������Ĵ����������ӵ�actionList��
            actionList.put(task.getCreateAction(getActionId()));
            // ��actionList���ӵ�jsPost��
            jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);

            // client_version
            // ���ӿͻ��˰汾��Ϣ��jsPost��
            jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);

            // post
            // ����POST���󣬲�����Ӧ����ת��ΪJSONObject
            JSONObject jsResponse = postRequest(jsPost);
            // ����Ӧ�л�ȡ������飬����ȡ��һ�����
            JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
                    GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
            // �ӽ���л�ȡ�´��������ID�������õ����������
            task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));

        } catch (JSONException e) {��
        	// ���񲢼�¼JSON�����쳣
            Log.e(TAG, e.toString());
            e.printStackTrace();
            throw new ActionFailureException("create task: handing jsonobject failed");
        }
    }
/*
 *  ����һ���µ������б����������ϴ�����������
 * @param tasklist Ҫ�����������б�����
 * @throws NetworkFailureException �����������ʧ��
 * @throws ActionFailureException �������ʧ�ܣ�����JSON����ʧ�ܣ�
 */
    public void createTaskList(TaskList tasklist) throws NetworkFailureException {
    	 // �ύ���²���
        commitUpdate();
        try {
        	 // ����һ���µ�JSONObject�����ڴ洢POST���������
            JSONObject jsPost = new JSONObject();
            // ����һ��JSONArray�����ڴ洢�����б��Ĵ�������
            JSONArray actionList = new JSONArray();

            // action_list
            // �������б��Ĵ����������ӵ�actionList��
            actionList.put(tasklist.getCreateAction(getActionId()));
            // ��actionList���ӵ�jsPost��
            jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);

            // client version
            // ���ӿͻ��˰汾��Ϣ��jsPost��
            jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);

            // post
            // ����POST���󣬲�����Ӧ����ת��ΪJSONObject
            JSONObject jsResponse = postRequest(jsPost);
            // ����Ӧ�л�ȡ������飬����ȡ��һ�����
            JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
                    GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
            // �ӽ���л�ȡ�´��������б���ID�������õ������б�������
            tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));

        } catch (JSONException e) {
        	 // ���񲢼�¼JSON�����쳣
            Log.e(TAG, e.toString());
            e.printStackTrace();
            throw new ActionFailureException("create tasklist: handing jsonobject failed");
        }
    }
/*
 * �ύ����δ��ɵĸ��²�������������
 * @throws NetworkFailureException �����������ʧ��
 * @throws ActionFailureException �������ʧ�ܣ�����JSON����ʧ�ܣ�
 */
    public void commitUpdate() throws NetworkFailureException {
    	 // ����Ƿ���δ��ɵĸ��²���
        if (mUpdateArray != null) {
            try {
            	 // ����һ���µ�JSONObject�����ڴ洢POST���������
                JSONObject jsPost = new JSONObject();

                // action_list
                // ��δ��ɵĸ��²����������ӵ�jsPost��
                jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray);

                // client_version
                // ���ӿͻ��˰汾��Ϣ��jsPost��
                jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
                // ����POST����
                postRequest(jsPost);
                // �ύ�ɹ�����ո��²�������
                mUpdateArray = null;
            } catch (JSONException e) {
            	 // ���񲢼�¼JSON�����쳣
                Log.e(TAG, e.toString());
                e.printStackTrace();
                throw new ActionFailureException("commit update: handing jsonobject failed");
            }
        }
    }
/*
 * ��һ���ڵ����ӵ����²��������У����ڱ�Ҫʱ�ύ���²�����
 * @param node Ҫ���ӵĽڵ����
 * @throws NetworkFailureException �����������ʧ��
 */
    public void addUpdateNode(Node node) throws NetworkFailureException {
    	  // ���ڵ��Ƿ�Ϊ��
        if (node != null) {
            // too many update items may result in an error
            // set max to 10 items
        	// ������²����������Ѿ��г���10����Ŀ�����ύ���²���
            // �������ĸ��²������´���
            if (mUpdateArray != null && mUpdateArray.length() > 10) {
                commitUpdate();
            }
            // ������²�������Ϊ�գ��򴴽�һ���µ�JSONArray
            if (mUpdateArray == null)
                mUpdateArray = new JSONArray();
            // ���ڵ�ĸ��²������ӵ����²���������
            mUpdateArray.put(node.getUpdateAction(getActionId()));
        }
    }
/*
 *  �������һ�������б��ƶ�����һ�������б���
 * @param task Ҫ�ƶ����������
 * @param preParent �����ԭʼ�������б�
 * @param curParent ������¸������б�
 * @throws NetworkFailureException �����������ʧ��
 * @throws ActionFailureException �������ʧ�ܣ�����JSON����ʧ�ܣ�
 */
    public void moveTask(Task task, TaskList preParent, TaskList curParent)
            throws NetworkFailureException {
    	// �ύ���²���
        commitUpdate();
        try {
        	// ����һ���µ�JSONObject�����ڴ洢POST���������
            JSONObject jsPost = new JSONObject();
            // ����һ��JSONArray�����ڴ洢������ƶ�����
            JSONArray actionList = new JSONArray();
            // ����һ��JSONObject�����ڴ洢������ƶ�����
            JSONObject action = new JSONObject();

            // action_list
            // �����ƶ�����������Ϊ"move"
            action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
                    GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE);
            // �����ƶ�������ID
            action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
            // ����Ҫ�ƶ��������ID
            action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid());
            // ���������ͬһ�������б����ƶ������Ҳ��ǵ�һ������������ǰһ���ֵ������ID
            if (preParent == curParent && task.getPriorSibling() != null) {
                // put prioring_sibing_id only if moving within the tasklist and
                // it is not the first one
                action.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, task.getPriorSibling());
            }
            // ���������ԭʼ�������б���ID
            action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid());
            // ����������¸������б���ID
            action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid());
            // ��������ڲ�ͬ�������б�֮���ƶ���������Ŀ�������б���ID
            if (preParent != curParent) {
                // put the dest_list only if moving between tasklists
                action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid());
            }
            // ���ƶ��������ӵ�actionList��
            actionList.put(action);
            // ��actionList���ӵ�jsPost��
            jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);

            // client_version
            // ���ӿͻ��˰汾��Ϣ��jsPost��
            jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
            // ����POST����
            postRequest(jsPost);

        } catch (JSONException e) {
        	// ���񲢼�¼JSON�����쳣
            Log.e(TAG, e.toString());
            e.printStackTrace();
            throw new ActionFailureException("move task: handing jsonobject failed");
        }
    }
/*
 *  ɾ��һ���ڵ㣬��������Ϊ��ɾ��״̬��
 * @param node Ҫɾ���Ľڵ����
 * @throws NetworkFailureException �����������ʧ��
 * @throws ActionFailureException �������ʧ�ܣ�����JSON����ʧ�ܣ�
 */
    public void deleteNode(Node node) throws NetworkFailureException {
    	// �ύ���²���
        commitUpdate();
        try {
        	// ����һ���µ�JSONObject�����ڴ洢POST���������
            JSONObject jsPost = new JSONObject();
            // ����һ��JSONArray�����ڴ洢�ڵ��ɾ������
            JSONArray actionList = new JSONArray();

            // action_list
            // ���ڵ���Ϊ��ɾ��
            node.setDeleted(true);
            // ��ȡ�ڵ�ĸ��²��������������ӵ�actionList��
            actionList.put(node.getUpdateAction(getActionId()));
            // ��actionList���ӵ�jsPost��
            jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);

            // client_version
            // ���ӿͻ��˰汾��Ϣ��jsPost��
            jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
            // ����POST����
            postRequest(jsPost);
            // �ύ�ɹ�����ո��²�������
            mUpdateArray = null;
        } catch (JSONException e) {
        	// ���񲢼�¼JSON�����쳣
            Log.e(TAG, e.toString());
            e.printStackTrace();
            throw new ActionFailureException("delete node: handing jsonobject failed");
        }
    }
/*
 * ��ȡ���������б���������һ�����������б���JSONArray��
 * @return �����������JSONArray
 * @throws NetworkFailureException �����������ʧ��
 * @throws ActionFailureException �������ʧ�ܣ�����δ��¼��JSON����ʧ�ܣ�
 */
    public JSONArray getTaskLists() throws NetworkFailureException {
    	// ����û��Ƿ��ѵ�¼�����δ��¼���׳��쳣
        if (!mLoggedin) {
            Log.e(TAG, "please login first");
            throw new ActionFailureException("not logged in");
        }

        try {
        	 // ����һ��HttpGet�������ڷ���GET����
            HttpGet httpGet = new HttpGet(mGetUrl);
            HttpResponse response = null;
            // ִ��GET���󣬲���ȡ��Ӧ
            response = mHttpClient.execute(httpGet);

            // get the task list
            // ��ȡ��Ӧ���ݲ�ת��Ϊ�ַ���
            String resString = getResponseContent(response.getEntity());
            // ����Ӧ��������ȡ���������б���JSON�ַ���
            String jsBegin = "_setup(";
            String jsEnd = ")}</script>";
            int begin = resString.indexOf(jsBegin);
            int end = resString.lastIndexOf(jsEnd);
            String jsString = null;
            if (begin != -1 && end != -1 && begin < end) {
                jsString = resString.substring(begin + jsBegin.length(), end);
            }
            // ����ȡ��JSON�ַ���ת��ΪJSONObject
            JSONObject js = new JSONObject(jsString);
            // ��JSONObject�л�ȡ�����б���JSONArray������
            return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS);
        } catch (ClientProtocolException e) {
        	// ���񲢼�¼HTTPЭ���쳣
            Log.e(TAG, e.toString());
            e.printStackTrace();
            throw new NetworkFailureException("gettasklists: httpget failed");
        } catch (IOException e) {
        	// ���񲢼�¼IO�쳣
            Log.e(TAG, e.toString());
            e.printStackTrace();
            throw new NetworkFailureException("gettasklists: httpget failed");
        } catch (JSONException e) {
        	// ���񲢼�¼JSON�����쳣
            Log.e(TAG, e.toString());
            e.printStackTrace();
            throw new ActionFailureException("get task lists: handing jasonobject failed");
        }
    }
/*
 *  ��ȡָ�������б��е��������񣬲�����һ�����������JSONArray��
 * @param listGid �������ID
 * @return ���������JSONArray
 * @throws NetworkFailureException �����������ʧ��
 * @throws ActionFailureException �������ʧ�ܣ�����JSON����ʧ�ܣ�
 */
    public JSONArray getTaskList(String listGid) throws NetworkFailureException {
    	// �ύ���²���
        commitUpdate();
        try {
        	// ����һ���µ�JSONObject�����ڴ洢POST���������
            JSONObject jsPost = new JSONObject();
            // ����һ��JSONArray�����ڴ洢��ȡ�����б��IJ���
            JSONArray actionList = new JSONArray();
            // ����һ��JSONObject�����ڴ洢����Ļ�ȡ����
            JSONObject action = new JSONObject();

            // action_list
            // ���û�ȡ����������Ϊ"getall"
            action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
                    GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL);
            // ���û�ȡ������ID
            action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
            // ����Ҫ��ȡ�������б���ID
            action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid);
            // �����Ƿ��ȡ��ɾ����������������Ϊfalse
            action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false);
            // ����ȡ�������ӵ�actionList��
            actionList.put(action);
            // ��actionList���ӵ�jsPost��
            jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);

            // client_version
            // ���ӿͻ��˰汾��Ϣ��jsPost��
            jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
            // ����POST���󣬲�����Ӧ����ת��ΪJSONObject
            JSONObject jsResponse = postRequest(jsPost);
            // ����Ӧ�л�ȡ�����б���JSONArray������
            return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS);
        } catch (JSONException e) {
        	 // ���񲢼�¼JSON�����쳣
            Log.e(TAG, e.toString());
            e.printStackTrace();
            throw new ActionFailureException("get task list: handing jsonobject failed");
        }
    }
/*
 * ��ȡ��ǰͬ�����˻���
 * @return ��ǰͬ�����˻�����
 */
    public Account getSyncAccount() {
        return mAccount;
    }
/*
 * ���ø��²������顣
 */
    public void resetUpdateArray() {
        mUpdateArray = null;
    }
}