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.HFApplication;
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.CampaignData;
import kr.co.hanwha.hifive.data.model.CampaignDataRoot;
import kr.co.hanwha.hifive.data.model.CrudDataRoot;
import kr.co.hanwha.hifive.data.model.DabDataRoot;
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.MainProjectRoot;
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.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.MessageType;
import kr.co.hanwha.hifive.enums.ProjectEvaluationType;
import kr.co.hanwha.hifive.enums.TaskEvaluationType;
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;
import kr.co.hanwha.hifive.util.HFInfoUtil;

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

public class HFNetworkUtil extends HanwhaNetworkUtil implements HFNetworkContract {
    protected final String BASE_URL = (HFApplication.SERVER == HFApplication.SERVER_TYPE.REAL ? BuildConfig.REAL_URL :
            (HFApplication.SERVER == HFApplication.SERVER_TYPE.DEV ? BuildConfig.DEV_URL : BuildConfig.LOCAL_URL));
    protected final String WEB_BASE_URL = (HFApplication.SERVER == HFApplication.SERVER_TYPE.REAL ? BuildConfig.WEB_REAL_URL :
            (HFApplication.SERVER == HFApplication.SERVER_TYPE.DEV ? BuildConfig.WEB_DEV_URL : BuildConfig.WEB_LOCAL_URL));

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

    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_FILTER = "filter";
    private final String PARAM_TRACK_ID = "track_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_DESCRIPTION = "description";
    private final String PARAM_DUE_TO = "due_to";
    private final String PARAM_LIST_REQ_TASKS_MEMBER_INFO = "listReqTasksMemberInfo";
    private final String PARAM_EVALUATION = "evaluation";
    private final String PARAM_REQ_TASKS_MESSAGE_INFO = "reqTasksMessageInfo";
    private final String PARAM_TOKEN = "token";
    private final String PARAM_DEVICE_UID = "device_uid";
    private final String PARAM_LIST_REQ_CAMPAIGNS = "listReqCampaigns";
    private final String PARAM_LIST_REQ_PROJECT_ID = "listReqProjectId";
    private final String PARAM_LIST_MEMBER_INFO = "listMemberInfo";
    private final String PARAM_ID = "id";
    private final String PARAM_GROUP_ID = "group_id";
    private final String PARAM_VALUE = "value";
    private final String PARAM_CMD = "cmd";

    private final String VALUE_CMD_R = "R";
    private final String VALUE_CMD_U = "U";

    /**************************************************
     * 회원가입(인증) - WEB URL
     **************************************************/
    protected final String WEB_MEMBERS_NEW = WEB_BASE_URL + "/members/new";
    /**************************************************
     * SETTING - 개인정보 관리
     **************************************************/
    protected final String WEB_SETTINGS_EDIT_PROFILE = WEB_BASE_URL + "/settings/edit/profile";
    /**************************************************
     * SETTING - 비밀번호 재설정
     **************************************************/
    protected final String WEB_SETTINGS_EDIT_PASSWORD = WEB_BASE_URL + "/settings/edit/password";
    /**************************************************
     * SETTING - 간편 비밀번호 초기화
     **************************************************/
    protected final String WEB_SETTINGS_DELETE_SIMPLE_PASSWORD = WEB_BASE_URL + "/settings/delete/simple-password";
    /**************************************************
     * SETTING - 유닛 리스트(캡틴인 목록만)
     **************************************************/
    protected final String WEB_SETTINGS_GET_UNIT_LIST = WEB_BASE_URL + "/settings/get/unit-list";
    /**************************************************
     * PROJECT - EDIT ISSUE - WEB URL
     **************************************************/
    protected final String WEB_SETTINGS_EDIT_UNIT = WEB_BASE_URL + "/settings/edit/unit";

    /**************************************************
     * TEST - WEB URL
     **************************************************/
    protected final String WEB_TEST_WEBVIEW = WEB_BASE_URL + "/test/webview";
//    url = "http://192.168.0.27:38999/b/v2/test/webview";

    /**************************************************
     * 푸쉬 토큰 등록
     **************************************************/
    protected final String MEMBERS_ADD_DEVICE = BASE_URL + "/members/add/device";
    /**************************************************
     * 업데이트 확인
     **************************************************/
    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";
    /**************************************************
     * 간편 로그인 설정
     **************************************************/
    protected final String MEMBERS_UPDATE_AUTHCODE = BASE_URL + "/members/update/authcode";
    /**************************************************
     * 모든 Member 목록
     **************************************************/
    protected final String MEMBERS_GET_ALL_DROPDOWN = BASE_URL + "/members/get/all/dropdown";
    /**************************************************
     * 푸쉬 목록
     **************************************************/
    protected final String MEMBERS_GET_NOTIFICATIONS_PAGE = BASE_URL + "/members/get/notifications/page";
    /**************************************************
     * 푸쉬 확인
     **************************************************/
    protected final String MEMBERS_CONFIRM_NOTIFICATIONS = BASE_URL + "/members/confirm/notifications";
    /**************************************************
     * 푸쉬 상태 확인 / 변경
     **************************************************/
    protected final String MEMBERS_CRUD_NOTIFICATIONS_SWITCH = BASE_URL + "/members/crud/notifications/switch";

