#include "num.h"

Num::Num(int value, int modulo) {
    if (modulo == 0) {
        this->value = value;
    } else {
        this->value = value % modulo;
        this->modulo = modulo;
    }
}

Num &Num::operator=(const Num &other) = default;

Num Num::operator+(const Num &other) {
    int64_t new_value =  this->value +  other.value;

    Num new_num(new_value, this->modulo);
    return new_num;
}

Num Num::operator*(const Num &other) {
    int64_t new_value =  this->value *  other.value;

    Num new_num(new_value, this->modulo);
    return new_num;
}

Num Num::operator-(const Num &other) {
    int64_t new_value =  this->value -  other.value;
    Num new_num(new_value, this->modulo);
    return new_num;
}
Num Num::operator-(int num) {
    int64_t new_value =  this->value -  num;
    Num new_num(new_value, this->modulo);
    return new_num;
}
Num Num::operator+(int num) {
    int64_t new_value =  this->value +  num;
    Num new_num(new_value, this->modulo);
    return new_num;
}

Num Num::operator*(int num) {
    int64_t new_value =  this->value *  num;
    Num new_num(new_value, this->modulo);
    return new_num;
}
Num &Num::operator+=(int num) {
    int64_t new_value =  this->value +  num;
    this->value = new_value % this->modulo;
    return *this;
}

Num &Num::operator*=(int num) {
    int64_t new_value =  this->value *  num;
    this->value = new_value % this->modulo;
    return *this;
}

Num &Num::operator*=(const Num &other) {
    int64_t new_value =  this->value *  other.value;
    this->value = new_value % this->modulo;
    return *this;
}

Num &Num::operator+=(const Num &other) {
    int64_t new_value =  this->value +  other.value;
    this->value = new_value % this->modulo;
    return *this;
}

Num &Num::operator-=(const Num &other) {
    int64_t new_value =  this->value -  other.value;
    this->value = new_value % this->modulo;
    if (this->value < 0) {
        this->value = this->modulo + this->value;
    }
    return *this;
}
Num &Num::operator-=(int num) {
    int64_t new_value =  this->value -  num;
    this->value = new_value % this->modulo;
    if (this->value < 0) {
        this->value = this->modulo + this->value;
    }
    return *this;
}