package org.ntlab.acanthus_client.views.sign_up;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.ntlab.acanthus_client.R;
import org.ntlab.acanthus_client.databinding.ActivitySignUpBinding;
import org.ntlab.acanthus_client.entities.AccountUidJson;
import org.ntlab.acanthus_client.resources.accounts.AccountsRest;
import org.ntlab.acanthus_client.views.login.LoginScreenActivity;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

//-----------------------------------------------------------------
//
public class SignUpActivity extends AppCompatActivity {
    private ActivitySignUpBinding binding;

    //-----------------------------------------------------------------
    //
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    // サインアップ時押下時
    public void onClickSignUp(View view) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        final AccountsRest accountsRest = retrofit.create(AccountsRest.class);

        // 各フォーム取得
        String name = binding.editTextTextSignUpPersonName.getText().toString();
        String emailAddress = binding.editTextTextSignUpEmailAddress.getText().toString();
        String password = binding.editTextTextSignUpPassword.getText().toString();

        Call<AccountUidJson> call = accountsRest.createAccount(name, emailAddress, password);
        call.enqueue(new Callback<AccountUidJson>() {
            @Override
            public void onResponse(Call<AccountUidJson> call, Response<AccountUidJson> response) {
                if (response.isSuccessful()) transitionLoginActivity();
                else clearForm();
            }

            @Override
            public void onFailure(Call<AccountUidJson> call, Throwable t) {
            }
        });
    }

    //-----------------------------------------------------------------
    // アカウント所持ボタン押下処理
    public void onClickAlreadyLoggedIn(View view) {
        transitionLoginActivity();
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    // ログイン画面遷移
    private void transitionLoginActivity() {
        Intent intent = new Intent(getApplication(), LoginScreenActivity.class);
        startActivity(intent);
        finish();
    }

    //-----------------------------------------------------------------
    // 登録内容のクリア
    private void clearForm() {
        binding.editTextTextSignUpPersonName.getEditableText().clear();
        binding.editTextTextSignUpEmailAddress.getEditableText().clear();
        binding.editTextTextSignUpPassword.getEditableText().clear();
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    // 初期化
    private void init() {
        setContentView(R.layout.activity_sign_up);
        binding = ActivitySignUpBinding.inflate(getLayoutInflater());
    }
    //-----------------------------------------------------------------
}