/**
 * This file is part of the Meeds project (https://meeds.io/).
 *
 * Copyright (C) 2020 - 2025 Meeds Association contact@meeds.io
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.exoplatform.services.organization.impl;

import java.util.Date;
import java.util.Objects;

import org.exoplatform.services.organization.ExtendedCloneable;
import org.exoplatform.services.organization.OrganizationService;
import org.exoplatform.services.organization.User;

public class UserImpl implements User, ExtendedCloneable {

  /**
   * The serial version UID
   */
  private static final long serialVersionUID = 6919266039776618161L;

  private String            id;

  private String            userName;

  private String            password;

  private String            firstName;

  private String            lastName;

  private String            email;

  private Date              createdDate;

  private Date              lastLoginTime;

  private String            organizationId;

  private String            displayName;

  private Boolean           enabled;

  private String            originatingStore;

  private String            creationSource;

  private boolean           automaticDeactivation;

  public UserImpl() {
  }

  public UserImpl(String username) {
    this.userName = username;
  }

  public String getDisplayName() {
    return displayName != null ? displayName : getFirstName() + " " + getLastName();
  }

  public void setDisplayName(String displayName) {
    this.displayName = displayName;
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getUserName() {
    return userName;
  }

  public void setUserName(String name) {
    this.userName = name;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  // wrapper method
  public String getFullName() {
    return getDisplayName();
  }

  public void setFullName(String fullName) {
    setDisplayName(fullName);
  }

  public Date getCreatedDate() {
    return createdDate;
  }

  public void setCreatedDate(Date t) {
    createdDate = t;
  }

  public Date getLastLoginTime() {
    return lastLoginTime;
  }

  public void setLastLoginTime(Date t) {
    lastLoginTime = t;
  }

  // toString
  public String toString() {
    return "User[" + id + "|" + userName + "]" + (organizationId == null ? "" : ("@" + organizationId));
  }

  public String getOrganizationId() {
    return organizationId;
  }

  public void setOrganizationId(String organizationId) {
    this.organizationId = organizationId;
  }

  /**
   * @return <code>true</code> if the user is enabled, <code>false</code>
   *         otherwise
   */
  public boolean isEnabled() {
    return enabled == null || enabled.booleanValue();
  }

  /**
   * Set it to <code>true</code> or <code>null</code> to enable the user,
   * <code>false</code> otherwise
   */
  public void setEnabled(Boolean enabled) {
    this.enabled = enabled;
  }

  /**
   * Set originating store name (internal or external)
   *
   * @param originatingStore
   */
  @Override
  public void setOriginatingStore(String originatingStore) {
    this.originatingStore = originatingStore;
  }

  /**
   * @return originating store name (internal or external)
   */
  @Override
  public String getOriginatingStore() {
    return originatingStore;
  }

  /**
   * @return true if the user was initially added to internal store
   */
  @Override
  public boolean isInternalStore() {
    return originatingStore == null || OrganizationService.INTERNAL_STORE.equals(originatingStore);
  }

  @Override
  public String getCreationSource() {
    return creationSource;
  }

  @Override
  public void setCreationSource(String creationSource) {
    this.creationSource = creationSource;
  }

  @Override
  public boolean isAutomaticDeactivation() {
    return automaticDeactivation;
  }

  @Override
  public void setAutomaticDeactivation(boolean automaticDeactivation) {
    this.automaticDeactivation = automaticDeactivation;
  }

  @Override
  public UserImpl clone() { // NOSONAR
    UserImpl ui;
    try {
      ui = (UserImpl) super.clone();
      if (createdDate != null) {
        ui.createdDate = (Date) createdDate.clone();
      }
      if (lastLoginTime != null) {
        ui.lastLoginTime = (Date) lastLoginTime.clone();
      }
    } catch (CloneNotSupportedException e) {
      return this;
    }
    return ui;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o)
      return true;
    if (o == null || getClass() != o.getClass())
      return false;
    UserImpl user = (UserImpl) o;
    return Objects.equals(id, user.id)
           && Objects.equals(userName, user.userName)
           && Objects.equals(password, user.password)
           && Objects.equals(firstName, user.firstName)
           && Objects.equals(lastName, user.lastName)
           && Objects.equals(email, user.email)
           && Objects.equals(createdDate, user.createdDate)
           && Objects.equals(lastLoginTime, user.lastLoginTime)
           && Objects.equals(organizationId, user.organizationId)
           && Objects.equals(displayName, user.displayName)
           && Objects.equals(enabled, user.enabled);
  }

  @Override
  public int hashCode() {
    return Objects.hash(id,
                        userName,
                        password,
                        firstName,
                        lastName,
                        email,
                        createdDate,
                        lastLoginTime,
                        organizationId,
                        displayName,
                        enabled);
  }
}
