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

import blue.lapis.common.LapisCommonsPlugin;
import blue.lapis.common.economy.account.EconomyAccount;
import blue.lapis.common.economy.api.TransactionAPI;
import blue.lapis.common.economy.event.TransactionEvent;
import blue.lapis.common.economy.event.impl.TransactionEventImpl;
import org.spongepowered.api.event.Result;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 *
 */
public class Transaction implements TransactionAPI {

    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 account) {
        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 EconomyAccount getAccount() {
        return account;
    }

    @Nullable
    public Object getReason() {
        return reason;
    }

    @Nullable
    public Object getInitiator() {
        return initiator;
    }

    @Nullable
    public Object getTarget() {
        return target;
    }

    public double getDelta() {
        return delta;
    }

    @Nonnull
    public Status getStatus() {
        return status;
    }

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

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

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

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

    @Nonnull
    public Transaction fireEvent() {
        status = Status.EVENT_FIRED;

        TransactionEvent event = new TransactionEventImpl(this);
        LapisCommonsPlugin.getGame().getEventManager().call(event);

        if (event.getResult() == Result.DENY) status = Status.CANCELLED;
        if (event.isCancelled()) status = Status.CANCELLED;

        return this;
    }

    public Status commit() {
        if (status != Status.EVENT_FIRED) {
            throw new IllegalStateException(
                    "Expected transaction state: EVENT_FIRED. Instead found " + status.name());
        }
        status = (account.apply(this)) ? Status.COMPLETE : Status.FAILED;

        return status;
    }


}

@@ -1467,16 +1467,61 @@
 ntImpl;%0A
+import com.google.common.base.Preconditions;%0A
 import o
@@ -3990,49 +3990,12 @@
 DENY
-) status = Status.CANCELLED;%0A        if (
+ %7C%7C 
 even
@@ -4010,16 +4010,28 @@
 elled())
+%0A           
  status 
@@ -4046,25 +4046,24 @@
 .CANCELLED;%0A
-%0A
         retu
@@ -4115,27 +4115,48 @@
         
-if 
+Preconditions.checkState
 (status 
 != Statu
@@ -4151,9 +4151,9 @@
 tus 
-!
+=
 = St
@@ -4172,150 +4172,109 @@
 IRED
-) %7B%0A            throw new IllegalStateException(%0A                    %22Expected transaction state: EVENT_FIRED. Instead found %22 + status.name()
+, %22Expected transaction state %25s, %22 +%0A                %22found %25s instead.%22, Status.EVENT_FIRED, status
 );%0A 
@@ -4284,17 +4284,14 @@
     
-%7D%0A       
+return
  sta
@@ -4296,17 +4296,16 @@
 tatus = 
-(
 account.
@@ -4315,17 +4315,16 @@
 ly(this)
-)
  ? Statu
@@ -4354,32 +4354,8 @@
 LED;
-%0A%0A        return status;
 %0A   

Use Guava Preconditions for transaction state checks.