package kr.co.hanwha.hifive.network;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;

import kr.co.hanwha.hifive.BuildConfig;
import kr.co.hanwha.hifive.data.model.AddTaskRoot;
import kr.co.hanwha.hifive.data.model.AlarmListRoot;
import kr.co.hanwha.hifive.data.model.BaseData;
import kr.co.hanwha.hifive.data.model.LoginData;
import kr.co.hanwha.hifive.data.model.MainHomeAssignedRoot;
import kr.co.hanwha.hifive.data.model.MainHomeRequestRoot;
import kr.co.hanwha.hifive.data.model.MemberInfoRoot;
import kr.co.hanwha.hifive.data.model.MessageAddData;
import kr.co.hanwha.hifive.data.model.MessageListRoot;
import kr.co.hanwha.hifive.data.model.ProjectInfoRoot;
import kr.co.hanwha.hifive.data.model.ProjectTaskRoot;
import kr.co.hanwha.hifive.data.model.ReqMemberInfo;
import kr.co.hanwha.hifive.data.model.RequestTaskMessageInfo;
import kr.co.hanwha.hifive.data.model.SearchTagData;
import kr.co.hanwha.hifive.data.model.TaskData;
import kr.co.hanwha.hifive.data.model.TaskDetailRoot;
import kr.co.hanwha.hifive.data.model.UpdateData;
import kr.co.hanwha.hifive.enums.EvaluationType;
import kr.co.hanwha.hifive.enums.MessageType;
import kr.co.hanwha.hifive.enums.TaskStatusType;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkCallback;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkClient;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkUtil;
import kr.co.hanwha.hifive.util.HFCommonUtil;

/**
 * Created by sanghoonkwon on 2018. 1. 11..
 */

public class HFNetworkUtil extends HanwhaNetworkUtil implements HFNetworkContract {
    protected final String BASE_URL = BuildConfig.DEV_URL;

    private final int RESULT_SUCCESS = 0;
    protected final int COMMENT_PAGE_SIZE = 100;

    private final String PARAM_LOGIN_ID = "login_id";
    private final String PARAM_MEMBER_ID = "member_id";
    private final String PARAM_PASSWORD = "password";
    private final String PARAM_AUTHCODE = "authcode";
    private final String PARAM_ENCRYPTION_MEMBER_ID = "encryption_member_id";
    private final String PARAM_PROJECT_ID = "project_id";
    private final String PARAM_TRACK_ID = "track_id";
    private final String PARAM_NOTI_INFO_ID = "noti_info_id";
    private final String PARAM_TASK_ID = "task_id";
    private final String PARAM_IS_PINNED = "is_pinned";
    private final String PARAM_CONTENTS = "contents";
    private final String PARAM_TYPE = "type";
    private final String PARAM_CURRENT_PAGE_NO = "current_page_no";
    private final String PARAM_RECORDS_PER_PAGE = "records_per_page";
    private final String PARAM_SEARCH_TAG = "searchTag";
    private final String PARAM_AUTHOR_MEMBER_ID = "author_member_id";
    private final String PARAM_IS_TODAY = "is_today";
    private final String PARAM_TASK_NAME = "task_name";
    private final String PARAM_DESCRIPTION = "description";
    private final String PARAM_DUE_TO = "due_to";
    private final String PARAM_LIST_REQ_TASKS_MEMBER_INFO = "listReqTasksMemberInfo";
    private final String PARAM_STATUS = "status";
    private final String PARAM_EVALUATION = "evaluation";
    private final String PARAM_REQ_TASKS_MESSAGE_INFO = "reqTasksMessageInfo";

