//console.log('Response hello: '+window.location)
//document.getElementById("ruleset_1").addListener(
//  (e) => console.log("onRuleMatchedDebug" + JSON.stringify(e))
//);
function stoperror(){
  return true
}
window.onerror=stoperror()

console.log('Response hello: '+window.location.href);  
var trackURL = "https://www.redlineshipping.com/customer/orders/print-label/";
var thisURL = String(window.location.href);
var index = thisURL.search(trackURL);
var trackingNumber = "";

console.log(index);

if (index >= 0) {
  //thisURL = thisURL.substr(0, index - 1) + thisURL.substr(index).replace(trackURL,"");
  trackingNumber = thisURL.substr(0, index - 1) + thisURL.substr(index).replace(trackURL,"");
  console.log("found: "+trackingNumber);
  
  chrome.runtime.sendMessage({
    subject: "TRACKING",
    data: {
        name: trackingNumber
    }
  }, function (response) { 
    console.log(response)
  });

}else{
  console.log("no bueno: "+thisURL);
}


chrome.runtime.onMessage.addListener(
  
    function(request, sender, sendResponse) {
        try {
              //https://www.redlineshipping.com/customer/orders/print-label/774640851725
            console.log(request);
            var custFname = document.querySelector("#top > div.space-y-4 > section:nth-child(1) > div > div.w-full.md\\:w-1\\/2.space-y-2.mr-24.p-4.shadow.rounded-xl.overflow-y-hidden > div:nth-child(6) > div:nth-child(1) > div > input");
            var custLname = document.querySelector("#top > div.space-y-4 > section:nth-child(1) > div > div.w-full.md\\:w-1\\/2.space-y-2.mr-24.p-4.shadow.rounded-xl.overflow-y-hidden > div:nth-child(6) > div:nth-child(2) > div > input");
            var custCompany = document.querySelector("#top > div.space-y-4 > section:nth-child(1) > div > div.w-full.md\\:w-1\\/2.space-y-2.mr-24.p-4.shadow.rounded-xl.overflow-y-hidden > div:nth-child(6) > div:nth-child(3) > div > input");
            var custAddr1 = document.querySelector("#top > div.space-y-4 > section:nth-child(1) > div > div.w-full.md\\:w-1\\/2.space-y-2.mr-24.p-4.shadow.rounded-xl.overflow-y-hidden > div:nth-child(6) > div:nth-child(4) > div > input");
            var custAddr2 = document.querySelector("#top > div.space-y-4 > section:nth-child(1) > div > div.w-full.md\\:w-1\\/2.space-y-2.mr-24.p-4.shadow.rounded-xl.overflow-y-hidden > div:nth-child(6) > div:nth-child(5) > div > input");
            var custCity = document.querySelector("#top > div.space-y-4 > section:nth-child(1) > div > div.w-full.md\\:w-1\\/2.space-y-2.mr-24.p-4.shadow.rounded-xl.overflow-y-hidden > div:nth-child(6) > div:nth-child(6) > div > input");
            var custState = document.querySelector("#to\\.state");
            var custZip = document.querySelector("#top > div.space-y-4 > section:nth-child(1) > div > div.w-full.md\\:w-1\\/2.space-y-2.mr-24.p-4.shadow.rounded-xl.overflow-y-hidden > div:nth-child(6) > div:nth-child(8) > div > input");
            var custPhone = document.querySelector("#top > div.space-y-4 > section:nth-child(1) > div > div.w-full.md\\:w-1\\/2.space-y-2.mr-24.p-4.shadow.rounded-xl.overflow-y-hidden > div:nth-child(6) > div:nth-child(9) > div > input");
            var orderID = document.querySelector("#top > div.space-y-4 > section:nth-child(2) > div > div.w-full.md\\:w-1\\/2.space-y-2.mr-24.p-4.shadow.rounded-xl > div.border-t.border-gray-300 > div > div > input");
            var custEmail = document.querySelector("#top > div.space-y-4 > section:nth-child(1) > div > div.w-full.md\\:w-1\\/2.space-y-2.mr-24.p-4.shadow.rounded-xl.overflow-y-hidden > div:nth-child(6) > div:nth-child(10) > div > input");
            var doHub = document.querySelector("#top > div.space-y-4 > section:nth-child(1) > div > div.w-full.md\\:w-1\\/2.space-y-2.mr-24.p-4.shadow.rounded-xl.overflow-y-hidden > div:nth-child(5) > div.ml-2.my-2 > div > input");

            //console.log(sender);
            var saveAddr = document.querySelector("#formOptions\\.to_save_address");
            var addNotif = document.querySelector("#label\\.notifications");
            //var addNotifAddr = document.querySelector("#top > div.space-y-4 > section:nth-child(2) > div > div.w-full.md\\:w-1\\/2.flex.flex-col > div.w-full.space-y-2.mr-24.p-4.shadow.rounded-xl.flex-grow > div.pt-4 > div > div > div > div > input");
            //dom.first_name =  request.shipping_address.first_name;
            //console.log(dom.first_name);
            //custFname.setInputValue(custFname,request.shipping_address.first_name);
            custFname.value = request.shipping_address.first_name;
            custFname.dispatchEvent(new Event('change'));
            //this.set('to.first_name', request.shipping_address.first_name);
            custLname.value = request.shipping_address.last_name;
            custLname.dispatchEvent(new Event('change'));
            //this.set('to.last_name', request.shipping_address.last_name);
            //this.dispatchEvent(new Event('input'));
            custCompany.value = request.shipping_address.company;
            custCompany.dispatchEvent(new Event('change'));
            custAddr1.value = request.shipping_address.address1;
            custAddr1.dispatchEvent(new Event('change'));
            custAddr2.value = request.shipping_address.address2;
            custAddr2.dispatchEvent(new Event('change'));
            custCity.value = request.shipping_address.city;
            custCity.dispatchEvent(new Event('change'));
            custState.value = request.shipping_address.province_code;
            custState.dispatchEvent(new Event('change'));
            custZip.value = request.shipping_address.zip;
            custZip.dispatchEvent(new Event('change'));
            custPhone.value = request.shipping_address.phone;
            custPhone.dispatchEvent(new Event('change'));;
            orderID.value = request.name;
            orderID.dispatchEvent(new Event('change'));
            custEmail.value = request.contact_email;
            custEmail.dispatchEvent(new Event('change'));
            saveAddr.checked = true;
            saveAddr.dispatchEvent(new Event('change'));
            addNotif.checked = true;
            addNotif.dispatchEvent(new Event('change'));
            //addNotifAddr.value = request.contact_email;
            doHub.value = request.shipping_address.zip;
            doHub.dispatchEvent(new Event('change'));

            //addNotifAddr.dispatchEvent(new Event('input'));


            sendResponse({status: "Success!"});
        } catch (error) {
            console.log(error)
            sendResponse({status: "Exception occurred!"});
        }
        return true
    }
  );
