package net.methodyne.bellvue.system;
/**
 * Created by Methodyne GmbH.
 * User: yuri
 * Date: 1.3.2003
 * Time: 16:01
 */

import net.methodyne.bellvue.core.Sdata;
import net.methodyne.bellvue.core.ResultObject;

import java.io.*;
import java.util.Date;
import java.util.Vector;

public final class User implements Serializable {
    public long id = -1l;
    public String username = "";
    public String password = "";
    public String email = "";
    public String fullName = "";
    public String phone = "";
    public boolean active = true;
    public Date notAfter = new Date(0);
    public Date created = new Date();
    public int listlength = 20;
    public String layout = "left";
    public String style = "default.css";
    public Vector groups = new Vector();
    public Vector referenceGroups = new Vector();

    public String dispNotAfter(Sdata sd){
        return "dd.MM.yyyy";
    }

    public String dispCreated(Sdata sd){
        return "dd.MM.yyyy";
    }

    public String dispStyle(Sdata sd){
        String fl = sd.sctx.getRealPath("/css");
        String names = "";
        File css = new File(fl);
        String[] files = css.list();
        for(int i = 0 ; i < files.length; i++) if( files[i].endsWith(".css") ) names += "," + files[i];
        return "SELECT" + names;
    }

    public String dispLayout(Sdata sd){
        String fl = sd.sctx.getInitParameter("frameLayoutNames");
        return "SELECT," + fl;
    }

    public void setLayout(String l, Sdata sd){
        String[] flnames = sd.sctx.getInitParameter("frameLayoutNames").split(",");
        String[] flfiles = sd.sctx.getInitParameter("frameLayoutFiles").split(",");
        int pos;
        String file = "index1.html";
        for(pos = 0; pos < flnames.length; pos ++){
            if(flnames[pos].equals(l)){
                file = flfiles[pos];
                break;
            }
        }

        ResultObject r = new ResultObject();
        r.title = l + "-Layout";
        r.result = "Click here: <a target=\"_top\" href=\"" + file + "\"> "+ l + " </a>";
        sd.view.add(r);
        layout = l;
    }

    public String typeGroups(){  return "net.methodyne.bellvue.system.Group"; }

    public String aboutCreated(Sdata sd){return "off";}

    public String aboutGroups(Sdata sd){
        for(int i = 0 ; i < sd.user.groups.size(); i++){
            if( ((Group) sd.user.groups.elementAt(i)).groupname.equalsIgnoreCase("admin") ){
                return "on";
            }
        }
        return "off";
    }

    public String aboutActive(Sdata sd){
        return aboutGroups(sd);
    }

    public String aboutNotAfter(Sdata sd){
        return aboutGroups(sd);
    }

    public String getTitle(){
        return username;
    }

    public String dispPassword(Sdata sd){
        return "HIDDEN";
    }

    public String setListlength(String listlength, Sdata sd) {
        int ll = Integer.parseInt(listlength);
        if((ll > 2) && (ll < 3001)){
            this.listlength = ll;
            return null;
        }
        else{
            return "Listlength must be between 3 and 3000.";
        }
    }

}