﻿//Interact(int? actionID, int? targetCharacterId, int? targetItemRealId, int? targetRoomRealId, decimal? cashInvolved, decimal? bankInvolved)

function TransactionResponse(r) {
    var v = null;
    var message = '';
    var error = '';
    for (var i in r.d) {
        v = r.d[i].Value;
        switch (r.d[i].Key) {
            case "error": error = v; break;
            case "message": message = v; break;
            case "c cash": GlobalUpdateCash(v); break;
            case "c bank": GlobalUpdateBank(v); break;
            case "t cash": break;
            case "t bank": break;
            case "rr cash":
                //if (v == 0) { $('.uxRoomRealSafe').hide(); }
                $('.uxRoomRealCurrentMoney').html(GlobalAddCommas(parseFloat(v).toFixed(2)));
                UpdateStreetResetUi();
                break;
        }
    }

    if (error != '') {
        GlobalShowGenericDialog('Whoops :(', error);
    } else {
        GlobalShowGenericDialog('Success!', message);
    }
}

function GiveItem(itemRealID) {
    var str = "<div id='GiveItemPersonSelection'>";
    str += $($('.peopleInStreet')[0]).html();
    str += "</div>";
    GlobalShowGenericDialog("Give Item",
    "Click the person you would you like to give this item to.<br /><br />Recipitents must be in same location as you.<br/><br/>" + str,
    {
        Cancel: function () { $(this).dialog('close'); }
    });


    $('#GiveItemPersonSelection > div').removeAttr('onclick');

    $('#GiveItemPersonSelection > div')
    .unbind('click')
    .click(function () {
        $(this).dialog('close');
        //GlobalBlockUi();
        DoGiveItem(parseInt($(this).attr('data-person-id')), itemRealID);
    });
}
function GiveItemDone(result) {
    GlobalShowGenericDialog("Give Item", result.d[0].Value);
}

function doTransactionBase(actionId, targetCharacterId, targetItemRealId, targetRoomRealId, cashInvolved, bankInvolved, callBack) {
    var dataStr = "";
    dataStr += "{'actionID': " + actionId + ",";
    if (targetCharacterId == null) { dataStr += "'targetCharacterId':null,"; } else { dataStr += "'targetCharacterId':" + targetCharacterId + ","; }
    if (targetItemRealId == null) { dataStr += "'targetItemRealId':null,"; } else { dataStr += "'targetItemRealId':" + targetItemRealId + ","; }
    if (targetItemRealId == null) { dataStr += "'targetItemRealId':null,"; } else { dataStr += "'targetItemRealId':" + targetItemRealId + ","; }
    if (targetRoomRealId == null) { dataStr += "'targetRoomRealId':null,"; } else { dataStr += "'targetRoomRealId':" + targetRoomRealId + ","; }
    if (cashInvolved == null) { dataStr += "'cashInvolved':null,"; } else { dataStr += "'cashInvolved':" + targetRoomRealId + ","; }
    if (bankInvolved == null) { dataStr += "'bankInvolved':null}"; } else { dataStr += "'bankInvolved':" + bankInvolved + "}"; }
    $.ajax({ type: "POST", url: "WebService.asmx/Interact", contentType: "application/json; charset=utf-8", data: dataStr, dataType: "json", success: callBack, error: StreetUpdateError });
}

function DoGiveItem(cid, irid) {
    doTransactionBase(81, cid, irid, null, null, null, GiveItemDone);
}
function TransactionTakeMoneyFromSafe() {
    doTransactionBase(62, null, null, null, null, null, TransactionResponse);
}
function TransactionPutMoneyInSafe() {
    doTransactionBase(61, null, null, null, null, null, TransactionResponse);
}
