/*
 * Copyright (c) 2010-2013 Evolveum
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.evolveum.midpoint.common.security;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.lang.Validate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.evolveum.midpoint.util.DebugDumpable;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.Dumpable;
import com.evolveum.midpoint.util.MiscUtil;
import com.evolveum.midpoint.xml.ns._public.common.common_2a.ActivationStatusType;
import com.evolveum.midpoint.xml.ns._public.common.common_2a.ActivationType;
import com.evolveum.midpoint.xml.ns._public.common.common_2a.CredentialsType;
import com.evolveum.midpoint.xml.ns._public.common.common_2a.UserType;
import com.evolveum.prism.xml.ns._public.types_2.PolyStringType;

/**
 * @author semancik
 *
 */
public class MidPointPrincipal implements UserDetails, Dumpable, DebugDumpable {
	
	private static final long serialVersionUID = 8299738301872077768L;
    private UserType user;
    private Collection<Authorization> authorizations = new ArrayList<Authorization>();
    private ActivationStatusType effectiveActivationStatus;

    public MidPointPrincipal(UserType user) {
        Validate.notNull(user, "User must not be null.");
        this.user = user;
    }

	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#getAuthorities()
	 */
	@Override
	public Collection<Authorization> getAuthorities() {
		return authorizations;
	}

	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#getPassword()
	 */
	@Override
	public String getPassword() {
		// We won't return password
		return null;
	}

	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#getUsername()
	 */
	@Override
	public String getUsername() {
		return getUser().getName().getOrig();
	}

	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonExpired()
	 */
	@Override
	public boolean isAccountNonExpired() {
		// TODO
		return true;
	}

	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonLocked()
	 */
	@Override
	public boolean isAccountNonLocked() {
		// TODO
		return true;
	}

	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#isCredentialsNonExpired()
	 */
	@Override
	public boolean isCredentialsNonExpired() {
		// TODO
		return true;
	}

	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#isEnabled()
	 */
	@Override
	public boolean isEnabled() {
        if (effectiveActivationStatus == null) {
            effectiveActivationStatus = createEffectiveActivationStatus();
        }
		return effectiveActivationStatus == ActivationStatusType.ENABLED;
	}

    private ActivationStatusType createEffectiveActivationStatus() {
        //todo improve

        CredentialsType credentials = user.getCredentials();
        if (credentials == null || credentials.getPassword() == null){
            return ActivationStatusType.DISABLED;
        }

        if (user.getActivation() == null) {
            return ActivationStatusType.DISABLED;
        }

        ActivationType activation = user.getActivation();
        
        if (activation.getAdministrativeStatus() != null) {
            return activation.getAdministrativeStatus();
        }
        
        long time = System.currentTimeMillis();
        if (activation.getValidFrom() != null) {
            long from = MiscUtil.asDate(activation.getValidFrom()).getTime();
            if (time < from) {
                return ActivationStatusType.DISABLED;
            }
        }
        if (activation.getValidTo() != null) {
            long to = MiscUtil.asDate(activation.getValidTo()).getTime();
            if (to < time) {
                return ActivationStatusType.DISABLED;
            }
        }
        
        if (activation.getAdministrativeStatus() == null) {
            return ActivationStatusType.DISABLED;
        }

        return activation.getAdministrativeStatus();
    }

	public UserType getUser() {
        return user;
    }

    public PolyStringType getName() {
        return getUser().getName();
    }

    public String getFamilyName() {
        PolyStringType string = getUser().getFamilyName();
        return string != null ? string.getOrig() : null;
    }

    public String getFullName() {
        PolyStringType string = getUser().getFullName();
        return string != null ? string.getOrig() : null;
    }

    public String getGivenName() {
        PolyStringType string = getUser().getGivenName();
        return string != null ? string.getOrig() : null;
    }

    public String getOid() {
        return getUser().getOid();
    }

	/* (non-Javadoc)
	 * @see com.evolveum.midpoint.util.Dumpable#dump()
	 */
	@Override
	public String dump() {
		return debugDump();
	}

	/* (non-Javadoc)
	 * @see com.evolveum.midpoint.util.DebugDumpable#debugDump()
	 */
	@Override
	public String debugDump() {
		return debugDump(0);
	}

	/* (non-Javadoc)
	 * @see com.evolveum.midpoint.util.DebugDumpable#debugDump(int)
	 */
	@Override
	public String debugDump(int indent) {
		StringBuilder sb = new StringBuilder();
		DebugUtil.debugDumpLabel(sb, "MidPointPrincipal", indent);
		sb.append("\n");
		DebugUtil.debugDumpWithLabel(sb, "User", user.asPrismObject(), indent + 1);
		sb.append("\n");
		DebugUtil.debugDumpWithLabel(sb, "Authorizations", authorizations, indent + 1);
		return sb.toString();
	}

	@Override
	public String toString() {
		return "MidPointPrincipal(" + user + ", autz=" + authorizations + ")";
	}

}

@@ -4553,72 +4553,8 @@
    %0A
-        if (activation.getAdministrativeStatus() == null) %7B%0A    
     
@@ -4589,82 +4589,17 @@
 ype.
-DIS
+EN
 ABLED;%0A
-        %7D%0A%0A        return activation.getAdministrativeStatus();%0A
     

fix for user login.