package ch.pizzalemon.android.helper;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import ch.pizzalemon.android.R;
import ch.pizzalemon.android.activity.BaseActivity;

/**
 * Created by cimenmus on 04/10/2017.
 */

public class DateTimeHelper {

    public static long getMillisFromDateString(String dateString){

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = null;
        try {
            Date date = formatter.parse(dateString);
            calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar.getTimeInMillis();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static String getFormattedDateFromDateString(String dateString){

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = null;
        try {
            Date date = formatter.parse(dateString);
            calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar.get(Calendar.DAY_OF_MONTH) + " " +
                    getMonthName(calendar.get(Calendar.MONTH)) + " " +
                    calendar.get(Calendar.YEAR);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    private static String getMonthName(int monthIndex){
        switch (monthIndex){
            case 0:
                return BaseActivity.currentActivity.getString(R.string.month_name_january);
            case 1:
                return BaseActivity.currentActivity.getString(R.string.month_name_february);
            case 2:
                return BaseActivity.currentActivity.getString(R.string.month_name_march);
            case 3:
                return BaseActivity.currentActivity.getString(R.string.month_name_april);
            case 4:
                return BaseActivity.currentActivity.getString(R.string.month_name_may);
            case 5:
                return BaseActivity.currentActivity.getString(R.string.month_name_jun);
            case 6:
                return BaseActivity.currentActivity.getString(R.string.month_name_july);
            case 7:
                return BaseActivity.currentActivity.getString(R.string.month_name_august);
            case 8:
                return BaseActivity.currentActivity.getString(R.string.month_name_september);
            case 9:
                return BaseActivity.currentActivity.getString(R.string.month_name_october);
            case 10:
                return BaseActivity.currentActivity.getString(R.string.month_name_november);
            case 11:
                return BaseActivity.currentActivity.getString(R.string.month_name_december);
            default:
                return "";
        }
    }
}
