import InputHelper from '../inputhelper';

let browser: BrowserMp;
let price: Number;

let lastkilometer: Number = 0;
let lastPrice: Number = 0;
let totalPrice: Number = 0;
var myVar;
export default function taximeterInput(globalData: GlobalData) {
    mp.events.add("CLIENT:setFarePrice", () => {
        /*
        var textBox = new InputHelper("Setzen Sie ihre Fahrtkosten [2 - 50 $/km]", globalData);
        textBox.show();
        textBox.getValue((data) => {
            var amount = parseInt(data);
            if (isNaN(amount)) {
                mp.game.graphics.notify('~r~Du musst eine Nummer eingeben!');
                return;
            }
            price = amount;
            mp.events.callRemote("SERVER:setFare", amount);
        });
        }
        */
        mp.events.callRemote("SERVER:setFare", 60);
        price = 60;
    });

    mp.events.add("CLIENT:startFare", () => {
        browser = mp.browsers.new('package://assets/html/taximeter/index.html');
        browser.execute(`setFarePrice('${price}');`)
        myVar = setInterval(myTimer, 100);
    });

    function myTimer() {
        if (totalPrice == lastPrice) return;
        let payPrice = +totalPrice - +lastPrice;
        lastPrice = totalPrice;
        mp.events.callRemote("SERVER:payFare", payPrice, lastkilometer);
    }

    mp.events.add("CEF:updateLastPrice", (price, lastkilometer1) => {
        totalPrice = price
        lastkilometer = lastkilometer1;
    });


    mp.events.add("CLIENT:startCustomerFare", (price1, km) => {
        browser = mp.browsers.new('package://assets/html/taximeter/index.html');
        browser.execute(`setFarePrice('${price1}');`);
        browser.execute(`updateKilometer('${km}')`);

    });

    mp.events.add("CLIENT:updateFare", (km) => {
        browser.execute(`updateKilometer('${JSON.parse(km)}')`)
    });


    mp.events.add("CLIENT:cancelFareCustomer", () => { //for Customer
        mp.events.callRemote("SERVER:cancelFareCustomer", lastPrice);
        browser.destroy();
        browser = null;
    });

    mp.events.add("CLIENT:cancelFare", () => { //for Taxidriver
        browser.destroy();
        browser = null;
    });
}