package com.example.leudaemialikeme.Activity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.example.leudaemialikeme.CallAlarm;
import com.example.leudaemialikeme.Model.Clock;
import com.example.leudaemialikeme.R;

import java.util.Calendar;

import static com.example.leudaemialikeme.Activity.MdctRmdActivity.list;
import static com.example.leudaemialikeme.Activity.MdctRmdActivity.timeAdapter;

public class AddClock extends BaseActivity implements View.OnClickListener {

    private Calendar calendar;
    private TextView show_hour;
    private TextView show_minute;
    private EditText content_name;
    private EditText content_dosage;
    private Button set;
    private Button save;
    private ImageView back;
    String hourformat;
    String minuteformat;
    Clock clock = new Clock();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_clock);
        show_hour = findViewById(R.id.hour);
        show_minute = findViewById(R.id.minute);
        content_name = findViewById(R.id.content_name);
        content_dosage = findViewById(R.id.content_dosage);
        set = findViewById(R.id.set_time);
        set.setOnClickListener(this);
        save = findViewById(R.id.save);
        back = findViewById(R.id.back_list_clock);
        back.setImageResource(R.drawable.ic_back);
        back.setOnClickListener(this);
        save.setOnClickListener(this);
        calendar = Calendar.getInstance();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.set_time:
                calendar.setTimeInMillis(System.currentTimeMillis());
                int mhour = calendar.get(Calendar.HOUR_OF_DAY);
                int mminute = calendar.get(Calendar.MINUTE);
                new TimePickerDialog(AddClock.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        //calendar.setTimeInMillis(System.currentTimeMillis());
                        calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                        calendar.set(Calendar.MINUTE, minute);
                        calendar.set(Calendar.SECOND, 0);
                        calendar.set(Calendar.MILLISECOND, 0);


                        hourformat = format(hourOfDay);
                        minuteformat = format(minute);
                        Toast.makeText(AddClock.this, "" + hourformat + ":" + minuteformat, Toast.LENGTH_SHORT).show();
                        show_hour.setText(hourformat);
                        show_minute.setText(minuteformat);


                    }
                }, mhour, mminute, true).show();
                break;
            case R.id.save:
                Intent intent = new Intent(AddClock.this, CallAlarm.class);
                PendingIntent sender = PendingIntent.getBroadcast(
                        AddClock.this, 0, intent, 0);
                AlarmManager am;
                am = (AlarmManager) getSystemService(ALARM_SERVICE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    if (System.currentTimeMillis()>calendar.getTimeInMillis()+40000){
                        //加24小时
                        am.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis()+86400000, sender);
                    }else {
                        am.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
                    }
                }
                clock.setHour(hourformat);
                clock.setMinute(minuteformat);
                clock.setName("" + content_name.getText().toString());
                clock.setDosage("" + content_dosage.getText().toString());
                clock.setClockType(Clock.clock_open);
                if (clock.getHour()!=null&&clock.getMinute()!=null) {
                    clock.save();
                    list.add(clock);
                    timeAdapter.notifyDataSetChanged();
                    Log.e("Listnumber======",list.size()+"");
                    finish();
                }else {
                    Toast.makeText(this, "请选择闹钟时间", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.back_list_clock:
                finish();
                break;


        }
    }

    private String format(int x) {
        String s = "" + x;
        if (s.length() == 1) {
            s = "0" + s;
        }
        return s;
    }
}
