package com.brilliance.mda.support.td;

import com.brilliance.mda.runtime.mda.IAuthInfo;
import com.brilliance.mda.runtime.mda.IStream;
import com.brilliance.mda.runtime.mda.impl.StreamImpl;

import java.util.List;

public class TDAuthInfo implements IAuthInfo {
    private String userId;
    private IStream sysStream;
    private IStream ddsStream;
    private IStream keepAuthInfo;

    public TDAuthInfo(){}

    public TDAuthInfo(String userId) {
        this.userId = userId;
        sysStream = new StreamImpl();
        ddsStream = new StreamImpl();
        keepAuthInfo = new StreamImpl();
    }

    @Override
    public String getUserId() {
        return userId;
    }

    @Override
    public IStream getSysStream() {
        return sysStream;
    }

    @Override
    public IStream getDdsStream() {
        return ddsStream;
    }

    public IStream getKeepAuthInfo() {
        return keepAuthInfo;
    }

    public void recoveryAuthInfo(){
        List<String> keyRows = this.getKeepAuthInfo().getRows();
        List<String> sysRows = this.getSysStream().getRows();
        for(int i=0;i< keyRows.size();i+=2){
            String key = keyRows.get(i);
            String value = keyRows.get(i+1);
            int idx = sysRows.indexOf(key);
            if(idx>=0){
                sysRows.set(idx+1,value);
            }
            else{
                sysRows.add(key);
                sysRows.add(value);
            }
        }
    }
}
