package kr.co.hanwha.hifive.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import kr.co.hanwha.hifive.data.model.TaskData;
import kr.co.hanwha.hifive.network.HFNetworkUtil;
import kr.co.hanwha.hifive.screen.RootActivity;
import kr.co.hanwha.hifive.screen.alarm.AlarmActivity;
import kr.co.hanwha.hifive.screen.common.CommonWebViewActivity;
import kr.co.hanwha.hifive.screen.intro.AuthNumberActivity;
import kr.co.hanwha.hifive.screen.intro.CheckAuthNumberActivity;
import kr.co.hanwha.hifive.screen.intro.IntroActivity;
import kr.co.hanwha.hifive.screen.main.MainActivity;
import kr.co.hanwha.hifive.screen.main.dashboard.DashboardProjectActivity;
import kr.co.hanwha.hifive.screen.main.setting.MainSettingWebviewActivity;
import kr.co.hanwha.hifive.screen.main.setting.SettingScreenActivity;
import kr.co.hanwha.hifive.screen.main.webview.NoticeWebViewActivity;
import kr.co.hanwha.hifive.screen.search.SearchActivity;
import kr.co.hanwha.hifive.screen.task.AddTaskActivity;
import kr.co.hanwha.hifive.screen.task.AddTaskSelectCampaignActivity;
import kr.co.hanwha.hifive.screen.task.AddTaskSelectMemberActivity;
import kr.co.hanwha.hifive.screen.task.TaskDetailActivity;

import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;

/**
 * Activity util
 * Created by sanghoonkwon on 2018. 1. 10..
 */

public class HFActivityUtil {
    public static final int REQUEST_ADD_TASK_SELECT_MEMBER = 1011;
    public static final int REQUEST_ADD_TASK_SELECT_CAMPAIGN = 1012;
    public static final int REQUEST_ADD_TASK = 1031;
    public static final int REQUEST_EDIT_TASK = 1032;
    public static final int REQUEST_EDIT_TASK_REREQUEST = 1033;
    public static final int REQUEST_TASK_DETAIL = 1041;
    public static final int REQUEST_SET_EASYLOGIN_REPEAT = 1050;

    public static final String INTENT_FINISH = "finish";
    public static final String INTENT_IS_EDIT = "is_edit";
    public static final String INTENT_IS_REREQUEST = "is_rerequest";
    public static final String INTENT_TASK_ID = "task_id";
    public static final String INTENT_PROJECT_ID = "project_id";
    public static final String INTENT_TRACK_ID = "track_id";
    public static final String INTENT_FLAG = "flag";
    public static final String INTENT_SEARCH_WORD = "search_word";
    public static final String INTENT_AUTH_NUMBER_REPEAT = "auth_number_repeat";
    public static final String INTENT_AUTH_NUMBER = "auth_number";
    public static final String INTENT_URL = "url";
    public static final String INTENT_VISIBLE_SHARE = "share";
    public static final String INTENT_VISIBLE_TITLE = "signup";
    public static final String INTENT_VISIBLE_SUBTITLE = "signup";

    public static final String INTENT_NOTICE_NO = "notice_no";
    public static final String INTENT_NOTICE_URL = "notice_url";
    public static final String INTENT_ENCRYPTION = "encryption";

    public static String LAST_TAG = "";

    public static void clearTag() {
        LAST_TAG = "";
    }

