package com.emato.ich.local;

import android.os.Bundle;

import com.emato.ich.MainActivity;
import com.emato.ich.utils.StringUtils;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 本地SESSION
 */
public class LocalStorage {

    private final Session SESSION = new Session();

    private LocalStorage(){}

    public static LocalStorage getInstance(){
        return LocalStorageInnerClass.INSTANCE;
    }

    public Session getSession(){
        return SESSION;
    }

    public Session setSession(Session session) {
        if (StringUtils.isNullOrEmpty(SESSION.phone) || StringUtils.isNullOrEmpty(SESSION.token)) {
            SESSION.token = session.getToken();
            SESSION.phone = session.getPhone();
            if (session.getExpiryTime() > 0) {
                SESSION.expiryTime = session.getExpiryTime();
            }
            if (session.getTime() > 0) {
                SESSION.time = session.getTime();
            }
        }
        return SESSION;
    }

    public void cleanSession(MainActivity activity){
        SESSION.phone = "";
        SESSION.token = "";
        SESSION.exception = true;
        Map<String, Bundle> bundleMap = activity.getBundleMap();
        if (activity != null && bundleMap != null && bundleMap.size() > 0) {
            bundleMap.clear();
        }
    }

    /**
     * 过期返回true, 否则返回false
     * @param session   session
     * @return          是否
     */
    public boolean isExpired(Session session) {

        long sessionTime = session.getTime();
        long currentTime = System.currentTimeMillis();
        long diff = currentTime - sessionTime;
        return diff > session.getExpiryTime() * 60 * 1000;
    }

    public static final class LocalStorageInnerClass {
        private final static LocalStorage INSTANCE = new LocalStorage();
    }

    public static class Session {

        private String phone;

        private String token;

        private long time;

        private int expiryTime = 30;

        /**
         * 新增字段 只能做一次异常处理, 点击异常处理的 异常事件按钮 后该值为false
         */
        private boolean exception = true;

        private TimeUnit timeUnit = TimeUnit.MINUTES;

        public String getPhone() {
            return phone;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token.replace("\n", "").trim();
        }

        public long getTime() {
            return time;
        }

        public void setTime(long time) {
            this.time = time;
        }

        public int getExpiryTime() {
            return expiryTime;
        }

        public void setExpiryTime(int expiryTime) {
            this.expiryTime = expiryTime;
        }

        public TimeUnit getTimeUnit() {
            return timeUnit;
        }

        public void setTimeUnit(TimeUnit timeUnit) {
            this.timeUnit = timeUnit;
        }

        public boolean getException() {
            return exception;
        }

        public void setException(boolean exception) {
            this.exception = exception;
        }
    }
}
