import Adapter from 'src/adapters/adapter';
import bidfactory from 'src/bidfactory';
import bidmanager from 'src/bidmanager';
import * as utils from 'src/utils';
import { ajax } from 'src/ajax';
import { STATUS } from 'src/constants';

const ENDPOINT = '//rtb.vertamedia.com/hb/';

function VertamediaAdapter() {
  var baseAdapter = Adapter.createNew('vertamedia'),
      bidRequest;

  baseAdapter.callBids = function (bidRequests) {
    if (!bidRequests || !bidRequests.bids || bidRequests.bids.length === 0) {
      return;
    }

    var RTBDataParams = prepareAndSaveRTBRequestParams(bidRequests.bids[0]);

    if (!RTBDataParams) {
      return;
    }

    ajax(ENDPOINT, handleResponse, RTBDataParams, {
      contentType: 'text/plain',
      withCredentials: true,
      method: 'GET'
    });
  };

  function prepareAndSaveRTBRequestParams(bid) {
    if (!bid || !bid.params || !bid.params.aid || !bid.placementCode) {
      return;
    }

    bidRequest = bid;
    bidRequest.width = parseInt(bid.sizes[0], 10) || undefined;
    bidRequest.height = parseInt(bid.sizes[1], 10) || undefined;

    return {
      aid: bid.params.aid,
      w: parseInt(bid.sizes[0], 10) || undefined,
      h: parseInt(bid.sizes[1], 10) || undefined,
      domain: document.location.hostname
    };
  }

  /* Notify Prebid of bid responses so bids can get in the auction */
  function handleResponse(response) {
    var parsed;

    try {
      parsed = JSON.parse(response);
    } catch (error) {
      utils.logError(error);
    }

    if (!parsed || parsed.error || !parsed.bids || !parsed.bids.length) {
      bidmanager.addBidResponse(bidRequest.placementCode, createBid(STATUS.NO_BID));

      return;
    }

    bidmanager.addBidResponse(bidRequest.placementCode, createBid(STATUS.GOOD, parsed.bids[0]));
  }

  function createBid(status, tag) {
    var bid = bidfactory.createBid(status, tag);

    bid.code = baseAdapter.getBidderCode();
    bid.bidderCode = bidRequest.bidder;

    if (!tag || status !== STATUS.GOOD) {
      return bid;
    }

    bid.mediaType = 'video';
    bid.cpm = tag.cpm;
    bid.creative_id = tag.cmpId;
    bid.width = bidRequest.width;
    bid.height = bidRequest.height;
    bid.descriptionUrl = tag.url;
    bid.vastUrl = tag.url;

    return bid;
  }

  return {
    createNew: VertamediaAdapter.createNew,
    callBids: baseAdapter.callBids,
    setBidderCode: baseAdapter.setBidderCode
  };

}

VertamediaAdapter.createNew = function () {
  return new VertamediaAdapter();
};

module.exports = VertamediaAdapter;

@@ -968,185 +968,503 @@
 id;%0A
-    bidRequest.width = parseInt(bid.sizes%5B0%5D, 10) %7C%7C undefined;%0A    bidRequest.height = parseInt(bid.sizes%5B1%5D, 10) %7C%7C undefined;%0A
+%0A    let size = getSize(bid.sizes);%0A%0A    bidRequest.width = size.width;%0A    bidRequest.height = size.height;%0A%0A    return %7B%0A      aid: bid.params.aid,%0A      w: size.width,%0A      h: size.height,%0A      domain: document.location.hostname%0A    %7D;%0A  %7D%0A%0A  function getSize(requestSizes) %7B%0A    var parsed = %7B%7D,%0A        size = utils.parseSizesInput(requestSizes)%5B0%5D;%0A%0A    if (typeof size !== 'string') %7B
 %0A    
+  
 return 
-%7B%0A      aid: bid.params.aid,
+parsed;%0A    %7D%0A%0A    let parsedSize = size.toUpperCase().split('X');%0A%0A    return %7B
 %0A      w
 : pa
@@ -1459,16 +1459,20 @@
 %0A      w
+idth
 : parseI
@@ -1474,25 +1474,26 @@
 arseInt(
-bid.s
+parsedS
 ize
-s
 %5B0%5D, 10)
@@ -1514,16 +1514,21 @@
 %0A      h
+eight
 : parseI
@@ -1530,25 +1530,26 @@
 arseInt(
-bid.s
+parsedS
 ize
-s
 %5B1%5D, 10)
@@ -1565,50 +1565,8 @@
 ined
-,%0A      domain: document.location.hostname
 %0A   

Fix adapter getSize (#1064)