package blue.lapis.common.economy;

import blue.lapis.common.economy.account.EconomyAccount;

/**
 *
 */
public class Transaction {
    private boolean isSetter = false;
    private Status status = Status.INCOMPLETE;
    private EconomyAccount account;
    private Object initiator = null;
    private Object target = null;
    private Object reason = null;
    private double delta = 0.0d;
    private double balance = Double.NaN;

    private Transaction(EconomyAccount against) {
        this.account = account;
    }

    public static Transaction of(EconomyAccount account) {
        return new Transaction(account);
    }

    public static Transaction setBalance(EconomyAccount account, double balance) {
        Transaction result = new Transaction(account);
        result.balance = balance;
        result.isSetter = true;
        return result;
    }

    public Transaction withInitiator(Object o) {
        if (status==Status.INCOMPLETE) this.initiator = o;
        return this;
    }

    public Transaction withTarget(Object o) {
        if (status==Status.INCOMPLETE) this.target = o;
        return this;
    }

    public Transaction withReason(Object o) {
        if (status==Status.INCOMPLETE) this.reason = o;
        return this;
    }

    public Object getReason() {
        return reason;
    }

    public Object getInitiator() {
        return initiator;
    }

    public Object getTarget() {
        return target;
    }

    public double getDelta() {
        return delta;
    }

    public Transaction add(double amount) {
        delta += amount;
        return this;
    }

    public Transaction subtract(double amount) {
        delta -= amount;
        return this;
    }

    public Transaction multiplyBy(double amount) {
        delta *= amount;
        return this;
    }

    public Transaction divideBy(double amount) {
        delta /= amount;
        return this;
    }

    public Transaction fireEvent() {
        status = Status.EVENT_FIRED;
        //TODO: Fire
        //TODO: Update Status to CANCELLED if needed
        return this;
    }

    public Transaction commit() {
        if (status!=Status.EVENT_FIRED) {
            throw new IllegalStateException("Expected transaction state: EVENT_FIRED. Instead found "+status.name());
        }

        //TODO: Commit to the EconomyAccount
        return this;
    }

    public static enum Status {
        INCOMPLETE,
        EVENT_FIRED,
        CANCELLED,
        FAILED,
        COMPLETE;
    }
}

@@ -1,8 +1,1168 @@
+/*%0A * LapisCommons%0A * Copyright (c) 2014, LapisDev %3Chttps://github.com/LapisDev%3E%0A *%0A * Permission is hereby granted, free of charge, to any person obtaining a copy%0A * of this software and associated documentation files (the %22Software%22), to deal%0A * in the Software without restriction, including without limitation the rights%0A * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell%0A * copies of the Software, and to permit persons to whom the Software is%0A * furnished to do so, subject to the following conditions:%0A *%0A * The above copyright notice and this permission notice shall be included in all%0A * copies or substantial portions of the Software.%0A *%0A * THE SOFTWARE IS PROVIDED %22AS IS%22, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR%0A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,%0A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE%0A * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER%0A * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,%0A * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE%0A * SOFTWARE.%0A */%0A
 package 

Fix header in Transaction