package com.example.nemophila;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.example.nemophila.entities.AccountJson;
import com.example.nemophila.resources.AccountsRest;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
@JsonIgnoreProperties(ignoreUnknown=true)
public class LoginActivity extends AppCompatActivity {
private final ExecutorService executor = Executors.newSingleThreadExecutor();
private Retrofit retrofit;
private AccountsRest AccountsRest;
private Nemophila nemophila;
//座標指定
private double defaultLat = 38.74;
private double defaultLong = 137.26;
private float defaultZoom = 5f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//Nemophilaと連携
nemophila = (Nemophila) this.getApplication();
//通信の初期化
this.retrofit = new Retrofit.Builder()
.baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/nemophila/")
.addConverterFactory(JacksonConverterFactory.create())
.build();
this.AccountsRest = retrofit.create(AccountsRest.class);
//Loginボタンを押したとき
findViewById(R.id.LoginButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//IDとpwの入力
EditText edituid = (EditText) findViewById(R.id.editTextUserID);
String uid = edituid.getText().toString();
EditText editpw = (EditText) findViewById(R.id.editTextTextNewPassword);
String pw = editpw.getText().toString();
// 通信
Call<AccountJson> call = AccountsRest.getAccounts(uid,pw);
call.enqueue (new Callback<AccountJson>() {
@Override
public void onResponse(Call<AccountJson> call, Response<AccountJson> response) {
if (response.isSuccessful()) {
System.out.println("通信成功:changeBelongsAndMaster");
//nemophila(自端末)にname,id,tokenを記憶
nemophila.setName(response.body().getName());
nemophila.setUid(response.body().getId());
nemophila.setToken(response.body().getToken());
//座標指定(仮)
nemophila.setCameraLatitude(defaultLat);
nemophila.setCameraLongitude(defaultLong);
nemophila.setZoom(defaultZoom);
//mainに画面遷移
getIcon("http://nitta-lab-www.is.konan-u.ac.jp/nemophila-data/icon" + nemophila.getUid() + ".jpg", false);
} else {
System.out.println("通信可能:changeBelongsAndMaster: " + response.code());
if (response.code() == 500){
((TextView) findViewById(R.id.textView)).setText("Invalid UserID or Password");
} else if (response.code() == 404){
((TextView) findViewById(R.id.textView)).setText("Login failed. Please enter the correct credentials.");
}
}
}
@Override
public void onFailure(Call<AccountJson> call, Throwable t) {
System.out.println("通信失敗:changeBelongsAndMaster");
System.out.println(t);
}
});
}
});
//SignUpボタンを押したとき
findViewById(R.id.SignUp).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(LoginActivity.this,SignUpActivity.class);
startActivity(intent);
}
});
//ForgotPasswordボタンを押したとき(処理未記入)
findViewById(R.id.ForgotPW).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Intent intent = new Intent(LoginActivity.this, ChangePwActivity.class);
// startActivity(intent);
}
});
}
private void getIcon(String urlString, boolean isDefault){
executor.execute(() -> {
try{
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.connect();
InputStream is = con.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
nemophila.setIcon(bitmap);
Intent intent = new Intent(LoginActivity.this,MapsActivity.class);
startActivity(intent);
} catch (Exception e){
e.printStackTrace();
if(!isDefault) {
getIcon("http://nitta-lab-www.is.konan-u.ac.jp/nemophila-data/test01.jpg", true);
} else {
Intent intent = new Intent(LoginActivity.this,MapsActivity.class);
startActivity(intent);
}
}
});
}
}