package ca.wmcd.routiner.ui;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.LinearLayout;

import java.util.Calendar;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.InjectViews;
import butterknife.OnClick;
import ca.wmcd.routiner.R;
import ca.wmcd.routiner.data.Routine;
import ca.wmcd.routiner.data.RoutineDatabase;
import ca.wmcd.routiner.data.RoutineNotifierService;

/**
 * Created by WalrusCow on 3/15/15.
 * View to create a new routine
 */
public class RoutineCreatorView extends LinearLayout {
    @InjectViews(
            {R.id.weekday_sunday, R.id.weekday_monday, R.id.weekday_tuesday, R.id.weekday_wednesday,
             R.id.weekday_thursday, R.id.weekday_friday, R.id.weekday_saturday})
    List<WeekdaySelectorView> weekdayViews;

    @InjectView(R.id.routine_goal_input) EditText goalInput;

    public RoutineCreatorView(Context context) {
        super(context);
        init(context);
    }

    public RoutineCreatorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public RoutineCreatorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public RoutineCreatorView(Context context, AttributeSet attrs, int defStyleAttr,
                              int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        inflate(context, R.layout.view_routine_creator, this);
        ButterKnife.inject(this);
    }

    @OnClick(R.id.create_routine)
    public void createRoutine() {
        Routine routine = new Routine();
        routine.weekdayMask = 0;
        for (WeekdaySelectorView weekdayView : weekdayViews) {
            if (weekdayView.isSelected()) {
                routine.setWeekdaySelected(weekdayView.weekday, true);
            }
        }
        routine.timeMin = 60 * 8;
        routine.goal = goalInput.getText().toString();

        Context context = getContext();

        RoutineDatabase.save(context, routine);

        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        PendingIntent intent = PendingIntent.getService(
                context, 1, new Intent(context, RoutineNotifierService.class), PendingIntent.FLAG_CANCEL_CURRENT);

        // Five seconds from now
        long wakeTime = Calendar.getInstance().getTimeInMillis() + 5000;
        am.set(AlarmManager.RTC_WAKEUP, wakeTime, intent);
    }
}

@@ -42,120 +42,22 @@
 oid.
-app.AlarmManager;%0Aimport android.app.PendingIntent;%0Aimport android.content.Context;%0Aimport android.content.Inten
+content.Contex
 t;%0Ai
@@ -162,35 +162,8 @@
 t;%0A%0A
-import java.util.Calendar;%0A
 impo
@@ -370,16 +370,16 @@
 outine;%0A
+
 import c
@@ -419,61 +419,8 @@
 ase;
-%0Aimport ca.wmcd.routiner.data.RoutineNotifierService;
 %0A%0A/*
@@ -1965,96 +1965,357 @@
   %7D%0A
-        routine.timeMin = 60 * 8;%0A        routine.goal = goalInput.getText().toString();
+%0A        if (routine.weekdayMask == 0) %7B%0A            // No days set%0A            // TODO: Notify user%0A            return;%0A        %7D%0A%0A        routine.timeMin = 60 * 8;%0A        routine.goal = goalInput.getText().toString();%0A        if (routine.goal.equals(%22%22)) %7B%0A            // No goal set%0A            // TODO: Notify user%0A            return;%0A        %7D
 %0A%0A  
@@ -2352,17 +2352,16 @@
 text();%0A
-%0A
         
@@ -2397,24 +2397,26 @@
  routine);%0A%0A
+//
         Alar
@@ -2493,16 +2493,18 @@
 RVICE);%0A
+//
         
@@ -2552,16 +2552,18 @@
 ervice(%0A
+//
         
@@ -2669,17 +2669,21 @@
 RRENT);%0A
-%0A
+//%0A//
         
@@ -2707,16 +2707,18 @@
 rom now%0A
+//
         
@@ -2782,16 +2782,18 @@
 + 5000;%0A
+//
         

Check routine creation inputs