package com.example.ui;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DigitalClock;
import android.widget.TimePicker;

public class MainActivity extends Activity {
 private GregorianCalendar mCalendar; // 설정 일시
 private AlarmManager mManager; // 알람 메니저
 private TimePicker tp; // 알람설정하는 타임피크
 //알림
// private static final String NOTIFY_KEY_1 = "NOTIFY_KEY_1";
 private static final int NOTIFY_2 = 0x1002;
    private NotificationManager notifier = null; 
 
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
    
    
     //현재시각 취득
     mCalendar = new GregorianCalendar();
     Log.i("현재 시간을 취득 합니다", mCalendar.getTime().toString());
     //알람 매니저를 취득
     mManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    
     // notification 취득
     notifier = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    
     final Notification notify = new Notification();
    
     //상태바 아이콘으로 표시할 아이콘의 리소스 ID
     notify.icon = R.drawable.icon;
    
     //알림이 뜰 때 상태바에 일시적으로 표시될 텍스트
     notify.tickerText = "알람이 설정 되었습니다.!";
    
     //알림에 표시될 발생 시각
     notify.when = System.currentTimeMillis();
    
////////////////////////////////////////////////////////////////////////////////
      // 디지털 시계 리스너 생성
     DigitalClock dt = (DigitalClock)findViewById(R.id.DigitalClock01);
        dt.setTextColor(Color.MAGENTA);
     
     // 타임 피크 리스너 생성
     TimePicker tp = (TimePicker)findViewById(R.id.TimePicker01);
////////////////////////////////////////////////////////////////////////////////
     Button alSet = (Button)findViewById(R.id.AlSet);
     Button alReset = (Button)findViewById(R.id.Alreset);
     Button viewSet = (Button)findViewById(R.id.ViewSet);
     
     //알람 설정 버튼
     alSet.setOnClickListener(new View.OnClickListener() {
   
   public void onClick(View v) {
    setAlarm();
   }
  });
     
     
     //알람 취소 버튼
     alReset.setOnClickListener(new View.OnClickListener() {
   
   public void onClick(View v) {
    resetAlarm();
   }
  });
     
     
     // notification 설정
     viewSet.setOnClickListener(new View.OnClickListener() {
   
   public void onClick(View v) {
   
    Notification notify = new Notification(
      R.drawable.icon,
      "요리의 알람이 설정 되었습니다!", System.currentTimeMillis());
    
    notify.flags = Notification.FLAG_AUTO_CANCEL;
    
    long[] vibrates = new long[]{200, 200, 600, 600, 600, 200,
      200, 600, 600, 200, 200, 200, 200, 600, 200, 200,
      600, 200, 200, 600, 600, 200, 600, 200, 600, 600,
      200, 200, 200, 600, 600, 200, 200, 200, 200, 600};
    
    notify.vibrate = vibrates;
    
    Intent toLaunch = new Intent(MainActivity.this, MainActivity.class);
    PendingIntent intentBack = PendingIntent.getActivity(MainActivity.this
      , 0, toLaunch, 0);
    
    notify.setLatestEventInfo(MainActivity.this
      , "음식요리법(알람)", "알람을 설정 하셨습니다.", intentBack);
    
    notifier.notify(NOTIFY_2, notify);
    
    Vibrator vibe = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
    
    vibe.vibrate(500);
    
   }
  });
     
    
////////////////////////////////////////////////////////////////////////////////
   
     tp.setCurrentHour(mCalendar.get(Calendar.HOUR_OF_DAY));
     tp.setCurrentMinute(mCalendar.get(Calendar.MINUTE));
     
     
          
  }
////////////////////////////////////////////////////////////////////////////////
//알람 설정 할때 시간 되면 인텐트 되도록
 private PendingIntent pendingIntent(){

////////이부분 말입니다. 이부분에서 if문을써서 타임피크로 설정한 시간일 때 뭔가 인텐트를하든 뭘 하든 하고 싶은데
////////어떻게 if문을 써야 할까요 ㅠㅠㅠㅠㅠㅠㅠㅠ
  Intent intent = new Intent(getApplicationContext(), test.class);
  PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
    return pi;
    }

 
 //알람 설정
 private void setAlarm() {
  mManager.set(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), pendingIntent());
   Log.i("HelloAlarmActivity", mCalendar.getTime().toString());
  
 }
 
 //알람 취소
 private void resetAlarm() {
    mManager.cancel(pendingIntent());
   }

////////////////////////////////////////////////////////////////////////////// 
  //일자 설정 클래스의 상태변화 리스너
  public void onDateChanged (DatePicker view, int year, int monthOfYear, int dayOfMonth) {
   mCalendar.set (year, monthOfYear, dayOfMonth, tp.getCurrentHour(), tp.getCurrentMinute());
   Log.i("HelloAlarmActivity", mCalendar.getTime().toString());
   }
/*
  //시각 설정 클래스의 상태변화 리스너
  public void onTimeChanged (TimePicker view, int hourOfDay, int minute) {
   mCalendar.set (mDate.getYear(), mDate.getMonth(), mDate.getDayOfMonth(), hourOfDay, minute);
   Log.i("HelloAlarmActivity",mCalendar.getTime().toString());
  }
*/
 
 }