package de.sebse.fuplanner.services.KVV.types;

import android.content.Context;
import android.support.annotation.Nullable;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;

/**
 * Created by sebastian on 29.10.17.
 */

public class LoginToken implements Serializable {
    private static final long EASY_LOGIN_TIME_MILLIS = 1000 * 60 * 60 * 300;
    private static final String FILE_NAME = "LoginTokenSaving";

    private final String username;
    private final String shibsessionKey;
    private final String shibsessionName;
    private final String JSESSIONID;
    private String fullname;
    private String email;
    private long saveDate = 0;

    public LoginToken(String username, String shibsessionKey, String shibsessionName, String JSESSIONID) {
        this.username = username;
        this.shibsessionKey = shibsessionKey;
        this.shibsessionName = shibsessionName;
        this.JSESSIONID = JSESSIONID;
    }

    @Nullable
    public static LoginToken load(Context context) throws IOException, ClassNotFoundException {
        FileInputStream fis = context.openFileInput(FILE_NAME);
        ObjectInputStream is = new ObjectInputStream(fis);
        LoginToken loginToken = (LoginToken) is.readObject();
        is.close();
        fis.close();
        loginToken = (loginToken.saveDate > (System.currentTimeMillis() - EASY_LOGIN_TIME_MILLIS)) ? loginToken : null;
        return loginToken;
    }

    public void save(Context context) throws IOException {
        saveDate = System.currentTimeMillis();
        FileOutputStream fos = context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
        ObjectOutputStream os = new ObjectOutputStream(fos);
        os.writeObject(this);
        os.close();
        fos.close();
    }

    public void delete(Context context) {
        context.deleteFile(FILE_NAME);
    }

    public void setAdditionals(String fullname, String email) {
        this.fullname = fullname;
        this.email = email;
    }

    public String getUsername() {
        return username;
    }

    private String getShibsessionKey() {
        return shibsessionKey;
    }

    private String getShibsessionName() {
        return shibsessionName;
    }

    private String getJSESSIONID() {
        return JSESSIONID;
    }

    public String getFullname() {
        return fullname;
    }

    public String getEmail() {
        return email;
    }

    public HashMap<String, String> getCookies() {
        HashMap<String, String> cookies = new HashMap<>();
        cookies.put("JSESSIONID", getJSESSIONID());
        cookies.put(getShibsessionKey(), getShibsessionName());
        cookies.put("pasystem_timezone_ok", "true");
        return cookies;
    }

    public boolean isSameUser(LoginToken token) {
        return token != null && this.getUsername().equals(token.getUsername());
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        HashMap<String, String> cookies = this.getCookies();
        for (String header: cookies.keySet()) {
            result.append(header).append("=").append(cookies.get(header)).append(";");
        }
        return result.substring(0, result.length()-1);
    }
}