    /**************************************************
     * 업데이트 확인
     **************************************************/
    protected final String APP_UPDATE = BASE_URL + "/app/update";
    /**************************************************
     * 계정 로그인
     **************************************************/
    protected final String MEMBERS_LOGIN = BASE_URL + "/members/login";
    /**************************************************
     * 간편 로그인
     **************************************************/
    protected final String MEMBERS_SIMPLE_LOGIN = BASE_URL + "/members/simplelogin";
    /**************************************************
     * 모든 Member 목록
     **************************************************/
    protected final String MEMBERS_GET_ALL = BASE_URL + "/members/get/all";
    /**************************************************
     * TASK 생성
     **************************************************/
    protected final String TASKS_ADD_LIST = BASE_URL + "/tasks/add/list";
    /**************************************************
     * MAIN - HOME TASK : 나의 업무 목록 조회
     **************************************************/
    protected final String TASKS_GET_MAIN_ASSIGNED = BASE_URL + "/tasks/get/main/assigned";
    /**************************************************
     * MAIN - HOME TASK : 요청한 업무 목록 조회
     **************************************************/
    protected final String TASKS_GET_MAIN_REQUEST = BASE_URL + "/tasks/get/main/request";
    /**************************************************
     * TASK 상세
     **************************************************/
    protected final String TASKS_GET_DETAILS = BASE_URL + "/tasks/get/details";
    /**************************************************
     * TASK - 오늘 할일 등록 / 취소
     **************************************************/
    protected final String TASKS_UPDATE_TODAY = BASE_URL + "/tasks/update/today";
    /**************************************************
     * TASK UPDATE
     **************************************************/
    protected final String TASKS_UPDATE = BASE_URL + "/tasks/update";
    /**************************************************
     * TASK DELETE
     **************************************************/
    protected final String TASKS_DELETE = BASE_URL + "/tasks/delete";
    /**************************************************
     * TASK 반려
     **************************************************/
    protected final String TASKS_UPDATE_REJECTION = BASE_URL + "/tasks/update/rejection";
    /**************************************************
     * 댓글 - 댓글 목록 조회
     **************************************************/
    protected final String MESSAGES_GET_PAGE = BASE_URL + "/messages/get/page";
    /**************************************************
     * 댓글 - 댓글 등록
     **************************************************/
    protected final String MESSAGES_ADD = BASE_URL + "/messages/add";
    /**************************************************
     * Task 검색
     **************************************************/
    protected final String TAGS_GET_TASKS = BASE_URL + "/tags/get/tasks";

    /** OLD **/
    /**************************************************
     * Project 전체 목록
     **************************************************/
    protected final String MOBILE_PROJECTS_GET_PROJECTS_INFO = BuildConfig.DEV_URL_OLD + "/mobile/Projects/getProjectsInfo.api";
    /**************************************************
     * 푸쉬 목록
     **************************************************/
    protected final String MOBILE_MEMBERS_NOTIFICATION_GET = BuildConfig.DEV_URL_OLD + "/mobile/Members/notification/get.api";
    /**************************************************
     * 푸쉬 확인
     **************************************************/
    protected final String MOBILE_MEMBERS_NOTIFICATION_UPDATE_OPEN = BuildConfig.DEV_URL_OLD + "/mobile/Members/notification/update/open.api";

