package addata

import (
	"encoding/json"
	"fmt"
	"github.com/gomodule/redigo/redis"
	"github.com/stretchr/testify/assert"
	"miads/adslib"
	"miads/adslib/ads_redis"
	"miads/adslib/utils"
	"net/http"
	"strings"
	"testing"
	"time"

	"github.com/rafaeljusto/redigomock"
	"github.com/jarcoal/httpmock"
)

var dsp utils.DspParam

func SetupTest() error {
	dspStr := "{'phone_type': '', 'advertiser': '', 'ip': '223.91.87.241', 'ad_type': 1, 'carrier_id': 0, 'imei': u'72710e3df592f07f6dbcc04b747f3653', 'dsp_city_code': 1156411300, 'num': 1, 'os_version': '6.0.1', 'ua_client': 'Mozilla/5.0 (Linux; Android 6.0.1; OPPO R9s Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36', 'real_md5_imei': u'72710e3df592f07f6dbcc04b747f3653', 'lng': 0.0, 'android_id': 'a3306c7ca6a9c2a3', 'category': '', 'city': '\\xe5\\x8d\\x97\\xe9\\x98\\xb3\\xe5\\xb8\\x82', 'source_flag': 0, 'ver': '', 'source': '', 'is_360_circle': 1, 'adspace_id': '', 'send_phone_type': 0, 'platform': 1, 'real_mi_flag': 0, 'screen_density': 3, 'channel': '', 'province': '\\xe6\\xb2\\xb3\\xe5\\x8d\\x97\\xe7\\x9c\\x81', 'real_req_source': 'kuxin', 'sup_click_flag': 1, 'screen_width': 1080, 'sup_show_flag': 1, 'brand': 'OPPO', 'screen_size': '1920x1080', 'mac': '986F60693A9F', 'idfa': '', 'lat': 0.0, 'user_id': 0, 'osVersion': '', 'model': 'OPPO R9s', 'device_id': '', 'hongtu': 0, 'sdkVersion': '', 'screen_scale': 0, 'local_ip': '__ADS_LP__', 'replace_flag': 1, 'device_id_type': 1, 'req_source': 'kuxin', 'headers': '', 'origin_imei': u'865873038260422', 'user_agent': '', 'request_id': '1592103420651_34020908', 'origin_mac': '98:6F:60:69:3A:9F', 'os_type': '', 'ua': u'Mozilla/5.0 (Linux; Android 9; Redmi 6 Pro Build/PKQ1.180917.001; wv) AppleWebKit/537.36 (KHTML like Gecko) Version/4.0 Chrome/74.0.3729.136 Mobile Safari/537.3', 'network_type': 9, 'placements': '', 'screen_height': 1920}"
	dspStr = strings.ReplaceAll(dspStr,": u'", ": '")
	dspStr = strings.ReplaceAll(dspStr,"'", "\"")
	dspStr = strings.ReplaceAll(dspStr,"\\", "")

	dsp.Init()
	err := json.Unmarshal([]byte(dspStr), &dsp)
	if err != nil {
		fmt.Printf("unmarshal dsp failed: %s", err)
		return err
	}
	return nil
}

func TestGetAdsReqUrl(t *testing.T) {
	err := SetupTest()
	assert.Nil(t,err)

	reqUrl := getAdsReqUrl(&dsp)
	assert.Equal(t, "https://m.video.xiaomi.com/api/a3/otv_emc?_emcp=pre_play&_devid=72710e3df592f07f6dbcc04b747f3653&ref=yilin&_sn=05817a33d4210ad2c67f4b869b5eedde&__ip__=223.91.87.241&_phonetype=0", reqUrl)
}

func TestGetReportUrl(t *testing.T) {
	err := SetupTest()
	assert.Nil(t,err)

	advertiser := "xiaomi"
	report := getReportUrl("VIEW", advertiser, &dsp)
	assert.Equal(t, "http://api-ads.tiantianqutao.com/ads_feedback?action=VIEW&advertiser=video&req_source=kuxin&brand=OPPO&city_code=1156411300&request_id=1592103420651_34020908&spefial_flag=0", report)
}

