package org.ntlab.acanthus_client;

import android.app.Application;
import android.content.SharedPreferences;
import android.graphics.drawable.shapes.OvalShape;

import androidx.core.math.MathUtils;

import org.ntlab.acanthus_client.entities.Animation;
import org.ntlab.acanthus_client.entities.AnimationJson;

import java.util.ArrayList;
import java.util.Collection;

//-----------------------------------------------------------------
// ユーザーアカウント
public class Acanthus extends Application {
    //-----------------------------------------------------------------
    private Integer preferenceUid;
    private String preferenceName;
    private String preferenceEmail;
    private String preferencePassword;
    private String preferenceToken;
    //-----------------------------------------------------------------
    private Integer aid;
    private Integer editingPageNo = 1;
    private Integer editingPageId = 0;
    private Animation currentAnimation;
    private Collection<AnimationJson> animationJsonList = new ArrayList<>();

    //-----------------------------------------------------------------
    // getter
    public Integer getPreferenceUid() {
        if (preferenceUid == null) {
            SharedPreferences preferences = getSharedPreferences("prefData", MODE_PRIVATE);
            preferenceUid = preferences.getInt("uid", 0);
        }
        return preferenceUid;
    }

    public String getPreferenceName() {
        if (preferenceName == null) {
            SharedPreferences preferences = getSharedPreferences("prefData", MODE_PRIVATE);
            preferenceName = preferences.getString("name", "");
        }
        return preferenceName;
    }

    public String getPreferenceEmail() {
        if (preferenceEmail == null) {
            SharedPreferences preferences = getSharedPreferences("prefData", MODE_PRIVATE);
            preferenceEmail = preferences.getString("email", "");
        }
        return preferenceEmail;
    }

    public String getPreferencePassword() {
        if (preferencePassword == null) {
            SharedPreferences preferences = getSharedPreferences("prefData", MODE_PRIVATE);
            preferencePassword = preferences.getString("password", "");
        }
        return preferencePassword;
    }

    public String getPreferenceToken() {
        if (preferenceToken == null) {
            SharedPreferences preferences = getSharedPreferences("prefData", MODE_PRIVATE);
            preferenceToken = preferences.getString("token", "");
        }
        return preferenceToken;
    }

    public Integer getAid() {
        if (aid == null) {
            SharedPreferences preferences = getSharedPreferences("prefData", MODE_PRIVATE);
            aid = preferences.getInt("aid", 0);
        }
        return aid;
    }

    public Integer getEditingPageNo() {
        if (editingPageNo == null) editingPageNo = 1;
        return editingPageNo;
    }

    public Integer getEditingPageId() {
        return this.editingPageId;
    }

    public Animation getCurrentAnimation() {
        return currentAnimation;
    }

    public Collection<AnimationJson> getAnimationJsonList() {
        return animationJsonList;
    }

    //-----------------------------------------------------------------
    // setter
    public void setPreferenceUid(Integer preferenceUid) {
        SharedPreferences preferences = getSharedPreferences("prefData", MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt("uid", preferenceUid);
        editor.commit();
    }

    public void setPreferenceName(String preferenceName) {
        SharedPreferences preferences = getSharedPreferences("prefData", MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("name", preferenceName);
        editor.commit();
    }

    public void setPreferenceEmail(String preferenceEmail) {
        SharedPreferences preferences = getSharedPreferences("prefData", MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("email", preferenceEmail);
        editor.commit();
    }

    public void setPreferencePassword(String preferencePassword) {
        SharedPreferences preferences = getSharedPreferences("prefData", MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("password", preferencePassword);
        editor.commit();
    }

    public void setPreferenceToken(String preferenceToken) {
        SharedPreferences preferences = getSharedPreferences("prefData", MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("token", preferenceToken);
        editor.commit();
    }

    public void setAid(Integer aid) {
        SharedPreferences preferences = getSharedPreferences("prefData", MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt("aid", aid);
        this.aid = aid;
        editor.commit();
    }

    public void setEditingPageNo(Integer editingPageNo) {
        this.editingPageNo = MathUtils.clamp(editingPageNo, 1, 999);
    }

    public void setCurrentAnimation(Animation currentAnimation) {
        this.currentAnimation = currentAnimation;
    }

    public void setEditingPageId(Integer editingPageId) {
        this.editingPageId = editingPageId;
    }

    //HomeViewModelでしようするためのsetter
    public void setAnimationJsonList(Collection<AnimationJson> sentAnimationJsonList) {
        this.animationJsonList.clear();
        this.animationJsonList.addAll(sentAnimationJsonList);
    }

    //-----------------------------------------------------------------
    //
    public void addAnimationJsonList(AnimationJson animationJson) {
        this.animationJsonList.add(animationJson);
    }
    //-----------------------------------------------------------------
    //-----------------------------------------------------------------


}
