package ads_external

import (
	"alpha/adDelivery/helper"
	"alpha/adDelivery/structure"
	"encoding/json"
	"fmt"
	"net/http"
	"strings"

	"github.com/elliotchance/phpserialize"
)

func ExtenalAdRenderNativeImage(znextra AdExchangeZone, adm string) string {

	adm_res := NativeExtnal{}

	var adTextStyle, adTextHeader, adTextBody, adTextFooter string
	var title, logo, image, image_width, image_height string
	var do_sponsored, do_description, do_ratting, do_likes, do_download, do_sales_price, do_original_price string
	var do_phone, do_description2, do_address, do_display_url, do_cta string
	var clickTag_start, clickTag_end, impression string

	var native_ob map[interface{}]interface{}
	phpserialize.Unmarshal([]byte(znextra.Native_objects), &native_ob)
	image_width = fmt.Sprintf("%v", native_ob["img_width"])
	image_height = fmt.Sprintf("%v", native_ob["img_height"])

	json.Unmarshal([]byte(adm), &adm_res)


	fmt.Println("adm_res.Native.Asset",adm_res)
	fmt.Println("image_width",image_width)
	fmt.Println("image_height",image_height)

	for _, e := range adm_res.Native.Asset {
		if e.ID == 1 {
			title = e.Title.Text
		}
		if e.ID == 2 {
			logo = e.Img.URL
		}

		if e.ID == 3 {
			image = e.Img.URL
		}

		if e.ID == 4 {
			do_sponsored = e.Data.Value
		}
		if e.ID == 5 {
			do_description = e.Data.Value
		}
		if e.ID == 6 {
			do_ratting = e.Data.Value
		}
		if e.ID == 7 {
			do_likes = e.Data.Value
		}
		if e.ID == 8 {
			do_download = e.Data.Value
		}
		if e.ID == 9 {
			do_original_price = e.Data.Value
		}
		if e.ID == 10 {
			do_sales_price = e.Data.Value
		}
		if e.ID == 11 {
			do_phone = e.Data.Value
		}
		if e.ID == 12 {
			do_address = e.Data.Value
		}
		if e.ID == 13 {
			do_description2 = e.Data.Value
		}
		if e.ID == 14 {
			do_display_url = e.Data.Value
		}
		if e.ID == 15 {
			do_cta = e.Data.Value
		}

	}

	if adm_res.Native.Link.URL != "" {

		clickTag_start = `<a href='` + adm_res.Native.Link.URL + `' target='_blank'>`
		clickTag_end = `</a>`

	}

	for _, e := range adm_res.Native.Imptrackers {

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

	}

	adTextStyle = ` <style>
	.nv-main-container
	{
		
	}
	.nv-header
	{    display: table;
    width: 100%;
     padding: 5px 10px;
		
	}
	.nv-header-logo
	{
		           width: 32px;
    display: table-cell;
    vertical-align: middle;

	}
	.nv-header-logo img 
	{
		    max-width: 32px;
			max-height: 32px;
	}
	.nv-header-title h3
	{
		margin:auto;
	}
	.nv-header-title
	{
	    padding: 0px 15px;
    max-width: 70%;
    display: table-cell;
    vertical-align: middle;
	}
	.nv-header-price strike
	{
		      opacity: 0.7;
    color: #5c5959;
	}

	.nv-header-price
	{
	display: table-cell;
	width: 140px;
	max-width:25%;
    text-align: right;
    vertical-align: middle;
    color: #ff5252;
    font-weight: 600;

	}
	.nv-body
	{ padding: 5px 10px;
    text-align: justify;
    display: table;
    width: 100%;
		
	}
	.nv-body img
	{
		height:` + image_height + `px;
		width:` + image_width + `px;
		    float: left;
			padding-right: 10px;
			padding-bottom: 10px;

	}
	.nv-footer
	{       padding: 5px 10px;
    display: table;
    width: 100%;
		
	}
		.nv-footer .nv-footer-fr
		{
    display: table-cell;
    text-align: left;
		}
		
		.nv-footer .nv-footer-en
		{
    display: table-cell;
    text-align: left;
			
			
		}
		.nv-footer-phone , .nv-footer-str , .nv-footer-dow ,.nv-footer-like , .nv-footer-cta
		{
						    margin-bottom: 5px;

		}
		
 .nv-footer-cta button
		{
			    width: 100%;
    text-align: center;
    max-width: 150px;
    padding: 8px 15px;
    background: #2f2ff6;
    color: white;
    border: none;
}
		.nv-footer-address , .nv-footer-spons , .nv-footer-lk
		{
			    margin-bottom: 5px;

		}
	
</style>`

	adTextHeader = `<div class="nv-header">`
	if logo != "" {
		adTextHeader += `<div class="nv-header-logo"><img src="` + logo + `"></div>`
	}

	if title != "" {
		adTextHeader += `<div class="nv-header-title"><h3>` + title + `</h3></div>`
	}

	if do_original_price != "" && do_sales_price != "" {
		adTextHeader += `<div class="nv-header-price"><strike>` + do_original_price + `</strike> &nbsp;` + do_sales_price + `</div>`
	} else if do_original_price != "" {
		adTextHeader += `<div class="nv-header-price">` + do_original_price + `</div>`
	} else if do_sales_price != "" {
		adTextHeader += `<div class="nv-header-price">` + do_sales_price + `</div>`
	}
	adTextHeader += `</div>`

	adTextBody = `<div class="nv-body">`

	if image != "" {
		adTextBody += `<img class="nv-body-img" src="` + image + `">`
	}

	adTextBody += do_description

	if do_description2 != "" {

		adTextBody += `<div>` + do_description + `</div>`

	}

	adTextFooter = `<div class="nv-footer">
			
			<div class="nv-footer-fr">`
	if do_address != "" {
		adTextFooter += `<div class="nv-footer-address">` + do_address + `</div>`
	}
	if do_sponsored != "" {
		adTextFooter += `<div class="nv-footer-spons">` + do_sponsored + `</div>`
	}
	if do_display_url != "" {
		adTextFooter += `<div class="nv-footer-lk"><a href="` + do_display_url + `">` + do_display_url + `</a></div>`
	}
	adTextFooter += `</div>`

	adTextFooter += `<div class="nv-footer-en">`
	if do_phone != "" {
		adTextFooter += `<div class="nv-footer-phone">` + do_phone + `	</div>`
	}
	if do_ratting != "" {
		adTextFooter += `<div class="nv-footer-str"> ` + do_ratting + ` Star</div>`
	}
	if do_download != "" {
		adTextFooter += `<div class="nv-footer-dow">Downloads : ` + do_download + `</div>`
	}
	if do_likes != "" {
		adTextFooter += `<div class="nv-footer-like">Likes : ` + do_likes + `</div>`
	}
	if do_cta != "" {
		adTextFooter += `<div class="nv-footer-cta">` + clickTag_start + `<button type="button">` + do_cta + `</button>` + clickTag_end + `</div>`
	}

	adTextFooter += `</div>
			</div>` + impression

	return adTextStyle + `<div class="nv-main-container" >` + adTextHeader + adTextBody + adTextFooter + `</div>`

}

