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 kr.co.hanwha.hifive.data.model.TaskData;
import kr.co.hanwha.hifive.data.source.TaskDataRepository;
import kr.co.hanwha.hifive.screen.alarm.AlarmActivity;
import kr.co.hanwha.hifive.screen.search.SearchActivity;
import kr.co.hanwha.hifive.screen.task.AddTaskActivity;
import kr.co.hanwha.hifive.screen.task.AddTaskSelectMemberActivity;
import kr.co.hanwha.hifive.screen.main.MainActivity;
import kr.co.hanwha.hifive.screen.task.TaskDetailActivity;

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

public class HFActivityUtil {
    public static final int ADD_TASK_SELECT_MEMBER = 1011;
    public static final int ADD_TASK = 1031;
    public static final int EDIT_TASK = 1032;
    public static final int REQUEST_TASK_DETAIL_MODIFY = 1041;

    public static final String INTENT_IS_EDIT = "is_edit";
    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 String LAST_TAG = "";

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

    /**
     * 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);
    }

    /**
     * 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);
        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_MODIFY);
    }

    /**
     * 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, 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, ADD_TASK_SELECT_MEMBER);
    }

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

        TaskDataRepository.setTaskData(taskData);
        Intent intent = new Intent(activity, AddTaskActivity.class);
        intent.putExtra(INTENT_IS_EDIT, true);
        activity.startActivityForResult(intent, EDIT_TASK);
    }

    /**
     * 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);
    }
}