    public void test(@NonNull Context context, @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = "https://qtalk-dev.dreamplus.center/newgw/a/v2/members/search/hr";//BASE_URL + "/members/addup";
        HashMap<String, Object> map = new HashMap<>();
//        map.put("login_id", "sanghoonkwon@hanwha.com");
//        map.put("name", "권상훈");
//        map.put("phone_mobile", "010-5018-6052");
//        map.put("company", "한화 S&C");
//        map.put("department", "개발팀");
//        map.put("rank", 10);
//        map.put("position", 10);
//        map.put("password", "1111");

        map.put("client_timezone", "09:00");
        map.put("encryption_member_id", "yxBcz8vJsoPySd3FCmJXVw==");
        map.put("keyword", "전");


        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(rest, url, getDefaultHeader(context), map, BaseData.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestCheckUpdate(@NonNull Context context, @Nullable String login_id, @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = APP_UPDATE;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_LOGIN_ID, TextUtils.isEmpty(login_id) ? "" : login_id);

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(rest, url, getDefaultHeader(context), map, UpdateData.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestMemberAll(@NonNull Context context, @NonNull String encryption_member_id, @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = MEMBERS_GET_ALL;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(rest, url, getDefaultHeader(context), map, MemberInfoRoot.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestSendTask(@NonNull Context context,
                                @NonNull int author_member_id,
                                @NonNull String encryption_member_id,
                                @NonNull String task_name,
                                @NonNull String due_to,
                                @NonNull ArrayList<ReqMemberInfo> listMemberId,
                                @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = TASKS_ADD_LIST;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_AUTHOR_MEMBER_ID, author_member_id);
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);
        map.put(PARAM_TASK_NAME, task_name);
        map.put(PARAM_DESCRIPTION, task_name);
        map.put(PARAM_DUE_TO, due_to);
        map.put(PARAM_LIST_REQ_TASKS_MEMBER_INFO, listMemberId);

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(rest, url, getDefaultHeader(context), map, AddTaskRoot.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestTasksGetMainAssigned(@NonNull Context context, @NonNull String encryption_member_id, @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = TASKS_GET_MAIN_ASSIGNED;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(rest, url, getDefaultHeader(context), map, MainHomeAssignedRoot.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestTasksGetMainRequest(@NonNull Context context, @NonNull String encryption_member_id, @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = TASKS_GET_MAIN_REQUEST;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(rest, url, getDefaultHeader(context), map, MainHomeRequestRoot.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestMembersLogin(@NonNull Context context, @NonNull String login_id, @NonNull String password, @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = MEMBERS_LOGIN;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_LOGIN_ID, login_id);
        map.put(PARAM_PASSWORD, password);

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(rest, url, getDefaultHeader(context), map, LoginData.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestMembersSimpleLogin(@NonNull Context context, @NonNull String login_id, @NonNull String encAuthcode, @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = MEMBERS_SIMPLE_LOGIN;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_LOGIN_ID, login_id);
        map.put(PARAM_AUTHCODE, encAuthcode);

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(rest, url, getDefaultHeader(context), map, LoginData.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestTasksGetDetails(@NonNull Context context,
                                       @NonNull String encryption_member_id,
                                       @NonNull int task_id,
                                       @NonNull int project_id,
                                       @NonNull int track_id,
                                       @Nullable HanwhaNetworkCallback callback) {

        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = TASKS_GET_DETAILS;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);
        map.put(PARAM_TASK_ID, task_id);
        map.put(PARAM_PROJECT_ID, project_id);
        map.put(PARAM_TRACK_ID, track_id);

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(rest, url, getDefaultHeader(context), map, TaskDetailRoot.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestUpdateTask(@NonNull Context context,
                                  @NonNull String encryption_member_id,
                                  @NonNull int author_member_id,
                                  @NonNull int task_id,
                                  @NonNull int project_id,
                                  @NonNull int track_id,
                                  @NonNull String task_name,
                                  @NonNull String description,
                                  @NonNull String due_to,
                                  @NonNull int taskStatusCode,
                                  @NonNull int evaluationCode,
                                  @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = TASKS_UPDATE;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);
        map.put(PARAM_AUTHOR_MEMBER_ID, author_member_id);
        map.put(PARAM_TASK_ID, task_id);
        map.put(PARAM_PROJECT_ID, project_id);
        map.put(PARAM_TRACK_ID, track_id);
        if (TextUtils.isEmpty(task_name) == false) map.put(PARAM_TASK_NAME, task_name);
        if (TextUtils.isEmpty(description) == false) map.put(PARAM_DESCRIPTION, description);
        if (TextUtils.isEmpty(due_to) == false) map.put(PARAM_DUE_TO, due_to);
        map.put(PARAM_STATUS, taskStatusCode);
        if (evaluationCode != EvaluationType.NULL.getCode())
            map.put(PARAM_EVALUATION, evaluationCode);

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(rest, url, getDefaultHeader(context), map, BaseData.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestUpdateTask(@NonNull Context context,
                                  @NonNull String encryption_member_id,
                                  @NonNull TaskData taskData,
                                  @Nullable HanwhaNetworkCallback callback) {
        requestUpdateTask(context,
                encryption_member_id,
                taskData.getAuthor_member_id(),
                taskData.getTask_id(),
                taskData.getProject_id(),
                taskData.getTrack_id(),
                taskData.getTask_name(),
                taskData.getTask_name(),
                taskData.getDue_to(),
                taskData.getStatus(),
                taskData.getEvaluation(),
                callback);
    }

    @Override
    public void requestTaskDelete(@NonNull Context context,
                                  @NonNull TaskData data,
                                  @NonNull String encryption_member_id,
                                  @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = TASKS_DELETE;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);
        map.put(PARAM_AUTHOR_MEMBER_ID, data.getAuthor_member_id());
        map.put(PARAM_TASK_ID, data.getTask_id());
        map.put(PARAM_PROJECT_ID, data.getProject_id());
        map.put(PARAM_TRACK_ID, data.getTrack_id());
        map.put(PARAM_MEMBER_ID, data.getMember_id());

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(rest, url, getDefaultHeader(context), map, BaseData.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestTaskAccept(@NonNull Context context,
                                  @NonNull String encryption_member_id,
                                  @NonNull TaskData taskData,
                                  @Nullable HanwhaNetworkCallback callback) {
        requestUpdateTask(context,
                encryption_member_id,
                taskData.getAuthor_member_id(),
                taskData.getTask_id(),
                taskData.getProject_id(),
                taskData.getTrack_id(),
                taskData.getTask_name(),
                taskData.getDescription(),
                taskData.getDue_to(),
                TaskStatusType.ASSIGNED.getCode(),
                EvaluationType.NULL.getCode(),
                callback);
    }

    @Override
    public void requestSetTaskEvaluation(@NonNull Context context,
                                         @NonNull String encryption_member_id,
                                         @NonNull TaskData taskData,
                                         @NonNull EvaluationType evaluationType,
                                         @Nullable HanwhaNetworkCallback callback) {
        requestUpdateTask(context,
                encryption_member_id,
                taskData.getAuthor_member_id(),
                taskData.getTask_id(),
                taskData.getProject_id(),
                taskData.getTrack_id(),
                taskData.getTask_name(),
                taskData.getDescription(),
                taskData.getDue_to(),
                TaskStatusType.EVALUATED.getCode(),
                evaluationType.getCode(),
                callback);
    }

    @Override
    public void requestTasksUpdateRejection(@NonNull Context context,
                                            @NonNull String encryption_member_id,
                                            @NonNull TaskData taskData,
                                            @NonNull String message,
                                            @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = TASKS_UPDATE_REJECTION;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);
        map.put(PARAM_AUTHOR_MEMBER_ID, taskData.getAuthor_member_id());
        map.put(PARAM_TASK_ID, taskData.getTask_id());
        map.put(PARAM_PROJECT_ID, taskData.getProject_id());
        map.put(PARAM_TRACK_ID, taskData.getTrack_id());

        map.put(PARAM_STATUS, TaskStatusType.REJECTED_REQUEST.getCode());
        map.put(PARAM_REQ_TASKS_MESSAGE_INFO, new RequestTaskMessageInfo(MessageType.REJECT_REQUEST.getCode(), message));

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(rest, url, getDefaultHeader(context), map, BaseData.class, mCommonHanwhaNetworkCallback);
    }

    public void requestTasksUpdateToday(@NonNull Context context,
                                        @NonNull TaskData data,
                                        @NonNull String encryption_member_id,
                                        @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = TASKS_UPDATE_TODAY;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);
        map.put(PARAM_AUTHOR_MEMBER_ID, data.getAuthor_member_id());
        map.put(PARAM_TASK_ID, data.getTask_id());
        map.put(PARAM_PROJECT_ID, data.getProject_id());
        map.put(PARAM_TRACK_ID, data.getTrack_id());
        map.put(PARAM_IS_TODAY, data.isIs_today());

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(rest, url, getDefaultHeader(context), map, BaseData.class, mCommonHanwhaNetworkCallback);
    }

    /**************************************************
     * OLD
     **************************************************/
    @Override
    public void requestGetProjectsInfo(@NonNull Context context,
                                       @NonNull String encryption_member_id,
                                       @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = MOBILE_PROJECTS_GET_PROJECTS_INFO;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(rest, url, getDefaultHeader(context), map, ProjectInfoRoot.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestNotificationList(@NonNull Context context, @NonNull String encryption_member_id, @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = MOBILE_MEMBERS_NOTIFICATION_GET;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(rest, url, getDefaultHeader(context), map, AlarmListRoot.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestNotificationOpen(@NonNull Context context, @NonNull String encryption_member_id,
                                        @NonNull int noti_info_id, @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = MOBILE_MEMBERS_NOTIFICATION_UPDATE_OPEN;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);
        map.put(PARAM_NOTI_INFO_ID, noti_info_id);

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(rest, url, getDefaultHeader(context), map, AlarmListRoot.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestGetMessageList(@NonNull Context context,
                                      @NonNull String encryption_member_id,
                                      @NonNull int task_id,
                                      @NonNull int current_page,
                                      @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = MESSAGES_GET_PAGE;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);
        map.put(PARAM_TASK_ID, task_id);
        map.put(PARAM_CURRENT_PAGE_NO, current_page);
        map.put(PARAM_RECORDS_PER_PAGE, COMMENT_PAGE_SIZE);
        map.put(PARAM_IS_PINNED, false);
        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(rest, url, getDefaultHeader(context), map, MessageListRoot.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestAddMessage(@NonNull Context context,
                                  @NonNull String encryption_member_id,
                                  @NonNull int task_id,
                                  @NonNull String contents,
                                  @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = MESSAGES_ADD;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);
        map.put(PARAM_TASK_ID, task_id);
        map.put(PARAM_CONTENTS, contents);
        map.put(PARAM_IS_PINNED, false);
        map.put(PARAM_TYPE, MessageType.COMMENT.getCode());
        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(rest, url, getDefaultHeader(context), map, MessageAddData.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestSearchTaskByTag(@NonNull Context context, @NonNull String encryption_member_id, @NonNull String searchTag, @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;

        String url = TAGS_GET_TASKS;

        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);
        map.put(PARAM_SEARCH_TAG, searchTag);

        mHanwhaNetworkCallback = callback;

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        client.request(rest, url, getDefaultHeader(context), map, SearchTagData.class, mCommonHanwhaNetworkCallback);
    }

    /**************************************************
     * HanwhaNetworkCallback
     **************************************************/
    private HanwhaNetworkCallback mHanwhaNetworkCallback = null;
    HanwhaNetworkCallback mCommonHanwhaNetworkCallback = new HanwhaNetworkCallback() {
        @Override
        public void onSuccess(Object response) {
            if (mHanwhaNetworkCallback != null) {
                if (response != null && response instanceof BaseData) {
                    BaseData baseData = (BaseData) response;
                    if (baseData != null) {
                        if (baseData.getRtn_code() == RESULT_SUCCESS) {
                            mHanwhaNetworkCallback.onSuccess(response);
                        } else {
                            mHanwhaNetworkCallback.onFailure(new HanwhaNetworkError(baseData.getRtn_code(), baseData.getRtn_message()));
                        }
                    } else {
                        mHanwhaNetworkCallback.onFailure(new HanwhaNetworkError());
                    }
                } else {
                    mHanwhaNetworkCallback.onFailure(new HanwhaNetworkError());
                }
            }
        }

        @Override
        public void onFailure(HanwhaNetworkError error) {
            if (mHanwhaNetworkCallback != null) mHanwhaNetworkCallback.onFailure(error);
        }

        @Override
        public void onPreExecute() {
            if (mHanwhaNetworkCallback != null) mHanwhaNetworkCallback.onPreExecute();
        }

        @Override
        public void onPostExecute() {
            if (mHanwhaNetworkCallback != null) mHanwhaNetworkCallback.onPostExecute();
        }
    };

    /**************************************************
     * Header
     **************************************************/
    private final String CONTENT_TYPE = "Content-Type";
    private final String USER_AGENT = "User-Agent";

    private HashMap<String, String> getDefaultHeader(Context context) {
        HashMap header = new HashMap();
        header.put(CONTENT_TYPE, "application/json");
        header.put(USER_AGENT, getOsVersion() + " " + getAppVersion(context));
//        header.put(USER_AGENT, "android/0.0.1 DCA/0.1.1 Chrome/18.0.1025.308");
        return header;
    }

    private String getOsVersion() {
//        if (NGWApplication.SERVER == NGWApplication.SERVER_TYPE.REAL)
//            return "Android/" + GWCommonUtil.getDeviceOsVersion();
//        else
        return "Android-dev/" + HFCommonUtil.getDeviceOsVersion();
    }

    private String getAppVersion(Context context) {
        return "DCA/" + HFCommonUtil.getAppVersion(context);
    }
}
