package com.example.leudaemialikeme.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.example.leudaemialikeme.Model.Event;
import com.example.leudaemialikeme.R;

import org.litepal.LitePal;

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

public class AddEventActivity extends BaseActivity {

    public static final String EVENT_DAY = "event_day";
    public static final String EVENT_MONTH= "event_month";
    public static final String EVENT_INFO = "event_info";
    public static final String EVENT_PLACE = "event_place";
    public static final String EVENT_DOCTOR_NAME = "event_doctor_name";
    public static final String EVENT_TIME = "event_time";
    public static final String EVENT_CREATE_TIME = "event_create_time";
    private TextView event_day,event_month,event_info;
    private TextView event_place,event_doctor_name,event_time,event_create_time;
    private String show_event_day,show_event_month,show_event_info;
    private String show_event_place,show_event_doctor_name,show_event_time,show_event_create_time;
    private ImageView delete,save;
    private Button add_date,add_time;
    private ImageView back;

    DateFormat format= DateFormat.getDateTimeInstance();
    Calendar calendar= Calendar.getInstance(Locale.CHINA);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_event);
        event_day = findViewById(R.id.add_event_day);
        event_month = findViewById(R.id.add_event_month);
        event_info = findViewById(R.id.add_event_info);
        event_place = findViewById(R.id.add_event_place);
        event_doctor_name = findViewById(R.id.add_event_doctor_name);
        event_time = findViewById(R.id.add_event_time);
        event_create_time = findViewById(R.id.show_time);
        delete = findViewById(R.id.delete_event);
        save = findViewById(R.id.save_event);
        add_date = findViewById(R.id.add_date);
        add_time = findViewById(R.id.add_time);
        back = findViewById(R.id.back_event_list);

        Intent intent = getIntent();
        show_event_day = intent.getStringExtra(EVENT_DAY);
        show_event_month = intent.getStringExtra(EVENT_MONTH);
        show_event_info = intent.getStringExtra(EVENT_INFO);
        show_event_place = intent.getStringExtra(EVENT_PLACE);
        show_event_doctor_name = intent.getStringExtra(EVENT_DOCTOR_NAME);
        show_event_time = intent.getStringExtra(EVENT_TIME);
        show_event_create_time = intent.getStringExtra(EVENT_CREATE_TIME);

        event_day.setText(show_event_day);
        event_month.setText(show_event_month);
        event_info.setText(show_event_info);
        event_place.setText(show_event_place);
        event_doctor_name.setText(show_event_doctor_name);
        event_time.setText(show_event_time);
        event_create_time.setText(show_event_create_time);

        //点击事项
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddEventActivity.this,EventActivity.class);
                startActivity(intent);
            }
        });


        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String deleteContent=event_info.getText().toString();
                LitePal.deleteAll(Event.class,"event_info=?",deleteContent);
                finish();
            }
        });

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //创建时间不为空，表明为修改
                if(show_event_create_time!=null){
                    String inputDay = event_day.getText().toString();
                    String inputMonth = event_month.getText().toString();
                    String inputInfo = event_info.getText().toString();
                    String inputPlace = event_place.getText().toString();
                    String inputName = event_doctor_name.getText().toString();
                    String inputTime = event_time.getText().toString();
                    Event  event = new Event();
                    event.setEvent_day(inputDay);
                    event.setEvent_month(inputMonth);
                    event.setEvent_info(inputInfo);
                    event.setEvent_place(inputPlace);
                    event.setEvent_doctor_name(inputName);
                    event.setEvent_time(inputTime);
//                    LitePal.where("ownerNetId = ?", String.valueOf(ownerNetId)).find(Friend.class);
                    event.updateAll("event_create_time=?",show_event_create_time);
                    finish();
                }else{
                    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                    Date date=new Date(System.currentTimeMillis());
                    String inputDay = event_day.getText().toString();
                    String inputMonth = event_month.getText().toString();
                    String inputInfo = event_info.getText().toString();
                    String inputPlace = event_place.getText().toString();
                    String inputName = event_doctor_name.getText().toString();
                    String inputTime = event_time.getText().toString();
                    Event  event = new Event();
                    event.setEvent_day(inputDay);
                    event.setEvent_month(inputMonth);
                    event.setEvent_info(inputInfo);
                    event.setEvent_place(inputPlace);
                    event.setEvent_doctor_name(inputName);
                    event.setEvent_time(inputTime);
                    event.setEvent_create_time(simpleDateFormat.format(date));
                    if(inputInfo.equals(LitePal.find(Event.class,3))){
                        Toast.makeText(AddEventActivity.this,"该大事记已存在",Toast.LENGTH_LONG).show();
                        finish();
                    }
                    event.save();
                    Toast.makeText(AddEventActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
                    finish();//操作完成结束当前活动
                }
            }
        });

        //获取选择的月，日
        add_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDatePickerDialog(AddEventActivity.this,  4, event_month,event_day, calendar);;
            }
        });

        //获取选择的时间
        add_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showTimePickerDialog(AddEventActivity.this,  4, event_time, calendar);
            }
        });
    }

    public static void showTimePickerDialog(Activity activity,int themeResId, final TextView event_time, Calendar calendar)  {
        new TimePickerDialog( activity,themeResId,
                // 绑定监听器
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//                        event_time.setText("您选择了：" + hourOfDay + "时" + minute  + "分");
                        event_time.setText(hourOfDay+"时"+minute+"分");
                    }
                }
                // 设置初始时间
                , calendar.get(Calendar.HOUR_OF_DAY)
                , calendar.get(Calendar.MINUTE)
                // true表示采用24小时制
                ,true).show();
    }


    public static void showDatePickerDialog(Activity activity, int themeResId, final TextView event_month,final TextView event_day, Calendar calendar)  {
        new DatePickerDialog(activity, themeResId, new DatePickerDialog.OnDateSetListener() {
            // 绑定监听器(How the parent is notified that the date is set.)
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                // 此处得到选择的时间，可以进行你想要的操作
//                tv.setText("您选择了：" + year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                event_day.setText(String.valueOf(dayOfMonth));
                event_month.setText(String.valueOf(monthOfYear+1));
            }
        }
                // 设置初始日期
                , calendar.get(Calendar.YEAR)
                , calendar.get(Calendar.MONTH)
                , calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

}
