<?php

declare(strict_types=1);


/**
 * Circles - Bring cloud-users closer together.
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Maxence Lange <maxence@artificial-owl.com>
 * @copyright 2021
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


namespace OCA\Circles\Model;

use daita\MySmallPhpTools\Db\Nextcloud\nc22\INC22QueryRow;
use daita\MySmallPhpTools\Exceptions\InvalidItemException;
use daita\MySmallPhpTools\IDeserializable;
use daita\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Deserialize;
use daita\MySmallPhpTools\Traits\TArrayTools;
use DateTime;
use JsonSerializable;
use OCA\Circles\AppInfo\Capabilities;
use OCA\Circles\Exceptions\MemberNotFoundException;
use OCA\Circles\Exceptions\ParseMemberLevelException;
use OCA\Circles\Exceptions\UserTypeNotFoundException;
use OCA\Circles\IFederatedUser;
use OCA\Circles\IMemberships;


/**
 * Class Member
 *
 * @package OCA\Circles\Model
 */
class Member extends ManagedModel implements
	IMemberships,
	IFederatedUser,
	IDeserializable,
	INC22QueryRow,
	JsonSerializable {


	use TArrayTools;
	use TNC22Deserialize;


	const LEVEL_NONE = 0;
	const LEVEL_MEMBER = 1;
	const LEVEL_MODERATOR = 4;
	const LEVEL_ADMIN = 8;
	const LEVEL_OWNER = 9;

	const TYPE_SINGLE = 0;
	const TYPE_USER = 1;
	const TYPE_GROUP = 2;
	const TYPE_MAIL = 4;
	const TYPE_CONTACT = 8;
	const TYPE_CIRCLE = 16;
	const TYPE_APP = 10000;
	const APP_CIRCLES = 10001;


	public static $TYPE = [
		0  => 'single',
		1  => 'user',
		2  => 'group',
		4  => 'mail',
		8  => 'contact',
		16 => 'circle',
		10000 => 'app'
	];

	/**
	 * Note: When editing those values, update lib/Application/Capabilities.php
	 *
	 * @see Capabilities::generateConstantsMember()
	 */
	const STATUS_INVITED = 'Invited';
	const STATUS_REQUEST = 'Requesting';
	const STATUS_MEMBER = 'Member';
	const STATUS_BLOCKED = 'Blocked';


	/**
	 * Note: When editing those values, update lib/Application/Capabilities.php
	 *
	 * @see Capabilities::generateConstantsMember()
	 * @var array
	 */
	public static $DEF_LEVEL = [
		1 => 'Member',
		4 => 'Moderator',
		8 => 'Admin',
		9 => 'Owner'
	];


	public static $DEF_TYPE_MAX = 31;


	/** @var string */
	private $id = '';

	/** @var string */
	private $circleId = '';

	/** @var string */
	private $singleId = '';

	/** @var string */
	private $userId = '';

	/** @var int */
	private $userType = 0;

	/** @var Circle */
	private $basedOn;

	/** @var Member */
	private $inheritanceFrom;

	/** @var FederatedUser */
	private $inheritedBy;

	/** @var string */
	private $instance = '';

	/** @var bool */
	private $local = false;

	/** @var int */
	private $level = 0;

	/** @var string */
	private $status = 'Unknown';

	/** @var string */
	private $note = '';

	/** @var string */
	private $displayName = '';

	/** @var int */
	private $displayUpdate = 0;

	/** @var string */
	private $contactId = '';

	/** @var string */
	private $contactMeta = '';

	/** @var Circle */
	private $circle;

	/** @var int */
	private $joined = 0;


	/** @var Member[] */
	private $members = null;

	/** @var Member[] */
	private $inheritedMembers = null;

	/** @var bool */
	private $detailedInheritedMember = false;

	/** @var Membership[] */
	private $memberships = null;


	/**
	 * Member constructor.
	 */
	public function __construct() {
	}


	/**
	 * @param string $id
	 *
	 * @return $this
	 */
	public function setId(string $id): self {
		$this->id = $id;

		return $this;
	}

	/**
	 * @return string
	 */
	public function getId(): string {
		return $this->id;
	}


	/**
	 * @param string $circleId
	 *
	 * @return Member
	 */
	public function setCircleId(string $circleId): self {
		$this->circleId = $circleId;

		return $this;
	}

	/**
	 * @return string
	 */
	public function getCircleId(): string {
		return $this->circleId;
	}


	/**
	 * This should replace user_id, user_type and instance; and will use the data from Circle with
	 * Config=CFG_SINGLE
	 *
	 * @param string $singleId
	 *
	 * @return $this
	 */
	public function setSingleId(string $singleId): self {
		$this->singleId = $singleId;

		return $this;
	}

	/**
	 * @return string
	 */
	public function getSingleId(): string {
		return $this->singleId;
	}


	/**
	 * @param string $userId
	 *
	 * @return Member
	 */
	public function setUserId(string $userId): self {
		$this->userId = $userId;
		if ($this->displayName === '') {
			$this->displayName = $userId;
		}

		return $this;
	}

	/**
	 * @return string
	 */
	public function getUserId(): string {
		return $this->userId;
	}


	/**
	 * @param int $userType
	 *
	 * @return Member
	 */
	public function setUserType(int $userType): self {
		$this->userType = $userType;

		return $this;
	}

	/**
	 * @return int
	 */
	public function getUserType(): int {
		return $this->userType;
	}


//	/**
//	 * @param int $source
//	 *
//	 * @return self
//	 */
//	public function setSource(int $source): self {
//		$this->source = $source;
//
//		return $this;
//	}
//
//	/**
//	 * @return int
//	 */
//	public function getSource(): int {
//		return $this->source;
//	}
//

	/**
	 * @param string $instance
	 *
	 * @return Member
	 */
	public function setInstance(string $instance): self {
		$this->instance = $instance;

		return $this;
	}

	/**
	 * @return string
	 */
	public function getInstance(): string {
		return $this->instance;
	}


	/**
	 * @return bool
	 */
	public function hasBasedOn(): bool {
		return !is_null($this->basedOn);
	}

	/**
	 * @param Circle|null $basedOn
	 *
	 * @return $this
	 */
	public function setBasedOn(?Circle $basedOn): self {
		$this->basedOn = $basedOn;

		return $this;
	}

	/**
	 * @return Circle|null
	 */
	public function getBasedOn(): ?Circle {
		return $this->basedOn;
	}


	/**
	 * @param FederatedUser|null $inheritedBy
	 *
	 * @return $this
	 */
	public function setInheritedBy(?FederatedUser $inheritedBy): self {
		$this->inheritedBy = $inheritedBy;

		return $this;
	}

	/**
	 * @return FederatedUser|null
	 */
	public function getInheritedBy(): ?FederatedUser {
		return $this->inheritedBy;
	}


	/**
	 * @param Member|null $inheritanceFrom
	 *
	 * @return $this
	 */
	public function setInheritanceFrom(?Member $inheritanceFrom): self {
		$this->inheritanceFrom = $inheritanceFrom;

		return $this;
	}

	/**
	 * @return Member|null
	 */
	public function getInheritanceFrom(): ?Member {
		return $this->inheritanceFrom;
	}


	/**
	 * @param bool $local
	 *
	 * @return Member
	 */
	public function setLocal(bool $local): self {
		$this->local = $local;

		return $this;
	}

	/**
	 * @return bool
	 */
	public function isLocal(): bool {
		return $this->local;
	}


	/**
	 * @param int $level
	 *
	 * @return Member
	 */
	public function setLevel(int $level): self {
		$this->level = $level;

		return $this;
	}

	/**
	 * @return int
	 */
	public function getLevel(): int {
		return $this->level;
	}


	/**
	 * @param string $status
	 *
	 * @return Member
	 */
	public function setStatus(string $status): self {
		$this->status = $status;

		return $this;
	}

	/**
	 * @return string
	 */
	public function getStatus(): string {
		return $this->status;
	}


	/**
	 * @param string $note
	 *
	 * @return Member
	 */
	public function setNote(string $note): self {
		$this->note = $note;

		return $this;
	}

	/**
	 * @return string
	 */
	public function getNote(): string {
		return $this->note;
	}


	/**
	 * @param string $displayName
	 *
	 * @return Member
	 */
	public function setDisplayName(string $displayName): self {
		if ($displayName !== '') {
			$this->displayName = $displayName;
		}

		return $this;
	}


	/**
	 * @param int $displayUpdate
	 *
	 * @return Member
	 */
	public function setDisplayUpdate(int $displayUpdate): self {
		$this->displayUpdate = $displayUpdate;

		return $this;
	}

	/**
	 * @return int
	 */
	public function getDisplayUpdate(): int {
		return $this->displayUpdate;
	}


	/**
	 * @return string
	 */
	public function getDisplayName(): string {
		return $this->displayName;
	}


	/**
	 * @param string $contactId
	 *
	 * @return Member
	 */
	public function setContactId(string $contactId): self {
		$this->contactId = $contactId;

		return $this;
	}

	/**
	 * @return string
	 */
	public function getContactId(): string {
		return $this->contactId;
	}


	/**
	 * @param string $contactMeta
	 *
	 * @return Member
	 */
	public function setContactMeta(string $contactMeta): self {
		$this->contactMeta = $contactMeta;

		return $this;
	}

	/**
	 * @return string
	 */
	public function getContactMeta(): string {
		return $this->contactMeta;
	}


	/**
	 * @param Circle $circle
	 *
	 * @return self
	 */
	public function setCircle(Circle $circle): self {
		$this->circle = $circle;

		return $this;
	}

	/**
	 * @return Circle
	 */
	public function getCircle(): Circle {
		return $this->circle;
	}

	/**
	 * @return bool
	 */
	public function hasCircle(): bool {
		return (!is_null($this->circle));
	}


	/**
	 * @param int $joined
	 *
	 * @return Member
	 */
	public function setJoined(int $joined): self {
		$this->joined = $joined;

		return $this;
	}

	/**
	 * @return int
	 */
	public function getJoined(): int {
		return $this->joined;
	}


	/**    /**
	 * @param array $members
	 *
	 * @return self
	 */
	public function setMembers(array $members): IMemberships {
		$this->members = $members;

		return $this;
	}

	/**
	 * @return array
	 */
	public function getMembers(): array {
		if (is_null($this->members)) {
			$this->getManager()->getMembers($this);
		}

		return $this->members;
	}


	/**
	 * @param array $members
	 * @param bool $detailed
	 *
	 * @return self
	 */
	public function setInheritedMembers(array $members, bool $detailed): IMemberships {
		$this->inheritedMembers = $members;
		$this->detailedInheritedMember = $detailed;

		return $this;
	}

	/**
	 * @param bool $detailed
	 *
	 * @return array
	 */
	public function getInheritedMembers(bool $detailed = false): array {
		if (is_null($this->inheritedMembers)
			|| ($detailed && !$this->detailedInheritedMember)) {
			$this->getManager()->getInheritedMembers($this, $detailed);
		}

		return $this->inheritedMembers;
	}


	/**
	 * @param array $memberships
	 *
	 * @return self
	 */
	public function setMemberships(array $memberships): IMemberships {
		$this->memberships = $memberships;

		return $this;
	}

	/**
	 * @return Membership[]
	 */
	public function getMemberships(): array {
		if (is_null($this->memberships)) {
			$this->getManager()->getMemberships($this);
		}

		return $this->memberships;
	}




//	/**
//	 * @param array $memberships
//	 *
//	 * @return self
//	 */
//	public function setMemberships(array $memberships): self {
//		$this->members = $memberships;
//
//		return $this;
//	}
//
//	/**
//	 * @return array
//	 */
//	public function getMemberships(): array {
//		if (is_null($this->members)) {
//			$this->getManager()->getMemberships($this);
//		}
//
//		return $this->members;
//	}


	/**
	 * @param Member $member
	 * @param bool $full
	 *
	 * @return bool
	 */
	public function compareWith(Member $member, bool $full = true): bool {
		if ($this->getId() !== $member->getId()
			|| $this->getCircleId() !== $member->getCircleId()
			|| $this->getSingleId() !== $member->getSingleId()
			|| $this->getUserId() !== $member->getUserId()
			|| $this->getUserType() <> $member->getUserType()
			|| $this->getInstance() !== $member->getInstance()) {
			return false;
		}

		if ($full
			&& ($this->getLevel() <> $member->getLevel()
				|| $this->getStatus() !== $member->getStatus())) {
			return false;
		}

		return true;
	}


	/**
	 * @param array $data
	 *
	 * @return $this
	 * @throws InvalidItemException
	 */
	public function import(array $data): IDeserializable {
		if ($this->get('userId', $data) === '') {
			throw new InvalidItemException();
		}

		$this->setId($this->get('id', $data));
		$this->setCircleId($this->get('circleId', $data));
		$this->setSingleId($this->get('singleId', $data));
		$this->setUserId($this->get('userId', $data));
		$this->setUserType($this->getInt('userType', $data));
		$this->setInstance($this->get('instance', $data));
		$this->setLocal($this->getBool('local', $data));
		$this->setLevel($this->getInt('level', $data));
		$this->setStatus($this->get('status', $data));
		$this->setDisplayName($this->get('displayName', $data));
		$this->setDisplayUpdate($this->getInt('displayUpdate', $data));
		$this->setNote($this->get('note', $data));
		$this->setContactId($this->get('contactId', $data));
		$this->setContactMeta($this->get('contactMeta', $data));
		$this->setJoined($this->getInt('joined', $data));

		try {
			/** @var Circle $circle */
			$circle = $this->deserialize($this->getArray('circle', $data), Circle::class);
			$this->setCircle($circle);
		} catch (InvalidItemException $e) {
		}

		try {
			/** @var Circle $circle */
			$circle = $this->deserialize($this->getArray('basedOn', $data), Circle::class);
			$this->setBasedOn($circle);
		} catch (InvalidItemException $e) {
		}

		try {
			/** @var FederatedUSer $inheritedBy */
			$inheritedBy = $this->deserialize($this->getArray('inheritedBy', $data), Membership::class);
			$this->setInheritedBy($inheritedBy);
		} catch (InvalidItemException $e) {
		}

		return $this;
	}


	/**
	 * @param array $data
	 * @param string $prefix
	 *
	 * @return INC22QueryRow
	 * @throws MemberNotFoundException
	 */
	public function importFromDatabase(array $data, string $prefix = ''): INC22QueryRow {
		if ($this->get($prefix . 'single_id', $data) === '') {
			throw new MemberNotFoundException();
		}

		$this->setId($this->get($prefix . 'member_id', $data));
		$this->setCircleId($this->get($prefix . 'circle_id', $data));
		$this->setSingleId($this->get($prefix . 'single_id', $data));
		$this->setUserId($this->get($prefix . 'user_id', $data));
		$this->setUserType($this->getInt($prefix . 'user_type', $data));
		$this->setInstance($this->get($prefix . 'instance', $data));
		$this->setLevel($this->getInt($prefix . 'level', $data));
		$this->setStatus($this->get($prefix . 'status', $data));
		$this->setDisplayName($this->get($prefix . 'cached_name', $data));
		$this->setNote($this->get($prefix . 'note', $data));
		$this->setContactId($this->get($prefix . 'contact_id', $data));
		$this->setContactMeta($this->get($prefix . 'contact_meta', $data));

		$cachedUpdate = $this->get($prefix . 'cached_update', $data);
		if ($cachedUpdate !== '') {
			$this->setDisplayUpdate(DateTime::createFromFormat('Y-m-d H:i:s', $cachedUpdate)->getTimestamp());
		}

		$joined = $this->get($prefix . 'joined', $data);
		if ($joined !== '') {
			$this->setJoined(DateTime::createFromFormat('Y-m-d H:i:s', $joined)->getTimestamp());
		}

		if ($this->getInstance() === '') {
			$this->setLocal(true);
			$this->setInstance($this->get('_params.local', $data));
		}

		$this->getManager()->manageImportFromDatabase($this, $data, $prefix);

		return $this;
	}


	/**
	 * @return string[]
	 */
	public function jsonSerialize(): array {
		$arr = [
			'id'            => $this->getId(),
			'circleId'      => $this->getCircleId(),
			'singleId'      => $this->getSingleId(),
			'userId'        => $this->getUserId(),
			'userType'      => $this->getUserType(),
			'basedOn'       => $this->getBasedOn(),
			'instance'      => $this->getInstance(),
			'local'         => $this->isLocal(),
			'level'         => $this->getLevel(),
			'status'        => $this->getStatus(),
			'displayName'   => $this->getDisplayName(),
			'displayUpdate' => $this->getDisplayUpdate(),
			'note'          => $this->getNote(),
			'contactId'     => $this->getContactId(),
			'contactMeta'   => $this->getContactMeta(),
			'joined'        => $this->getJoined()
		];

		if (!is_null($this->getInheritedBy())) {
			$arr['inheritedBy'] = $this->getInheritedBy();
		}

		if (!is_null($this->getInheritanceFrom())) {
			$arr['inheritanceFrom'] = $this->getInheritanceFrom();
		}

		if ($this->hasCircle()) {
			$arr['circle'] = $this->getCircle();
		}

		if (!is_null($this->members)) {
			$arr['members'] = $this->getMembers();
		}

		if (!is_null($this->inheritedMembers)) {
			$arr['inheritedMembers'] = $this->getInheritedMembers();
		}

		if (!is_null($this->memberships)) {
			$arr['memberships'] = $this->getMemberships();
		}


		return $arr;
	}


	/**
	 * @param int $level
	 *
	 * @return int
	 * @throws ParseMemberLevelException
	 */
	public static function parseLevelInt(int $level): int {
		if (!array_key_exists($level, self::$DEF_LEVEL)) {
			$all = implode(', ', array_keys(self::$DEF_LEVEL));
			throw new ParseMemberLevelException('Available levels: ' . $all, 121);
		}

		return $level;
	}


	/**
	 * @param string $levelString
	 *
	 * @return int
	 * @throws ParseMemberLevelException
	 */
	public static function parseLevelString(string $levelString): int {
		$levelString = ucfirst(strtolower($levelString));
		$level = array_search($levelString, Member::$DEF_LEVEL);

		if (!$level) {
			$all = implode(', ', array_values(self::$DEF_LEVEL));
			throw new ParseMemberLevelException('Available levels: ' . $all, 121);
		}

		return (int)$level;
	}

	/**
	 * @param string $typeString
	 *
	 * @return int
	 * @throws UserTypeNotFoundException
	 */
	public static function parseTypeString(string $typeString): int {
		$typeString = strtolower($typeString);
		$type = array_search($typeString, Member::$TYPE);

		if ($type === false) {
			$all = implode(', ', array_values(self::$TYPE));
			throw new UserTypeNotFoundException('Available types: ' . $all);
		}

		return (int)$type;
	}

}

