package com.symbol.elearningapp;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

import com.symbol.elearningapp.Utils.AppPreference;
import com.symbol.elearningapp.Utils.Constant;
import com.symbol.elearningapp.service.ClassDetail;
import com.symbol.elearningapp.service.RetrofitClient;
import com.symbol.elearningapp.service.ServiceApi;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ClassAddActivity extends AppCompatActivity {
    Calendar myCalendar;
    EditText etdate, etclasstime, etsubject;
    Button addBtn;
    ServiceApi serviceApi;
    AppPreference appPreference;
    ProgressDialog progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class_add);
        myCalendar = Calendar.getInstance();

        serviceApi = RetrofitClient.getApiClient(Constant.baseUrl.BASE_URL).create(ServiceApi.class);
        appPreference = new AppPreference(this);
        etdate = (EditText) findViewById(R.id.etdate);
        etclasstime = (EditText) findViewById(R.id.etclasstime);
        etsubject = (EditText) findViewById(R.id.etsubject);
        addBtn = findViewById(R.id.btn_add);
        DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {

                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateLabel();
            }

        };

        etdate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(ClassAddActivity.this, date, myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });

        etclasstime.setOnClickListener(new View.OnClickListener() {
            final Calendar c = Calendar.getInstance();
            int mHour = c.get(Calendar.HOUR_OF_DAY);
            int mMinute = c.get(Calendar.MINUTE);

            @Override
            public void onClick(View v) {
                TimePickerDialog timePickerDialog = new TimePickerDialog(ClassAddActivity.this,
                        new TimePickerDialog.OnTimeSetListener() {

                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay,
                                                  int minute) {

                                etclasstime.setText(hourOfDay + ":" + String.format("%02d", minute));
                            }
                        }, mHour, mMinute, true);
                timePickerDialog.show();
            }

        });
        addBtn.setOnClickListener(view -> {
            addClassdetails();
        });
    }

    public void addClassdetails() {
        progress = new ProgressDialog(this);
        progress.setTitle("Please wait");
        progress.setCancelable(false);
        progress.show();
        String Subject = etsubject.getText().toString();
        String Date = etdate.getText().toString();
        String time = etclasstime.getText().toString();
        if (TextUtils.isEmpty(Subject)) {
            appPreference.showToast("Subject name is required.");
        } else if (TextUtils.isEmpty(Date)) {
            appPreference.showToast("Please select the date");
        } else if (TextUtils.isEmpty(time)) {
            appPreference.showToast("Please select the time");
        } else {
            int id = Integer.parseInt(CommonData.getClassDetails().get(CommonData.getClassDetails().size() - 1).getId()) + 1;
            Call<ClassDetail> userCall = serviceApi.addClassDetail(new ClassDetail("" + id, Subject, Date, time, null));
            userCall.enqueue(new Callback<ClassDetail>() {
                @Override
                public void onResponse(Call<ClassDetail> call, Response<ClassDetail> response) {

                    if (!response.equals("")) {
                        progress.dismiss();
                        appPreference.showToast("Added Sucessfully!!");
                        stopActivity();
                    } else {
                        progress.dismiss();
                        appPreference.showToast("Server Error occured!!");
                    }
                }

                @Override
                public void onFailure(Call<ClassDetail> call, Throwable t) {
                    progress.dismiss();
                    appPreference.showToast(t.getMessage());
                }

            });
        }

    }

    private void updateLabel() {
        String myFormat = "MM/dd/yy";
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
        etdate.setText(sdf.format(myCalendar.getTime()));
    }

    private void stopActivity() {
        this.finish();
    }
}