package com.mob5.smarthouse

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Patterns
import android.view.View
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import com.mob5.smarthouse.common.Login
import com.mob5.smarthouse.common.MyRetrofit
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class SignUpScreen : AppCompatActivity() {
    lateinit var email: EditText
    lateinit var pass: EditText
    lateinit var name: EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sign_up_screen)

        email = findViewById(R.id.signup_email)
        pass = findViewById(R.id.signup_pass)
        name = findViewById(R.id.signup_name)
    }

    fun NewResReg(view: View) {
        if (email.text.isNotEmpty() && pass.text.isNotEmpty() && name.text.isNotEmpty()){
            if (Patterns.EMAIL_ADDRESS.matcher(email.text).matches()){
                val call = MyRetrofit.getRetrofit().register(email.text.toString(), name.text.toString(), pass.text.toString(), "5FA1B987-3890-4A87-9712-ACDEAD0173AE")
                call.enqueue(object : Callback<HashMap<String, String>> {
                    override fun onResponse(call: Call<HashMap<String, String>>, response: Response<HashMap<String, String>>) {
                        AlertDialog.Builder(this@SignUpScreen).setTitle("Успешно").setMessage("Зарегистрирован").setPositiveButton("OK", null).show()
                    }

                    override fun onFailure(call: Call<HashMap<String, String>>, t: Throwable) {
                        AlertDialog.Builder(this@SignUpScreen).setTitle("Ошибка").setMessage(t.message).setPositiveButton("OK", null).show()
                    }
                })
            }
            else AlertDialog.Builder(this).setTitle("Ошибка").setMessage("Неверный email").setPositiveButton("OK", null).show()
        }
        else AlertDialog.Builder(this).setTitle("Ошибка").setMessage("Заполните все поля").setPositiveButton("OK", null).show()
    }

    fun Back(view: View) {
        finish()
    }
}