//before dsp:
//after dsp: {'phone_type': '', 'advertiser': '', 'ip': '111.22.209.32', 'ad_type': 1, 'carrier_id': 0, 'imei': u'f25a9e70e339a6f64c412a569f074dbd', 'dsp_city_code': 1156431200, 'num': 1, 'os_version': '6.0', 'ua_client': 'Mozilla/5.0 (Linux; Android 6.0; DMZ V3 Build/MRA58K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/44.0.2403.119 Mobile Safari/537.36', 'real_md5_imei': u'f25a9e70e339a6f64c412a569f074dbd', 'lng': 0.0, 'android_id': '90a9674d80609ce9', 'category': '', 'city': '\xe6\x80\x80\xe5\x8c\x96\xe5\xb8\x82', 'source_flag': 0, 'ver': '', 'source': '', 'is_360_circle': 1, 'adspace_id': '', 'send_phone_type': 0, 'platform': 1, 'real_mi_flag': 0, 'screen_density': 3, 'channel': '', 'province': '\xe6\xb9\x96\xe5\x8d\x97\xe7\x9c\x81', 'real_req_source': 'kuxin', 'sup_click_flag': 1, 'screen_width': 1080, 'sup_show_flag': 1, 'brand': 'DMZ V3', 'screen_size': '1776x1080', 'mac': '00FCCDF87EAC', 'idfa': '', 'lat': 0.0, 'user_id': 0, 'osVersion': '', 'model': 'DMZ V3', 'device_id': '', 'hongtu': 0, 'sdkVersion': '', 'screen_scale': 0, 'local_ip': '__ADS_LP__', 'replace_flag': 1, 'device_id_type': 1, 'req_source': 'kuxin', 'headers': '', 'origin_imei': u'04E676CFCCAB', 'user_agent': '', 'request_id': '1592019855466_92027662', 'origin_mac': '00:FC:CD:F8:7E:AC', 'os_type': '', 'ua': u'Mozilla/5.0 (Linux; Android 5.1.1; mi 3 Build/LMY47I; wv) AppleWebKit/537.36 (KHTML like Gecko) Version/4.0 Chrome/52.0.2743.100 Mobile Safari/537.3', 'network_type': 9, 'placements': '', 'screen_height': 1920}
func TestGetAdsInfos(t *testing.T) {
	err := SetupTest()
	assert.Nil(t, err)

	httpmock.Activate()
	defer httpmock.DeactivateAndReset()
	httpmock.RegisterResponder("GET", "https://m.video.xiaomi.com/api/a3/otv_emc",
		func(req *http.Request) (*http.Response, error) {
			body:= "{\"data\":[{\"tag_id\":\"1.1.10.5\",\"emc_type\":\"pre_play\",\"display_type\":{\"name\":\"video\",\"delay\":0,\"row_count\":0},\"target\":\"mv://Linker?link_url=https%3A%2F%2Fccc-x.jd.com%2Fdsp%2Ftp%3Flog%3DQjg4m-UrTnWAWYp-Oy6S0clR4nGtNEYPoVJ1ZIACX2n459ZriJL1R8yurIpDOuAh5LIH7CWI2v2o1mO_d4cdXg%26v%3D708%26to%3Dhttps%253A%252F%252Fpro.m.jd.com%252Fwq%252Factive%252F413U2tisErrBY8RpbodBfWLSsCR1%252Findex.html%26mobiletype%3D__MOBILETYPE__%26adt%3D1%26didmd5%3D72710e3df592f07f6dbcc04b747f3653%26idfamd5%3D__IDFAMD5__%26idfa%3D__IDFA__\",\"skip_time\":0,\"target_addition\":[\"mv://LogHttp?event=VIEW\\u0026url=https%3A%2F%2Fxiaomi.m.cn.miaozhen.com%2Fx%2Fk%3D2173617%26p%3D7agXt%26dx%3D1%26rt%3D2%26pro%3Ds%26ns%3D117.59.91.57%26ni%3Dxiaomi_10215333_A%26v%3D__LOC__%26xa%3D__ADPLATFORM__%26tr%3D__REQUESTID__%26vg%3D__AUTOPLAY__%26nh%3D__AUTOREFRESH__%26mo%3D0%26m0%3D__OPENUDID__%26m0a%3D__DUID__%26m1%3D__ANDROIDID1__%26m1a%3D__ANDROIDID__%26m2%3D72710e3df592f07f6dbcc04b747f3653%26m4%3D__AAID__%26m5%3D__IDFA__%26m6%3D__MAC1__%26m6a%3D__MAC__%26m11%3D__OAID__%26mn%3D__ANAME__%26o%3D\",\"mv://LogHttp?event=VIEW\\u0026url=http%3A%2F%2Fv2.reachmax.cn%2Fcount%2Frun.php%3Fa%3D72710E3DF592F07F6DBCC04B747F3653%26ver%3D0%26cpid%3D214877%26pub%3D15%26bc%3DaP13H9eLJI%26l%3D2787172%26cid%3D22055730%26plat%3D2%26s%3D1%26meid%3Dc089abc058cb00cd%26rt%3D1592103420%26s_l%3D2787727%26t%3Ds\",\"mv://LogHttp?event=VIEW\\u0026url=http%3A%2F%2Fm.track.ad.xiaomi.com%2Ftask%2Fimp%2F47e83263d4%2Fe1bef0f1aa%2C0a__MAC__%2C0b72710e3df592f07f6dbcc04b747f3653%2C0c__OAID__%2C0d10215333%2C0e__ANDROIDVERSION__%2C0f__PLATFORM__%2C0k1.1.10.5%2C0h\",\"mv://LogHttp?event=CLICK\\u0026url=https%3A%2F%2Fxiaomi.m.cn.miaozhen.com%2Fr%2Fk%3D2173617%26p%3D7agXt%26dx%3D1%26rt%3D2%26pro%3Ds%26ns%3D117.59.91.57%26ni%3Dxiaomi_10215333%26v%3D__LOC__%26xa%3D__ADPLATFORM__%26tr%3D__REQUESTID__%26mo%3D0%26m0%3D__OPENUDID__%26m0a%3D__DUID__%26m1%3D__ANDROIDID1__%26m1a%3D__ANDROIDID__%26m2%3D72710e3df592f07f6dbcc04b747f3653%26m4%3D__AAID__%26m5%3D__IDFA__%26m6%3D__MAC1__%26m6a%3D__MAC__%26m11%3D__OAID__%26mn%3D__ANAME__%26o%3D\",\"mv://LogHttp?event=CLICK\\u0026url=http%3A%2F%2Fv2.reachmax.cn%2Fclick%2Frun.php%3Fa%3D72710E3DF592F07F6DBCC04B747F3653%26ver%3D0%26cpid%3D214877%26pub%3D15%26bc%3DaP13H9eLJI%26l%3D2787172%26cid%3D22055730%26plat%3D2%26s%3D1%26meid%3Dc089abc058cb00cd%26rt%3D1592103420%26s_l%3D2787727\",\"mv://LogHttp?event=VIEW\\u0026url=http%3A%2F%2Fapi.ad.xiaomi.com%2Fiqiyi%2FuploadLogAdSDK%3Fvalue%3DeyJCIjpbeyJCIjp7Il9hY3Rpb25fIjoiVklFVyIsIl9jYXRlZ29yeV8iOiJhZCIsImUiOiJWSUVXIiwiZXgiOiJobWJfakZiZ2tLZU9fLVJSQWxVZ3BEU0F5VXdVNnc4QjJ2WG1vSkZSOF9yYmlOdndZOTJZb1FibWFSaDJJQ2FqcWp3WklXZjVBbXVpUDhmbm5VcEh1MTI2eC11ZmxnOUtmM1pvSXQ4Ni1GUmZMSFFsV2JUMjZFRjdya2dmQnFHLXpHUTBRUmpJNllvU2diVlJ6cWNaNkJjT3VwVG5kQUJzYTVZTVI0YkJMNEVJOFl3X0U1cTdnRWEyc3B1LXlGY1R0SVBLeERZcTNuQ3RkX1k1ZWFHNEpxU3JYTjBVb1hyVE5sYlBjUW1MLXU4WTVrcnN6bVFnNmhIRTZfMXo2RDQ4ejM3dzhvUlVQcWhxZFVacHVXNjI0MldycEVwVTFUWVFKNlBVTmZfRjFJUER5cGdReGFLYzBu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aW1lIjoiMTU5MjEwMzQyMCIsImtleSI6InZpZGVvX2FkbG9nIiwicGsiOiJjb20ubWl1aS52aWRlbyIsInJldHJ5Q250IjowLCJzaWQiOiIiLCJzbiI6IiJ9fV0sIkgiOnsiYWFpZCI6IiIsImFuZHJvaWQiOiIiLCJibiI6IiIsImN2IjoiIiwiZGV2aWNlIjoiIiwiaW1laSI6IjcyNzEwZTNkZjU5MmYwN2Y2ZGJjYzA0Yjc0N2YzNjUzIiwibGFuZyI6IiIsIm1hYyI6IiIsIm1pIjoiIiwibWl1aSI6IiIsIm1vZGVsIjoiIiwibiI6IiIsInByb2R1Y3QiOiIiLCJyZWdpb24iOiIiLCJzZW5kZXIiOiIiLCJzdCI6IjE1OTIxMDM0MjAiLCJ1c2VyaWQiOjB9fQ%3D%3D%26ts%3D1592103420%26nonce%3Da91a12d7ccb4e058680d38aeddd480b0%26sign%3D207c90e809d01cbbfd1646ce8fffd93c\"],\"target_addition1\":null,\"title\":\"\",\"duration\":15,\"image_url\":\"\",\"video_url\":\"https://video.market.xiaomi.com/download/AdCenter/0a22e4176db3294427bcf8d6227d27be3f8420c88/AdCenter0a22e4176db3294427bcf8d6227d27be3f8420c88.mp4\",\"id\":\"10215333\",\"detail_url\":\"\",\"settings\":{\"click_type\":\"0\",\"show_close_at\":-1,\"hide_close_at\":-1,\"log_time\":\"\",\"click_ratio\":0,\"video_finish_ratio\":0,\"video_timer_ratio\":0},\"proportion\":\"1040:467\",\"load_ext\":\"\",\"n_urls\":null,\"buttonName\":\"查看详情\",\"is_download\":\"0\"}],\"eid\":\"ab58:0,ab54:03,ab41:1,ab49:2,ab52:02,ab17:1,ab29:30,ab_feed2:4,ab_feed1:0,ab31:0||\",\"msg\":\"OK\",\"result\":1}"
			return httpmock.NewStringResponse(200, body), nil
		},
	)

	advertiser := "xiaomi"

	err = ads_redis.Setup()

	conn := redigomock.NewConn()
	ads_redis.RedisConn = &redis.Pool{
		// Return the same connection mock for each Get() call.
		Dial:    func() (redis.Conn, error) { return conn, nil },
		MaxIdle: 10,
	}

	conn.Command("GET", "acfv2_{kuxin}_{ads_click}").Expect("{\"channel_flag\": 1, \"weigth\": 100}")
	conn.Command("GET", "acfv2_{kuxin02}_{ads_click}").Expect("{\"channel_flag\": 1, \"weigth\": 100}")
	conn.Command("GET", "adsv2_xiaomi_0").Expect(int64(784))
	conn.Command("GET", "adsv3_xiaomi_VIEW_0").Expect(int64(0))
	conn.Command("GET", "adsv3_xiaomi_VIDEO_TIMER_0").Expect(int64(215))
	conn.Command("GET", "adsv3_xiaomi_CLICK_0").Expect(int64(24))
	conn.Command("GET", "adsv3_xiaomi_VIDEO_FINISH_0").Expect(int64(760))
	conn.Command("GET", "dim2_72710e3df592f07f6dbcc04b747f3653_'223.91.87.241'").Expect(int64(time.Now().Unix()))
	if err != nil {
		fmt.Printf("setup redis failed: %s\n", err)
		return
	}

	adslib.GetConf()

	fmt.Printf("dsp: %+v", dsp)
	addata, err := GetAdsInfos(&dsp, advertiser)
	fmt.Printf("%+v %s", addata, err)
}

