package com.example.demo.module.smartbowsdk;

import com.example.demo.common.MD5Tool;
import com.example.demo.table.UserLogin;

public class SBS_LoginToken {
    private static final String slat = "LoginTokenSlatYouNotKnow";
    public int id;
    public long timestamp;
    public String sign;

    public SBS_LoginToken(String token) {
        try {
            String[] strings = token.split("-");
            id = Integer.parseInt(strings[0]);
            timestamp = Long.parseLong(strings[1]);
            sign = strings[2];
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public SBS_LoginToken(UserLogin userLogin) {
        id = userLogin.getId();
        timestamp = System.currentTimeMillis();
        sign = makeSign();
    }

    private String makeSign() {
        return MD5Tool.makeMD5(timestamp + slat + id);
    }

    public String getToken() {
        return id + "-" + timestamp + "-" + sign;
    }

    public boolean isValid() {
        boolean c1 = makeSign().equals(sign);
        boolean c2 = System.currentTimeMillis() - timestamp < 24 * 60 * 60 * 1000;
        return c1 && c2;
    }
}
