package com.reiyu.sleepin;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseUser;
import com.parse.SaveCallback;

/**
 * Created by Satomi on 1/3/16.
 */
public class WakeUpFragment extends AppCompatActivity {
    String date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_wake_up);
        Button button = (Button) findViewById(R.id.save_record);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker);
                int day = datePicker.getDayOfMonth();
                int month = datePicker.getMonth();
                int year = datePicker.getYear();
                date = year + "/" + (month + 1) + "/" + day;

                TimePicker tp1 = (TimePicker) findViewById(R.id.go_to_bed);
                TimePicker tp2 = (TimePicker) findViewById(R.id.wake_up);
                tp1.setIs24HourView(true);
                tp1.setIs24HourView(true);
                EditText memoText = (EditText) findViewById(R.id.memo);

                int hour = tp1.getCurrentHour();
                int minute = tp1.getCurrentMinute();
                String go_to_bed_time = hour + ":" + minute;
                hour = tp2.getCurrentHour();
                minute = tp2.getCurrentMinute();
                String wake_up_time = hour + ":" + minute;
                String memo = memoText.getText().toString();

                SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(WakeUpFragment.this);
                String username = sp.getString("@string/username", null);

                if (username != null) {
                    ParseObject testObject = new ParseObject("SleepRecord");
                    testObject.put("date", date);
                    testObject.put("go_to_bed", go_to_bed_time);
                    testObject.put("wake_up", wake_up_time);
                    testObject.put("memo", memo);
                    testObject.put("username", username);
                    testObject.saveInBackground(new SaveCallback() {
                        public void done(ParseException e) {
                            if (e == null) {
                                Log.e("Sleep Record", "Successfully saved");
                                wakeUp(date);
                            } else {
                                // Sign up didn't succeed. Look at the ParseException
                                // to figure out what went wrong
                                Log.e("Sleep Record", "Error", e);
                            }
                        }
                    });
                } else {
                    Log.e("Sleep Record", "username is null");
                    Toast.makeText(WakeUpFragment.this, "User info was empty. Please Sign in again.", Toast.LENGTH_SHORT);
                    startActivity(new Intent(WakeUpFragment.this, SignInFragment.class));
                }
            }
        });
    }

    private void wakeUp(String date) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        sp.edit().putString("@string/record_updated", date).commit();
        Toast.makeText(WakeUpFragment.this, "Sleep Record is successfully saved.", Toast.LENGTH_SHORT);

        startActivity(new Intent(WakeUpFragment.this, MainActivity.class));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_sign_out) {
            signOut();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void signOut() {
        ParseUser.logOut();

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        sp.edit().putBoolean("@string/signed_in", false).commit();
        sp.edit().putString("@string/username", null).commit();
        sp.edit().putString("@string/email", null).commit();
        startActivity(new Intent(WakeUpFragment.this, SignInFragment.class));
    }
}
@@ -928,143 +928,9 @@
 d);%0A
-        button.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View v) %7B%0A        
+%0A
     
@@ -1005,32 +1005,24 @@
 r);%0A        
-      
-  
 int day = da
@@ -1051,32 +1051,24 @@
 ();%0A        
-      
-  
 int month = 
@@ -1094,32 +1094,24 @@
 ();%0A        
-     
-   
 int year = d
@@ -1127,32 +1127,24 @@
 .getYear();%0A
-        
         date
@@ -1189,32 +1189,24 @@
 y;%0A%0A        
-     
-   
 TimePicker t
@@ -1253,32 +1253,126 @@
 o_bed);%0A        
+tp1.setCurrentHour(23);%0A        tp1.setCurrentMinute(00);%0A        tp1.setIs24HourView(true);%0A%0A
         TimePick
@@ -1421,24 +1421,16 @@
 ke_up);%0A
-        
         
@@ -1423,33 +1423,33 @@
 _up);%0A        tp
-1
+2
 .setIs24HourView
@@ -1452,32 +1452,33 @@
 View(true);%0A
+%0A
         
         tp1.
@@ -1469,88 +1469,237 @@
     
-        tp1.setIs24HourView(true);%0A                EditText memoText = (EditText
+EditText memoText = (EditText) findViewById(R.id.memo);%0A%0A%0A        button.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View v) %7B%0A                TimePicker tp1 = (TimePicker
 ) fi
@@ -1714,24 +1714,28 @@
 Id(R.id.
-memo
+go_to_bed
 );%0A
-%0A
         
@@ -1880,32 +1880,107 @@
  + %22:%22 + minute;
+%0A%0A                TimePicker tp2 = (TimePicker) findViewById(R.id.wake_up);
 %0A               
@@ -2116,16 +2116,89 @@
  minute;
+%0A%0A                EditText memoText = (EditText) findViewById(R.id.memo);
 %0A       

set default time(23:00) for WakeUpFragment