    /**
     * 앱 종료를 위한 Activity
     *
     * @param activity Activity
     * @param finish   boolean
     */
    public static void startRootActivity(@NonNull Activity activity, boolean finish) {
        if (RootActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = RootActivity.class.getSimpleName();

        Intent intent = new Intent(activity, RootActivity.class);
        intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(INTENT_FINISH, finish);
        activity.startActivity(intent);
    }

    /**
     * 간편 인증 - 로그인 완료후 백그라운드에서 진입시
     */
    public static void startCheckAuthNumberActivity(@NonNull Activity activity) {
        if (CheckAuthNumberActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = CheckAuthNumberActivity.class.getSimpleName();

        Intent intent = new Intent(activity, CheckAuthNumberActivity.class);
        intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivity(intent);
    }


    /**
     * @param activity Activity
     */
    public static void startIntroActivity(@NonNull Activity activity) {
        if (IntroActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = IntroActivity.class.getSimpleName();

        Intent intent = new Intent(activity, IntroActivity.class);
//        intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        activity.startActivity(intent);
    }


    /**
     * Action View : URL
     *
     * @param context Context
     * @param url     URL
     * @throws Exception
     */
    public static void startUrl(@NonNull Context context, String url) throws Exception {
        Uri uri = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        context.startActivity(intent);
    }

    /**
     * SETTING - 유닛 리스트(캡틴인 목록만)
     */
    public static void startMainSettingUnitWebviewActivity(@NonNull Activity activity) {
        if (MainSettingWebviewActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = MainSettingWebviewActivity.class.getSimpleName();

        HFNetworkUtil networkUtil = new HFNetworkUtil();
        String url = networkUtil.getWebUrlSettingGetUnitList();
        Intent intent = new Intent(activity, MainSettingWebviewActivity.class);
        intent.putExtra(INTENT_URL, url);
        activity.startActivity(intent);
    }

    /**
     * SETTING - 개인정보 관리
     */
    public static void startMainSettingEditProfileWebviewActivity(@NonNull Activity activity) {
        if (MainSettingWebviewActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = MainSettingWebviewActivity.class.getSimpleName();

        HFNetworkUtil networkUtil = new HFNetworkUtil();
        String url = networkUtil.getWebUrlSettingEditProfile();
        Intent intent = new Intent(activity, MainSettingWebviewActivity.class);
        intent.putExtra(INTENT_URL, url);
        activity.startActivity(intent);
    }

    /**
     * SETTING - 비밀번호 재설정
     *
     * @param activity Activity
     */
    public static void startMainSettingEditPasswordWebviewActivity(@NonNull Activity activity) {
        if (MainSettingWebviewActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = MainSettingWebviewActivity.class.getSimpleName();

        HFNetworkUtil networkUtil = new HFNetworkUtil();
        String url = networkUtil.getWebUrlSettingEditPassword();
        Intent intent = new Intent(activity, MainSettingWebviewActivity.class);
        intent.putExtra(INTENT_URL, url);
        activity.startActivity(intent);
    }

    /**
     * SETTING - 간편 비밀번호 초기화
     *
     * @param activity Activity
     */
    public static void startMainSettingDeleteSimplePasswordWebviewActivity(@NonNull Activity activity) {
        if (MainSettingWebviewActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = MainSettingWebviewActivity.class.getSimpleName();

        HFNetworkUtil networkUtil = new HFNetworkUtil();
        String url = networkUtil.getWebUrlSettingDeleteSimplePassword();
        Intent intent = new Intent(activity, MainSettingWebviewActivity.class);
        intent.putExtra(INTENT_URL, url);
        activity.startActivity(intent);
    }

    /**
     * 설정 - 기본 화면
     *
     * @param activity Activity
     */
    public static void startSettingScreenActivity(@NonNull Activity activity) {
        if (SettingScreenActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = SettingScreenActivity.class.getSimpleName();

        Intent intent = new Intent(activity, SettingScreenActivity.class);
        activity.startActivity(intent);
    }


    /**
     * Common Webview
     *
     * @param activity Activity
     */
    public static void startCommonWebViewActivity(@NonNull Activity activity, @NonNull String url) {
        if (CommonWebViewActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = CommonWebViewActivity.class.getSimpleName();

        Intent intent = new Intent(activity, CommonWebViewActivity.class);
        intent.putExtra(INTENT_URL, url);
        intent.putExtra(INTENT_VISIBLE_SHARE, true);
        activity.startActivity(intent);
    }

    /**
     * Common Webview
     *
     * @param activity Activity
     */
    public static void startCommonWebViewActivity(@NonNull Activity activity,
                                                  @NonNull String url,
                                                  @Nullable String encryption) {
        if (CommonWebViewActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = CommonWebViewActivity.class.getSimpleName();

        Intent intent = new Intent(activity, CommonWebViewActivity.class);
        intent.putExtra(INTENT_URL, url);
        if (TextUtils.isEmpty(encryption) == false)
            intent.putExtra(INTENT_ENCRYPTION, encryption);
        activity.startActivity(intent);
    }

    /**
     * Common Webview
     *
     * @param activity Activity
     */
    public static void startCommonWebViewActivity(@NonNull Activity activity, @NonNull String url,
                                                  @Nullable String encryption, @Nullable int project_id) {
        if (CommonWebViewActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = CommonWebViewActivity.class.getSimpleName();

        Intent intent = new Intent(activity, CommonWebViewActivity.class);
        intent.putExtra(INTENT_URL, url);
        intent.putExtra(INTENT_ENCRYPTION, encryption);
        intent.putExtra(INTENT_PROJECT_ID, project_id);
        activity.startActivity(intent);
    }

    /**
     * 공지사항 WebView
     *
     * @param activity Activity
     * @param url      URL
     * @param no       공지사항 NO
     */
    public static void startNoticeWebViewActivity(@NonNull Activity activity, @NonNull String url, @NonNull int no) {
        if (NoticeWebViewActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = NoticeWebViewActivity.class.getSimpleName();

        Intent intent = new Intent(activity, NoticeWebViewActivity.class);
        intent.putExtra(INTENT_NOTICE_URL, url);
        intent.putExtra(INTENT_NOTICE_NO, no);
        activity.startActivity(intent);
    }

    /**
     * Common Webview
     *
     * @param activity Activity
     */
    public static void startIssueEditActivity(@NonNull Activity activity, @NonNull String url,
                                              @Nullable String encryption, @Nullable int project_id) {
        if (CommonWebViewActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = CommonWebViewActivity.class.getSimpleName();

        Intent intent = new Intent(activity, CommonWebViewActivity.class);
        intent.putExtra(INTENT_URL, url);
        intent.putExtra(INTENT_ENCRYPTION, encryption);
        intent.putExtra(INTENT_PROJECT_ID, project_id);
        intent.putExtra(INTENT_VISIBLE_SHARE, false);
        intent.putExtra(INTENT_VISIBLE_TITLE, false);
        intent.putExtra(INTENT_VISIBLE_SUBTITLE, false);
        activity.startActivity(intent);
    }

    /**
     * Common Webview
     *
     * @param activity Activity
     */
    public static void startSignupActivity(@NonNull Activity activity) {
        if (CommonWebViewActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = CommonWebViewActivity.class.getSimpleName();

        HFNetworkUtil networkUtil = new HFNetworkUtil();
        Intent intent = new Intent(activity, CommonWebViewActivity.class);
        intent.putExtra(INTENT_URL, networkUtil.getWebUrlSignUp());
        intent.putExtra(INTENT_VISIBLE_SHARE, false);
        intent.putExtra(INTENT_VISIBLE_TITLE, false);
        intent.putExtra(INTENT_VISIBLE_SUBTITLE, false);
        activity.startActivity(intent);
    }

    /**
     * 간편 로그인 설정
     *
     * @param activity Activity
     */
    public static void startSetEasyLoginActivity(@NonNull Activity activity) {
        if (AuthNumberActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = AuthNumberActivity.class.getSimpleName();

        Intent intent = new Intent(activity, AuthNumberActivity.class);
        intent.putExtra(INTENT_AUTH_NUMBER_REPEAT, false);
        activity.startActivity(intent);
    }

    public static void startSetEasyLoginRepeatActivity(@NonNull Activity activity, @NonNull String password) {
        Intent intent = new Intent(activity, AuthNumberActivity.class);
        intent.putExtra(INTENT_AUTH_NUMBER_REPEAT, true);
        intent.putExtra(INTENT_AUTH_NUMBER, password);
        activity.startActivityForResult(intent, REQUEST_SET_EASYLOGIN_REPEAT);
    }


    /**
     * Main 화면 구성
     *
     * @param activity Activity
     */
    public static void startMainActivity(@NonNull Activity activity) {
//        if (MainActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = MainActivity.class.getSimpleName();

        Intent intent = new Intent(activity, MainActivity.class);
        intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivity(intent);
    }

    /**
     * TASK 상세
     *
     * @param activity Activity
     */
    public static void startTaskDetailActivity(@NonNull Activity activity, @NonNull TaskData taskData) {
        if (TaskDetailActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = TaskDetailActivity.class.getSimpleName();

        Intent intent = new Intent(activity, TaskDetailActivity.class);
        intent.putExtra(INTENT_TASK_ID, taskData.getTask_id());
        intent.putExtra(INTENT_PROJECT_ID, taskData.getProject_id());
        intent.putExtra(INTENT_TRACK_ID, taskData.getTrack_id());
        intent.putExtra(INTENT_FLAG, taskData.getFlag());
        activity.startActivityForResult(intent, REQUEST_TASK_DETAIL);
    }

    /**
     * TASK 상세
     *
     * @param activity Activity
     */
    public static void startTaskDetailActivity(@NonNull Activity activity, @NonNull int task_id, @NonNull int project_id, @Nullable int track_id) {
//        if (TaskDetailActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = TaskDetailActivity.class.getSimpleName();

        Intent intent = new Intent(activity, TaskDetailActivity.class);
        intent.putExtra(INTENT_TASK_ID, task_id);
        intent.putExtra(INTENT_PROJECT_ID, project_id);
        if (track_id > 0) intent.putExtra(INTENT_TRACK_ID, track_id);
//        intent.putExtra(INTENT_FLAG, taskData.getFlag());

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivityForResult(intent, REQUEST_TASK_DETAIL);
    }

    /**
     * Task 등록
     *
     * @param activity Activity
     */
    public static void startAddTaskActivity(@NonNull Activity activity) {
        if (AddTaskActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = AddTaskActivity.class.getSimpleName();

        Intent intent = new Intent(activity, AddTaskActivity.class);
        activity.startActivityForResult(intent, REQUEST_ADD_TASK);
    }

    /**
     * Task 등록 - Member 선택
     *
     * @param activity Activity
     */
    public static void startAddTaskSelectMemberActivity(@NonNull Activity activity) {
        if (AddTaskSelectMemberActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = AddTaskSelectMemberActivity.class.getSimpleName();

        Intent intent = new Intent(activity, AddTaskSelectMemberActivity.class);
        activity.startActivityForResult(intent, REQUEST_ADD_TASK_SELECT_MEMBER);
    }

    /**
     * Task 등록 - Campaign 선택
     *
     * @param activity Activity
     */
    public static void startAddTaskSelectCampaignActivity(@NonNull Activity activity) {
        if (AddTaskSelectCampaignActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = AddTaskSelectCampaignActivity.class.getSimpleName();

        Intent intent = new Intent(activity, AddTaskSelectCampaignActivity.class);
        activity.startActivityForResult(intent, REQUEST_ADD_TASK_SELECT_CAMPAIGN);
    }

    /**
     * Task 수정
     *
     * @param activity Activity
     */
    public static void startEditTaskActivity(@NonNull Activity activity, @NonNull int taskId, @NonNull int projectId) {
        if (AddTaskActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = AddTaskActivity.class.getSimpleName();

        Intent intent = new Intent(activity, AddTaskActivity.class);
        intent.putExtra(INTENT_IS_EDIT, true);
        intent.putExtra(INTENT_TASK_ID, taskId);
        intent.putExtra(INTENT_PROJECT_ID, projectId);
        activity.startActivityForResult(intent, REQUEST_EDIT_TASK);
    }

    /**
     * Task 수정 후 재 요청
     *
     * @param activity Activity
     */
    public static void startEditTaskAndReRequestActivity(@NonNull Activity activity, @NonNull int taskId, @NonNull int projectId) {
        if (AddTaskActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = AddTaskActivity.class.getSimpleName();

        Intent intent = new Intent(activity, AddTaskActivity.class);
        intent.putExtra(INTENT_IS_EDIT, true);
        intent.putExtra(INTENT_IS_REREQUEST, true);
        intent.putExtra(INTENT_TASK_ID, taskId);
        intent.putExtra(INTENT_PROJECT_ID, projectId);
        activity.startActivityForResult(intent, REQUEST_EDIT_TASK_REREQUEST);
    }

    /**
     * Alarm 목록
     *
     * @param activity Activity
     */
    public static void startAlarmActivity(@NonNull Activity activity) {
        if (AlarmActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = AlarmActivity.class.getSimpleName();

        Intent intent = new Intent(activity, AlarmActivity.class);
        activity.startActivity(intent);
    }

    /**
     * 검색
     *
     * @param activity Activity
     */
    public static void startSearchActivity(@NonNull Activity activity) {
        if (SearchActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = SearchActivity.class.getSimpleName();

        Intent intent = new Intent(activity, SearchActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivity(intent);
    }

    /**
     * 검색
     *
     * @param activity Activity
     */
    public static void startSearchActivity(@NonNull Activity activity, @Nullable String searchWord) {
        if (SearchActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = SearchActivity.class.getSimpleName();

        Intent intent = new Intent(activity, SearchActivity.class);
        intent.putExtra(INTENT_SEARCH_WORD, searchWord);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivity(intent);
    }

    /**
     * DASHBOARD - Project 상세
     *
     * @param activity  Activity
     * @param projectId Project ID
     */
    public static void startDashboardProjectActivity(@NonNull Activity activity, @Nullable int projectId) {
//        if (DashboardProjectActivity.class.getSimpleName().equals(LAST_TAG)) return;
        LAST_TAG = DashboardProjectActivity.class.getSimpleName();

        Intent intent = new Intent(activity, DashboardProjectActivity.class);
        intent.putExtra(INTENT_PROJECT_ID, projectId);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivity(intent);
    }
}
