package com.sliit.smartserviceassistant;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.icu.util.Calendar;
import android.os.Build;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileOutputStream;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class SignUp extends AppCompatActivity {


    TextView textName20;
    TextView textName21;
    TextView textName22;
    TextView textName23;
    TextView textName24;
    TextView textName25;
    TextView textName26;
    TextView text27;


    private DatePickerDialog.OnDateSetListener mDateSetListener;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        changeStatusBarColor();




        textName20 = (TextView) findViewById(R.id.editTextNameSignUp);
        textName21 = (TextView) findViewById(R.id.editTextEmailSignUp);
        textName22 = (TextView) findViewById(R.id.editTextFirstnameSignUp);
        textName23 = (TextView) findViewById(R.id.editTextLastnameSignUp);
        textName24 = (TextView) findViewById(R.id.editTextpaswordSignup);
        textName25 = (TextView) findViewById(R.id.editTextConfirmpaswordSignUp);
        textName26 = (TextView) findViewById(R.id.editTextDateOfBirthdaySignUp);

        textName26.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus) {
                    Calendar cal = Calendar.getInstance();
                    int year = cal.get(Calendar.YEAR);
                    int month = cal.get(Calendar.MONTH);
                    int day = cal.get(Calendar.DAY_OF_MONTH);

                    DatePickerDialog dialog=new DatePickerDialog(SignUp.this, android.R.style.Theme_Holo_Light_Dialog_MinWidth,mDateSetListener,year,month,day);
                    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    dialog.show();
                } else {
                    // Hide your calender here
                }
            }
        });
//        textName26.setOnClickListener(new View.OnClickListener() {
//            @RequiresApi(api = Build.VERSION_CODES.N)
//            @Override
//            public void onClick(View view) {
//
//
//            }
//        });

        mDateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                month = month + 1;
                Log.d(TAG, "onDateSet: mm/dd/yyy: " + month + "/" + day + "/" + year);

                String date = year+"-"+month+"-"+ day;
                textName26.setText(date);
            }
        };
        final Button button = findViewById(R.id.cirRegisterButton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (textName20 .getText().toString().isEmpty() || textName21.getText().toString().isEmpty() ||  textName22.getText().toString().isEmpty()||
                        textName23.getText().toString().isEmpty() ||
                        textName24.getText().toString().isEmpty() ||
                        textName25.getText().toString().isEmpty()){
//                        textName26.getText().toString().isEmpty()) {

                    System.out.println("HY1");
                    Toast.makeText(SignUp.this, "Please enter All the values", Toast.LENGTH_SHORT).show();

                    return;

                }else{
                    System.out.println("HY2");



                    postData1(textName20.getText().toString(), textName21.getText().toString(),textName22.getText().toString(),textName23.getText().toString(),textName24.getText().toString(),textName25.getText().toString(),textName26.getText().toString());

                    String FullName = textName22.getText().toString()+" "+textName23.getText().toString();
                    FileOutputStream outputStream;
                    try{
                        outputStream = openFileOutput("myfile",MODE_PRIVATE);
                        outputStream.write(FullName.getBytes());
                        outputStream.close();
                    }catch (Exception e){
                        e.printStackTrace();
                    }



                    Toast.makeText(SignUp.this, "Your Registration is sucessfull", Toast.LENGTH_SHORT).show();
                }


            }
        });


    }

    private void postData1(String username,
                            String email, String first_name,String last_name,String password,String password2,String date_of_birth
                          ){





        // builder and passing our base url
        Retrofit retrofit1 = new Retrofit.Builder()
                .baseUrl("http://192.168.1.5:8000/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();


      RetrofitAPI1 retrofitAPI1 = retrofit1.create(RetrofitAPI1.class);
////
        DataModal1 modal1 = new DataModal1(username,email,first_name,last_name,password,password2,date_of_birth);
////
        Call<DataModal1> call1 = retrofitAPI1.createPost1(modal1);
////
////
////
        call1.enqueue(new Callback<DataModal1>() {

            @Override
            public void onResponse(Call<DataModal1> call1, Response<DataModal1> response) {
                // this method is called when we get response from our api.
                Toast.makeText(SignUp.this, "Data added", Toast.LENGTH_SHORT).show();
                DataModal1 responseFromAPI1 = response.body();



            }
//
            @Override
            public void onFailure(Call<DataModal1> call1, Throwable t) {
                // setting text to our text view when
                // we get error response from API.
                Toast.makeText(SignUp.this, "Data can't added to API", Toast.LENGTH_SHORT).show();
            }
        });


    }
    private void changeStatusBarColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//            window.setStatusBarColor(Color.TRANSPARENT);
            window.setStatusBarColor(getResources().getColor(R.color.register_bk_color));


        }
    }

    public void onLoginClick(View view){
        startActivity(new Intent(this,Loging.class));
        overridePendingTransition(R.anim.slide_in_left,android.R.anim.slide_out_right);

    }



}