package org.ntlab.acanthus_client.views.login;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import org.ntlab.acanthus_client.Acanthus;
import org.ntlab.acanthus_client.R;
import org.ntlab.acanthus_client.databinding.ActivityLoginScreenBinding;
import org.ntlab.acanthus_client.views.MainActivity;
import org.ntlab.acanthus_client.views.sign_up.SignUpActivity;

//-----------------------------------------------------------------
// ログイン画面
public class LoginScreenActivity extends AppCompatActivity {

    //-----------------------------------------------------------------
    private LoginScreenViewModel loginScreenViewModel;

    //-----------------------------------------------------------------
    // ログインボタン押下時の処理
    public void onClickLogin(View view) {
        Acanthus acanthus = (Acanthus) getApplication();
        loginScreenViewModel.issueLoginToken(
                acanthus, findViewById(R.id.editTextTextLoginEmail), findViewById(R.id.editTextTextLoginPassword));
    }

    //-----------------------------------------------------------------
    // サインアップを押下
    public void onClickSignUp(View view) {
        transitionActivity(new SignUpActivity());
    }

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

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    // 初期化
    private void init() {
        setContentView(R.layout.activity_login_screen);
        startObserve();
    }

    //-----------------------------------------------------------------
    // VMの監視開始
    private void startObserve() {
        loginScreenViewModel = new ViewModelProvider(this).get(LoginScreenViewModel.class);
        loginScreenViewModel.getAcanthus().observe(this, new Observer<Acanthus>() {
            @Override
            public void onChanged(Acanthus acanthus) {
                succeedLogin();
            }
        });
    }

    //-----------------------------------------------------------------
    // ログイン成功時の処理
    private void succeedLogin() {
        transitionActivity(new MainActivity());
    }

    //-----------------------------------------------------------------
    // ログイン成功時の処理
    private void transitionActivity(AppCompatActivity appCompatActivity) {
        Intent intent = new Intent(getApplication(), appCompatActivity.getClass());
        startActivity(intent);
        finish();
    }
    //-----------------------------------------------------------------

}