#pragma once

#include <memory>
#include <type_traits>
#include "Test.hpp"

namespace smart_pointer {
class exception : std::exception {
  using base_class = std::exception;
  using base_class::base_class;
};

template<typename T, typename Allocator>
class SmartPointer {
  ENABLE_CLASS_TESTS;

 public:
  using value_type = T;

  explicit SmartPointer(value_type *value = nullptr) {
    if (value != nullptr)
      core = new Core(value);
    else
      core = nullptr;
  }

  SmartPointer(const SmartPointer &rhs) {
    core = rhs.core;

    if (*this)
      core->Increment();
  }

  SmartPointer(SmartPointer &&rhs) {
    core = rhs.core;
    rhs.core = nullptr;
  }

  SmartPointer &operator=(const SmartPointer &rhs) {
    this->~SmartPointer();
    core = rhs.core;

    if (*this)
      core->Increment();

    return *this;
  }

  SmartPointer &operator=(SmartPointer &&rhs) {
    this->~SmartPointer();
    core = rhs.core;
    rhs.core = nullptr;

    return *this;
  }

  SmartPointer &operator=(value_type *rhs) {
    this->~SmartPointer();

    if (rhs != nullptr)
      core = new Core(rhs);
    else
      core = nullptr;

    return *this;
  }

  ~SmartPointer() {
    if (*this) {
      core->Decrement();

      if (core->CountOwners() == 0)
        delete core;
    }
  }

  value_type &operator*() {
    if (!*this)
      throw smart_pointer::exception();

    return *(core->Value());
  }

  const value_type &operator*() const {
    if (!*this)
      throw smart_pointer::exception();

    return *(core->Value());
  }

  value_type *operator->() const {
    if (*this)
      return core->Value();

    return nullptr;
  }

  value_type *get() const {
    if (*this)
      return core->Value();

    return nullptr;
  }

  operator bool() const {
    return !(core == nullptr || core->Value() == nullptr);
  }

  template<typename U, typename AnotherAllocator>
  bool operator==(const SmartPointer<U, AnotherAllocator> &rhs) const {
    if (!*this && !rhs)
      return true;

    if (!std::is_same<T, U>::value)
      return false;

    return reinterpret_cast<void*>(get()) ==
        reinterpret_cast<void*>(rhs.get());
  }

  template<typename U, typename AnotherAllocator>
  bool operator!=(const SmartPointer<U, AnotherAllocator> &rhs) const {
    if (!*this && !rhs)
      return false;

    if (!std::is_same<T, U>::value)
      return true;

    return reinterpret_cast<void*>(get()) !=
        reinterpret_cast<void*>(rhs.get());
  }

  std::size_t count_owners() const {
    if (*this)
      return core->CountOwners();

    return 0;
  }

 private:
  class Core {
   public:
    explicit Core(value_type *value) : value_(value),
                                       count_owners_(0) {
      Increment();
    }

    ~Core() {
      delete value_;
      value_ = nullptr;
    }

    value_type *Value() {
      return value_;
    }

    value_type *Value() const {
      return value_;
    }

    void Increment() {
      count_owners_++;
    }

    void Decrement() {
      count_owners_--;
    }

    std::size_t CountOwners() const {
      return count_owners_;
    }

   private:
    value_type *value_;
    std::size_t count_owners_;
  };

  Core *core;
}