package jp.co.y_net.amm.dao;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;

import jp.co.y_net.amm.AppSession.LoginUser;
import jp.co.y_net.amm.common.AppLogger;
import jp.co.y_net.amm.common.AppUtils;
import jp.co.y_net.amm.common.CalcHash;

import org.apache.commons.lang.StringUtils;


public class Usr extends AppDto implements Serializable {
	private static final long serialVersionUID = 1L;
	
	

    private Integer usrid;
    private String loginid;
    private String name;
    private Integer kind;
    private Integer orgid;
    private Integer orgrole;
    private Integer stoplogin;
    private Integer fromdate;
    private Integer todate;
    private String pwhash;
    private String cres;
    private Long cresdate;
    private Long joindate;
    private Long lastlogintrydate;
    private Integer loginerrorcount;
    private Long lastlogindate;
    private Long modpwdate;
    private Long leavedate;
    private Integer status;
    private Long requestlogoutdate;
    
    /* 自動生成のアクセサ - start */
    public Integer getUsrid() {
        return usrid;
    }
    public void setUsrid(Integer usrid) {
        this.usrid = usrid;
    }
    public String getLoginid() {
        return loginid;
    }
    public void setLoginid(String loginid) {
        this.loginid = loginid;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getKind() {
        return kind;
    }
    public void setKind(Integer kind) {
        this.kind = kind;
    }
    public Integer getOrgid() {
        return orgid;
    }
    public void setOrgid(Integer orgid) {
        this.orgid = orgid;
    }
    public Integer getOrgrole() {
        return orgrole;
    }
    public void setOrgrole(Integer olgrole) {
        this.orgrole = olgrole;
    }
    public Integer getStoplogin() {
        return stoplogin;
    }
    public void setStoplogin(Integer stoplogin) {
        this.stoplogin = stoplogin;
    }
    public Integer getFromdate() {
        return fromdate;
    }
    public void setFromdate(Integer fromdate) {
        this.fromdate = fromdate;
    }
    public Integer getTodate() {
        return todate;
    }
    public void setTodate(Integer todate) {
        this.todate = todate;
    }
    public String getPwhash() {
        return pwhash;
    }
    public void setPwhash(String pwhash) {
        this.pwhash = pwhash;
    }
    public String getCres() {
        return cres;
    }
    public void setCres(String cres) {
        this.cres = cres;
    }
    public Long getCresdate() {
        return cresdate;
    }
    public void setCresdate(Long cresdate) {
        this.cresdate = cresdate;
    }
    public Long getJoindate() {
        return joindate;
    }
    public void setJoindate(Long joindate) {
        this.joindate = joindate;
    }
    public Long getLastlogintrydate() {
        return lastlogintrydate;
    }
    public void setLastlogintrydate(Long lastlogintrydate) {
        this.lastlogintrydate = lastlogintrydate;
    }
    public Integer getLoginerrorcount() {
        return loginerrorcount;
    }
    public void setLoginerrorcount(Integer loginerrorcount) {
        this.loginerrorcount = loginerrorcount;
    }
    public Long getLastlogindate() {
        return lastlogindate;
    }
    public void setLastlogindate(Long lastlogindate) {
        this.lastlogindate = lastlogindate;
    }
    public Long getModpwdate() {
        return modpwdate;
    }
    public void setModpwdate(Long modpwdate) {
        this.modpwdate = modpwdate;
    }
    public Long getLeavedate() {
        return leavedate;
    }
    public void setLeavedate(Long leavedate) {
        this.leavedate = leavedate;
    }
    public Integer getStatus() {
        return status;
    }
    public void setStatus(Integer status) {
        this.status = status;
    }
    public Long getRequestlogoutdate() {
        return requestlogoutdate;
    }
    public void setRequestlogoutdate(Long requestlogoutdate) {
        this.requestlogoutdate = requestlogoutdate;
    }    
    /* 自動生成のアクセサ - end */


    /**
     * LoginUserオブジェクトへの値コピー
     */
    public void copy(LoginUser loginUser) {
        loginUser.setUsrid(this.getUsrid());
        loginUser.setLoginid(this.getLoginid());
        loginUser.setKind(this.getKind());
        loginUser.setOrgid(this.getOrgid());
        loginUser.setOrgrole(this.getOrgrole());
    }
    
    public static final String 管理者初期パスワード = "testtest";
    public static Usr create運営管理者代表(Org org運営管理){
        return create運営管理者(org運営管理, null, null, true);
    }
    public static Usr create運営管理者(Org org運営管理, String loginid, String name, boolean is管理者代表) {
        int role;
        if(is管理者代表) {
            role = ROLE_管理者;
            loginid = "admin";
            name = "運営管理者";
        } else {
            role = ROLE_通常会員;
        }
        Usr usr運営管理者 = new Usr();
        usr運営管理者.setLoginid(loginid);
        usr運営管理者.setName(name);
        usr運営管理者.setKind(Usr.KIND_運営管理者);
        usr運営管理者.setOrgrole(role);
        usr運営管理者.setOrgid(org運営管理.getOrgid());
        usr運営管理者.setStatus(Usr.STATUS_登録済み);
        usr運営管理者.setStoplogin(AppDef.FLASE);
        String pwhash = CalcHash.passowrdHash(管理者初期パスワード, usr運営管理者.getLoginid()); // 初期パスワード
        usr運営管理者.setPwhash(pwhash);
        usr運営管理者.setModpwdate(AppUtils.createNowLong());
        
        return usr運営管理者;
    }
    
    
   
    
    /* 会員種別 */
    public static final int KIND_機関会員 = 1;
    public static final int KIND_機関管理者 = 2;
    public static final int KIND_運営管理者 = 3;
    
    /* 所属機関での役割 */
    public static final Integer ROLE_管理者 = 1;
    public static final Integer ROLE_通常会員 = 9;
    
    /* 状態 */
    public static final int STATUS_仮登録 = 0;
    public static final int STATUS_登録済み = 1;
    public static final int STATUS_退会 = 2;
    
    /**
     * @return 状態の名称
     */
    public String getStatus_Disp() {
        switch (status) {
        case STATUS_仮登録:
            return "仮登録";
        case STATUS_登録済み:
            return "登録済み";
        case STATUS_退会:
            return "退会";
        default:
            return String.valueOf(status);
        }
    }
    
    /** @return 種類の名称 */
    public String getKind_Disp() {
        switch (kind) {
        case KIND_機関会員:
            return "機関会員";
        case KIND_機関管理者:
            return "機関管理者";
        case KIND_運営管理者:
            return "運営管理者";
        default:
            return String.valueOf(status);
        }
    }
    public String getLastlogindate_Disp() {
        String lastlogin;
        try {
            final DateFormat DF = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            Date d = AppUtils.toDate(this.getLastlogindate());
            if(d == null) {
                lastlogin = "未ログイン";
            } else {
                lastlogin = DF.format(d);    
            }
            
        } catch (Exception e) {
            AppLogger.error(e);
            if (this.getLastlogindate() != null) {
                lastlogin = String.valueOf(this.getLastlogindate());
            } else {
                lastlogin = "";
            };
        }
        return lastlogin;
    }
    /**
     * @return 代表者の時はTrue
     */
    public boolean isAdmin() {
        return ROLE_管理者.equals(getKind());
    }

    public boolean isログイン停止() {
        if(this.stoplogin != null && this.stoplogin == 1) {
            return true;
        }
        return false;
    }
    
    /**
     * ・会員情報編集画面からの退会
     * ・登録会員情報削除によるカスケード退会
     */
    public void 退会処理() {
        this.setLeavedate(AppUtils.createNowLong());
        this.setStatus(Usr.STATUS_退会);
    }

    /**
     * 最終ログイン日時のソートに用いるComparator
     */
    public static class LastlogindateComparator implements Comparator<Usr> {
        /** trueに変更すると新しい順 */
        private boolean desc = false; 
        
        public LastlogindateComparator(boolean desc) {
            this.desc = desc;
        }
        
        @Override
        public int compare(Usr o1, Usr o2) {
            int reverse = 1;
            if(desc) {
                reverse = -1;
            }
            
            Long n1 = 0L;
            Long n2 = 0L;
            if(o1 != null && o1.getLastlogindate() != null) n1 = o1.getLastlogindate();
            if(o2 != null && o2.getLastlogindate() != null) n2 = o2.getLastlogindate();
            
            return Long.compare(n1, n2) * reverse;
        }
    }

    public String getName_Disp() {
        String str = StringUtils.abbreviate(getName(), 100);
        return str;
    }
    public String getLoginid_Disp() {
        String str = StringUtils.abbreviate(getLoginid(), 30);
        return str;
    }
    
}
