package com.example.tampopo_client;

import android.app.Application;
import android.content.SharedPreferences;

public class Tampopo extends Application {
  //いらないので下記2つをコメントアウトしました
  //public String getUserId;

  //public String getToken;
  private String token;
  private String userId;
  private String password;
  private String chatroomId;
  private String chatFriendId;
  private String mailaddress;
  private String nickname;
  private String icon;

  @Override
  public void onCreate(){
    super.onCreate();
    loadUserData();
  }


  public String getToken() {return token;}

  public void setToken(String token) {
    this.token = token;
    saveUserData();
  }

  public String getUserId() {
    return userId;
  }

  public void setUserId(String userId) {
    this.userId = userId;
    saveUserData();
  }

  public String getMailaddress() { return mailaddress; }

  public void setMailaddress(String mailaddress) {
    this.mailaddress = mailaddress;
    saveUserData();
  }



  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
    saveUserData();
  }

  public String getChatroomId() {
    return chatroomId;
  }

  public void setChatroomId(String ChatroomId) {
    this.chatroomId = ChatroomId;
    saveUserData();
  }
  public String getChatFriendId() {
    return chatFriendId;
  }

  public void setChatFriendId(String ChatFriendId) {
    this.chatFriendId = ChatFriendId;
    saveUserData();
  }


  public String getNickname() { return nickname; }

  public void setNickname(String nickname) {
    this.nickname = nickname;
    saveUserData();
  }

  public String getIcon() { return icon; }

  public void setIcon(String icon) {
    this.icon = icon;
    saveUserData();
  }

  // 永続的にデータを保存する
  private void saveUserData() {
    SharedPreferences prefs = getSharedPreferences("user_data", MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("token", token);
    editor.putString("userId", userId);
    editor.putString("password", password);
    editor.putString("chatroomId", chatroomId);
    editor.putString("chatFriendId", chatFriendId);
    editor.putString("mailaddress", mailaddress);
    editor.putString("nickname", nickname);
    editor.putString("icon", icon);
    editor.apply(); // 非同期で保存
  }

  // 起動時にデータを読み込む
  private void loadUserData() {
    SharedPreferences prefs = getSharedPreferences("user_data", MODE_PRIVATE);
    token = prefs.getString("token", "");
    userId = prefs.getString("userId", "");
    password = prefs.getString("password", "");
    chatroomId = prefs.getString("chatroomId", "");
    chatFriendId = prefs.getString("chatFriendId", "");
    mailaddress = prefs.getString("mailaddress", "");
    nickname = prefs.getString("nickname", "");
    icon = prefs.getString("icon", "");
  }
}
