package kr.co.hanwha.hifive.util;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Patterns;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;

import kr.co.hanwha.hifive.R;

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

public class HFStringUtil {
    /**
     * TASK 기한설정 화면 표시
     *
     * @param dueTime yyyy-MM-dd
     * @return Display String
     */
    public static String getDisplayDueDateTaskDetail(@NonNull String create_at, @NonNull String dueTime) {
        String display = "";
        if (TextUtils.isEmpty(create_at) == false)
            display = HFDateUtil.getChangeDateFormat(create_at, HFDateUtil.FORMAT_TASK_DUEDATE_SERVER, HFDateUtil.FORMAT_TASK_DUEDATE_DISPLAY);
        if (TextUtils.isEmpty(dueTime) == false)
            display += (TextUtils.isEmpty(display) == false ? " ~ " : "") + HFDateUtil.getChangeDateFormat(dueTime, HFDateUtil.FORMAT_TASK_DUEDATE_SERVER, HFDateUtil.FORMAT_TASK_DUEDATE_DISPLAY);

        return display;
    }

    /**
     * TASK 반복설정 화면 표시
     *
     * @param context  Context
     * @param weekLoop Bit 00000
     * @return Display String
     */
    public static String getDisplayWeekLoop(@NonNull Context context, @NonNull String weekLoop) {
        if (TextUtils.isEmpty(weekLoop) || weekLoop.length() != 7 || "0000000".equals(weekLoop))
            return context.getString(R.string.add_task_no_repeat);

        String display = "";
        if (weekLoop.length() == 5)
            weekLoop = "0" + weekLoop + "0";

        if ("1111111".equals(weekLoop)) return context.getString(R.string.add_task_everyday);
        else if ("0111110".equals(weekLoop))
            return context.getString(R.string.add_task_weekday);
        if ("1".equals(weekLoop.substring(0, 1)))
            display += (TextUtils.isEmpty(display) ? "" : " ") + context.getString(R.string.add_task_week_sunday_display);
        if ("1".equals(weekLoop.substring(1, 2)))
            display += (TextUtils.isEmpty(display) ? "" : " ") + context.getString(R.string.add_task_week_monday_display);
        if ("1".equals(weekLoop.substring(2, 3)))
            display += (TextUtils.isEmpty(display) ? "" : " ") + context.getString(R.string.add_task_week_tuesday_display);
        if ("1".equals(weekLoop.substring(3, 4)))
            display += (TextUtils.isEmpty(display) ? "" : " ") + context.getString(R.string.add_task_week_wednesday_display);
        if ("1".equals(weekLoop.substring(4, 5)))
            display += (TextUtils.isEmpty(display) ? "" : " ") + context.getString(R.string.add_task_week_thursday_display);
        if ("1".equals(weekLoop.substring(5, 6)))
            display += (TextUtils.isEmpty(display) ? "" : " ") + context.getString(R.string.add_task_week_friday_display);
        if ("1".equals(weekLoop.substring(6, 7)))
            display += (TextUtils.isEmpty(display) ? "" : " ") + context.getString(R.string.add_task_week_saturday_display);

        return display;
    }

    public static String[] extractLinks(String text) throws Exception {
        if (TextUtils.isEmpty(text)) return null;
        List<String> links = new ArrayList<>();
        Matcher m = Patterns.WEB_URL.matcher(text);
        while (m.find()) {
            String url = m.group();
            links.add(url);
        }
        return links.toArray(new String[links.size()]);
    }

    public static String getDisplayDueDateAddTask(@NonNull String dueTime) {
        if (TextUtils.isEmpty(dueTime)) return "";
        return HFDateUtil.getChangeDateFormat(dueTime, HFDateUtil.FORMAT_TASK_ADD_DUEDATE_SERVER, HFDateUtil.FORMAT_TASK_DUEDATE_DISPLAY);
    }
}