    /**************************************************
     * TASK 생성(등록)
     **************************************************/
    protected final String TASKS2_ADD = BASE_URL + "/tasks2/add";

    /**************************************************
     * MAIN - HOME TASK : 나의 업무 목록 조회
     **************************************************/
    protected final String TASKS2_GET_MAIN_ASSIGNED = BASE_URL + "/tasks2/get/main/assigned";
    /**************************************************
     * MAIN - HOME TASK : 요청한 업무 목록 조회
     **************************************************/
    protected final String TASKS2_GET_MAIN_REQUEST = BASE_URL + "/tasks2/get/main/request";
    /**************************************************
     * MAIN - PROJECT
     **************************************************/
    protected final String PROJECTS_GET_VIEW = BASE_URL + "/projects/get/view";
    /**************************************************
     * PROJECT ALL LIST
     **************************************************/
    protected final String PROJECTS_GET_VIEW_DROPDOWN = BASE_URL + "/projects/get/view/dropdown";
    /**************************************************
     * PROJECT UPDATE - EVALUATION
     **************************************************/
    protected final String PROJECTS_UPDATE_EVAL = BASE_URL + "/projects/update/eval";

    /**************************************************
     * TASK 상세
     **************************************************/
    protected final String TASKS2_GET_DETAILS = BASE_URL + "/tasks2/get/details";
    /**************************************************
     * TASK - 오늘 할일 등록 / 취소
     **************************************************/
    protected final String TASKS2_UPDATE_TODAY = BASE_URL + "/tasks2/update/today";
    /**************************************************
     * TASK UPDATE
     **************************************************/
    protected final String TASKS2_UPDATE_CONTENTS = BASE_URL + "/tasks2/update/contents";
    /**************************************************
     * TASK DELETE
     **************************************************/
    protected final String TASKS2_DELETE = BASE_URL + "/tasks2/delete";
    /**************************************************
     * 댓글 - 댓글 목록 조회
     **************************************************/
    protected final String MESSAGES_GET_PAGE = BASE_URL + "/messages/get/page";
    /**************************************************
     * 댓글 - 댓글 등록
     **************************************************/
    protected final String MESSAGES_ADD = BASE_URL + "/messages/add";
    /**************************************************
     * 댓글 - 댓글 삭제
     **************************************************/
    protected final String MESSAGES_DELETE = BASE_URL + "/messages/delete";
    /**************************************************
     * Task 검색
     **************************************************/
    protected final String TAGS_GET_TASKS = BASE_URL + "/tags/get/tasks";
    /**************************************************
     * Campaign 목록 By Project Id
     **************************************************/
    protected final String PROJECTS_GET_CAMPAIGNS = BASE_URL + "/projects/get/campaigns";
    /**************************************************
     * Campaign 목록 By Member Id
     **************************************************/
    protected final String MEMBERS_GET_CAMPAIGNS = BASE_URL + "/members/get/campaigns";
    /**************************************************
     * DASHBOARD 목록 조회
     **************************************************/
    protected final String PROJECTS_GET_DAB = BASE_URL + "/projects/get/dab";

    /**************************************************
     * tasks.status update 기능 생성 - assigned(status:20)
     **************************************************/
    protected final String TASKS2_UPDATE_STATUS_AS = BASE_URL + "/tasks2/update/status/as";
    /**************************************************
     * tasks.status update 기능 생성 - request(status:30)
     **************************************************/
    protected final String TASKS2_UPDATE_STATUS_REQ = BASE_URL + "/tasks2/update/status/req";
    /**************************************************
     * tasks.status update 기능 생성 - rejected_request(status:40)
     **************************************************/
    protected final String TASKS2_UPDATE_STATUS_R2 = BASE_URL + "/tasks2/update/status/r2";
    /**************************************************
     * tasks.status update 기능 생성 - resolved(status:60)
     **************************************************/
    protected final String TASKS2_UPDATE_STATUS_RST = BASE_URL + "/tasks2/update/status/rst";
    /**************************************************
     * tasks.status update 기능 생성 - rejected_evaluation(status:70)
     **************************************************/
    protected final String TASKS2_UPDATE_STATUS_RE = BASE_URL + "/tasks2/update/status/re";
    /**************************************************
     * tasks.status update 기능 생성 - closed(status:80)
     **************************************************/
    protected final String TASKS2_UPDATE_STATUS_CS = BASE_URL + "/tasks2/update/status/cs";