type NativeExtnal struct {
	Native struct {
		Asset []struct {
			ID    int `json:"id"`
			Title struct {
				Text string `json:"text,omitempty"`
			} `json:"title,omitempty"`
			Img struct {
				URL string `json:"url,omitempty"`
			} `json:"img,omitempty"`
			Data struct {
				Value string `json:"value,omitempty"`
			} `json:"data,omitempty"`
		} `json:"asset"`
		Link struct {
			URL string `json:"url,omitempty"`
		} `json:"link,omitempty"`
		Imptrackers []string `json:"imptrackers,omitempty"`
	} `json:"native,omitempty"`
}

func AdRenderOutstreamVideoAdSDK(htmltext string, zninfo structure.ZoneDetails, r *http.Request) map[string]interface{} {

	var click_url, imp_url string

	sdk_ad_outs := make(map[string]interface{})

	if zninfo.Vidtype == 1 { // aritcle

		sdk_ad_outs["imp_url"] = imp_url
		sdk_ad_outs["click_url"] = click_url
		sdk_ad_outs["ad_type"] = "IN_ART_VID"
		sdk_ad_outs["ad_tag"] = "NAT_INART"
		sdk_ad_outs["Native_ad"] = OutstreamSDKNv_ad{Ad_type: "IN_ART_VID", Video: htmltext}

	} else if zninfo.Vidtype == 3 { // intertital

		adTag := make(map[string]interface{})
		sdk_ad_outs["imp_url"] = imp_url
		//~ sdk_ad_outs["click_url"] = click_url
		sdk_ad_outs["ad_type"] = "M_INT_VID"
		//~ sdk_ad_outs["HTML_response"] =htmltext
		//~ sdk_ad_outs["layout"] = zninfo.Layout

		adTag["HTML_response"] = htmltext
		adTag["click_url"] = click_url
		adTag["layout"] = zninfo.Layout
		adTag["ad_type"] = "M_INT_VID"

		sdk_ad_outs["ad_tag"] = adTag

	} else if zninfo.Vidtype == 4 { // In feed

		Getposition := helper.GetUrlParam(r, "position")

		if Getposition == "true" { // position
			a := make(map[int]interface{})
			position1 := make(map[string]string)

			position := zninfo.Infeedpos
			repeating := zninfo.Infeedads

			arr := strings.Split(position, ",")

			for key, value := range arr {
				position1["position"] = value
				a[key] = position1
			}

			rep := make(map[string]interface{})
			rep["interval"] = repeating

			sdk_ad_outs["repeating"] = rep
			sdk_ad_outs["fixed"] = a

			return sdk_ad_outs

		}

		sdk_ad_outs["imp_url"] = imp_url
		sdk_ad_outs["click_url"] = click_url
		sdk_ad_outs["ad_type"] = "IN_FEED_VID"
		sdk_ad_outs["ad_tag"] = "NAT_FEED"
		sdk_ad_outs["Native_ad"] = OutstreamSDKNv_ad{Ad_type: "IN_FEED_VID",
			Imptracker: []string{click_url},
			Clktracker: click_url,
			Video:      htmltext}

	} else if zninfo.Vidtype == 6 { // Reward

		rew := strings.Split(zninfo.Reward, "|")
		amt := strings.Split(zninfo.Amount, "|")

		temp := make(map[int]interface{})
		adTag := make(map[string]interface{})
		rewards := make(map[string]interface{})

		for key, value := range rew {
			ga := make(map[string]string)
			ga["amount"] = amt[key]
			ga["name"] = value
			temp[key] = ga
		}

		sdk_ad_outs["ad_type"] = "REWARDED_VID"

		rewards["rewards"] = temp
		adTag["rewarded_val"] = rewards
		adTag["HTML_response"] = htmltext
		adTag["imp_url"] = imp_url
		adTag["click_url"] = click_url
		adTag["layout"] = zninfo.Layout
		adTag["ad_type"] = "REWARDED_VID"

		sdk_ad_outs["ad_tag"] = adTag

	}

	sdk := make(map[string]interface{})
	sdk["response"] = "success"
	sdk["ads"] = sdk_ad_outs

	return sdk

}

type OutstreamSDKNv_ad struct {
	Ad_type    string   `json:"ad_type,omitempty"`
	Video      string   `json:"video,omitempty"`
	Imptracker []string `json:"imptracker,omitempty"`
	Clktracker string   `json:"clktracker,omitempty"`
}
