package ads

import (
	"alpha/adDelivery/helper"
	"alpha/adDelivery/structure"
	config "alpha/configuration"
	"strconv"
)

func NormalAdRender(adDetails *AdDetails, zn structure.ZoneDetails, adDeliveryType string) {

	clickurl := ""
	clickTag_start := ""
	clickTag_end := ""
	ImgTag := ""

	if adDetails.Url != "" {
		clickurl = config.GetConfig("protocol", "webpath") + config.GetConfig("delivery", "webpath") + "/cl.php?oaparams=2&bannerid=" + strconv.Itoa(adDetails.Ad_id) + "&campaignid=" + strconv.Itoa(adDetails.Placement_id) + "&zoneid=" + strconv.Itoa(zn.Zoneid) + "___oadest=" + adDetails.Url

		clickTag_start = `<a href='` + clickurl + `' target='_blank'>`
		clickTag_end = `</a>`

	}

	impurl := config.GetConfig("protocol", "webpath") + config.GetConfig("delivery", "webpath") + "/lg.php?bannerid=" + strconv.Itoa(adDetails.Ad_id) + "&amp;campaignid=" + strconv.Itoa(adDetails.Placement_id) + "&amp;zoneid=" + strconv.Itoa(zn.Zoneid)

	impurl2 := config.GetConfig("protocol", "webpath") + config.GetConfig("delivery", "webpath") + "/lg.php?bannerid=" + strconv.Itoa(adDetails.Ad_id) + "&campaignid=" + strconv.Itoa(adDetails.Placement_id) + "&zoneid=" + strconv.Itoa(zn.Zoneid)

	ImgTag = `<img src='` + impurl + `' width='0' height='0' alt='' style='width: 0px; height: 0px;' />`

	rand1 := helper.RandToken(5)

	adDetails.Imp_url = impurl
	adDetails.Click_url = clickurl

	if adDetails.Type == "web" {
		adDetails.Html = clickTag_start + `<img src='` + config.GetConfig("protocol", "webpath") + config.GetConfig("images", "webpath") + `/` + adDetails.Filename + `' width='` + strconv.Itoa(adDetails.Width) + `' height='` + strconv.Itoa(adDetails.Height) + `' alt='' title='' border='0' />` + clickTag_end + `<div id='beacon_` + rand1 + `' style='position: absolute; left: 0px; top: 0px; visibility: hidden;'>` + ImgTag + `</div>`

	} else if adDetails.Type == "sql" {
		adDetails.Html = clickTag_start + `<img src='` + config.GetConfig("protocol", "webpath") + config.GetConfig("delivery", "webpath") + `/ai.php?filename=` + adDetails.Filename + `&contenttype=` + adDetails.Contenttype + `' width='` + strconv.Itoa(adDetails.Width) + `' height='` + strconv.Itoa(adDetails.Height) + `' alt='' title='' border='0' />` + clickTag_end + `<div id='beacon_` + rand1 + `' style='position: absolute; left: 0px; top: 0px; visibility: hidden;'><img src='` + impurl + `' width='0' height='0' alt='' style='width: 0px; height: 0px;' /></div>`
	} else if adDetails.Type == "url" {
		adDetails.Html = clickTag_start + `<img src='` + adDetails.Imageurl + `' width='` + strconv.Itoa(adDetails.Width) + `' height='` + strconv.Itoa(adDetails.Height) + `' alt='' title='' border='0' />` + clickTag_end + `<div id='beacon_` + rand1 + `' style='position: absolute; left: 0px; top: 0px; visibility: hidden;'>` + ImgTag + `</div>`
	} else if adDetails.Type == "txt" {
		adDetails.Html = clickTag_start + `` + adDetails.Bannertext + `` + clickTag_end + `<div id='beacon_` + rand1 + `' style='position: absolute; left: 0px; top: 0px; visibility: hidden;'>` + ImgTag + `</div>`
	} else if adDetails.Type == "html" {
		if adDetails.Filename != "" {
			adDetails.Html = clickTag_start + `<iframe id='rv-h5-` + rand1 + `' name='rv-h5-` + rand1 + `' src='` + config.GetConfig("protocol", "webpath") + config.GetConfig("images", "webpath") + "/" + adDetails.Filename + `/index.html?CkT={CRL}'  "marginwidth='0' marginheight='0' scrolling='no'  frameborder='0' width='` + strconv.Itoa(adDetails.Width) + `' height='` + strconv.Itoa(adDetails.Height) + `' style='width: ` + strconv.Itoa(adDetails.Width) + `px; height: ` + strconv.Itoa(adDetails.Height) + `px; border: 0'></iframe>` + clickTag_end

			if adDeliveryType != "sdk" {
				adDetails.Html = adDetails.Html + ` <script>  document.getElementById('rv-h5-` + rand1 + `').onload = true ? function () { var i = document.createElement('IMG');  i.src = '` + impurl2 + `';  } : null; </script><script type='text/javascript'>document.context='fHA6MQ=='; </script>`
			}

		} else {
			adDetails.Html = `<div>` + adDetails.Htmltemplate + `<div id='beacon_` + rand1 + `' style='position: absolute; left: 0px; top: 0px; visibility: hidden;'>` + ImgTag + `</div></div>`
		}
	}

	adDetails.Html = adDetails.Append + adDetails.Html + adDetails.Prepend

}