    @Override
    public String getWebUrlSettingEditProfile() {
        return WEB_SETTINGS_EDIT_PROFILE;
    }

    @Override
    public String getWebUrlSettingEditPassword() {
        return WEB_SETTINGS_EDIT_PASSWORD;
    }

    @Override
    public String getWebUrlSettingDeleteSimplePassword() {
        return WEB_SETTINGS_DELETE_SIMPLE_PASSWORD;
    }

    @Override
    public String getWebUrlSettingGetUnitList() {
        return WEB_SETTINGS_GET_UNIT_LIST;
    }

    @Override
    public String getWebUrlSettingEditUnit() {
        return WEB_SETTINGS_EDIT_UNIT;
    }

    @Override
    public String getWebUrlSignUp() {
        return WEB_MEMBERS_NEW;
    }

    public String getTestUrl() {
        return WEB_TEST_WEBVIEW;//BuildConfig.WEB_LOCAL_URL + "/test/webview";
    }

    public String getTestUrl2() {
        return BuildConfig.WEB_LOCAL_URL + "/test/post/map";
    }

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

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(context, 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(context, 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_DROPDOWN;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(context, 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 description,
                                @NonNull String due_to,
                                @NonNull ArrayList<ReqMemberInfo> listMemberId,
                                @Nullable CampaignData campaignData,
                                @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = TASKS2_ADD;
        HashMap<String, Object> map = new HashMap<>();

        if (listMemberId != null && listMemberId.size() > 0)
            map.put(PARAM_LIST_REQ_TASKS_MEMBER_INFO, listMemberId);
        map.put(PARAM_DUE_TO, due_to);
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);
        map.put(PARAM_AUTHOR_MEMBER_ID, author_member_id);
        map.put(PARAM_DESCRIPTION, description);

        if (campaignData != null) {
            ArrayList<CampaignData> list = new ArrayList<>();
            list.add(campaignData);
            map.put(PARAM_LIST_REQ_CAMPAIGNS, list);
        }

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(context, 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 = TASKS2_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(context, 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 = TASKS2_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(context, rest, url, getDefaultHeader(context), map, MainHomeRequestRoot.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestProjectsGetView(@NonNull Context context, @NonNull String encryption_member_id,
                                       int project_id,
                                       boolean isAll,
                                       @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = PROJECTS_GET_VIEW;

        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);
        map.put(PARAM_PROJECT_ID, project_id);
        map.put(PARAM_FILTER, (isAll ? 1 : 0));

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

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

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

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

    @Override
    public void requestProjectsUpdateEvaluation(@NonNull Context context,
                                                @NonNull String encryption_member_id,
                                                @NonNull int project_id,
                                                @NonNull ProjectEvaluationType evaluation,
                                                @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = PROJECTS_UPDATE_EVAL;

        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);
        map.put(PARAM_PROJECT_ID, project_id);
        map.put(PARAM_EVALUATION, evaluation.getCode());

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(context, rest, url, getDefaultHeader(context), map, BaseData.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(context, 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(context, rest, url, getDefaultHeader(context), map, LoginData.class, mCommonHanwhaNetworkCallback);
    }

    public void requestMembersUpdateAuthcode(@NonNull Context context, @NonNull String login_id, @NonNull String authcode, @NonNull String encryption_member_id, @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = MEMBERS_UPDATE_AUTHCODE;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_LOGIN_ID, login_id);
        map.put(PARAM_AUTHCODE, authcode);
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);

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

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

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

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

        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = TASKS2_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);

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

    @Override
    public void requestUpdateTask(@NonNull Context context,
                                  @NonNull String encryption_member_id,
                                  @NonNull TaskData taskData,
                                  @Nullable CampaignData campaignData,
                                  @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = TASKS2_UPDATE_CONTENTS;

        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_DESCRIPTION, taskData.getDescription());
        map.put(PARAM_DUE_TO, taskData.getDue_to());
        if (campaignData != null) {
            ArrayList<CampaignData> list = new ArrayList<>();
            list.add(campaignData);
            map.put(PARAM_LIST_REQ_CAMPAIGNS, list);
        }

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

    @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 = TASKS2_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(context, rest, url, getDefaultHeader(context), map, BaseData.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestTaskStatusAccept(@NonNull Context context,
                                        @NonNull String encryption_member_id,
                                        @NonNull int author_member_id,
                                        @NonNull int task_id,
                                        @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = TASKS2_UPDATE_STATUS_AS;
        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);

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

    @Override
    public void requestTaskStatusRequest(@NonNull Context context,
                                         @NonNull String encryption_member_id,
                                         @NonNull int author_member_id,
                                         @NonNull int task_id,
                                         @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = TASKS2_UPDATE_STATUS_REQ;
        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);

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

    @Override
    public void requestTaskStatusRejectedRequest(@NonNull Context context,
                                                 @NonNull String encryption_member_id,
                                                 @NonNull int author_member_id,
                                                 @NonNull int task_id,
                                                 @NonNull String message,
                                                 @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = TASKS2_UPDATE_STATUS_R2;
        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_REQ_TASKS_MESSAGE_INFO, new RequestTaskMessageInfo(MessageType.REJECT_REQUEST.getCode(), message));

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

    @Override
    public void requestTaskStatusEvaluated(@NonNull Context context,
                                           @NonNull String encryption_member_id,
                                           @NonNull int author_member_id,
                                           @NonNull int task_id,
                                           @NonNull TaskEvaluationType taskEvaluationType,
                                           @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = TASKS2_UPDATE_STATUS_RST;
        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);
        if (taskEvaluationType != TaskEvaluationType.NULL)
            map.put(PARAM_EVALUATION, taskEvaluationType.getCode());

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

    @Override
    public void requestTaskStatusRejectedEvaluation(@NonNull Context context,
                                                    @NonNull String encryption_member_id,
                                                    @NonNull int author_member_id,
                                                    @NonNull int task_id,
                                                    @NonNull String message,
                                                    @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = TASKS2_UPDATE_STATUS_RE;
        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_REQ_TASKS_MESSAGE_INFO, new RequestTaskMessageInfo(MessageType.REJECT_EVALUATION.getCode(), message));

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

    @Override
    public void requestTaskStatusClosed(@NonNull Context context,
                                        @NonNull String encryption_member_id,
                                        @NonNull int author_member_id,
                                        @NonNull int task_id,
                                        @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = TASKS2_UPDATE_STATUS_CS;
        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);

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(context, 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 = TASKS2_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(context, rest, url, getDefaultHeader(context), map, BaseData.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestNotificationOpen(@NonNull Context context,
                                        @NonNull String encryption_member_id,
                                        @Nullable int group_id,
                                        @Nullable int task_id,
                                        @Nullable int project_id,
                                        @Nullable int track_id,
                                        @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = MEMBERS_CONFIRM_NOTIFICATIONS;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);
        map.put(PARAM_GROUP_ID, group_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(context, rest, url, getDefaultHeader(context), map, AlarmListRoot.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestNotificationSet(@NonNull Context context,
                                       @NonNull String encryption_member_id,
                                       @NonNull boolean onoff,
                                       @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = MEMBERS_CRUD_NOTIFICATIONS_SWITCH;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);
        map.put(PARAM_VALUE, onoff ? "1" : "0");
        map.put(PARAM_CMD, VALUE_CMD_U);

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

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

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

    /**************************************************
     * OLD
     **************************************************/
    @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(context, 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(context, rest, url, getDefaultHeader(context), map, MessageAddData.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestDeleteMessage(@NonNull Context context,
                                     @NonNull String encryption_member_id,
                                     @NonNull int id,
                                     @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;
        String url = MESSAGES_DELETE;
        HashMap<String, Object> map = new HashMap<>();
        map.put(PARAM_ENCRYPTION_MEMBER_ID, encryption_member_id);
        map.put(PARAM_ID, id);
        HanwhaNetworkClient client = new HanwhaNetworkClient();
        mHanwhaNetworkCallback = callback;
        client.request(context, rest, url, getDefaultHeader(context), map, BaseData.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(context, rest, url, getDefaultHeader(context), map, SearchTagData.class, mCommonHanwhaNetworkCallback);
    }

    @Override
    public void requestCampaignListByProjectId(@NonNull Context context,
                                               @NonNull String encryption_member_id,
                                               @NonNull ArrayList<Integer> listReqProjectId,
                                               @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;

        String url = PROJECTS_GET_CAMPAIGNS;

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

        mHanwhaNetworkCallback = callback;

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

    @Override
    public void requestCampaignListByMemberId(@NonNull Context context,
                                              @NonNull String encryption_member_id,
                                              @NonNull ArrayList<Integer> listMemberInfo,
                                              @Nullable HanwhaNetworkCallback callback) {
        HanwhaNetworkClient.REST rest = HanwhaNetworkClient.REST.POST;

        String url = MEMBERS_GET_CAMPAIGNS;

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

        mHanwhaNetworkCallback = callback;

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

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

        String url = PROJECTS_GET_DAB;

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

        mHanwhaNetworkCallback = callback;

        HanwhaNetworkClient client = new HanwhaNetworkClient();
        client.request(context, rest, url, getDefaultHeader(context), map, DabDataRoot.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 final String USER_TYPE = "User-Type";

    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_TYPE, HFInfoUtil.getDepartmentValue(context));
//        header.put(USER_AGENT, "android/0.0.1 DCA/0.1.1 Chrome/18.0.1025.308");

        return header;
    }

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

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