package test

import (
	"bytes"
	"gd_management_gateway/rpc_apis/gd_management"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
	"testing"
	"time"
)

const (
	HOST  = "http://devmanage.sygd.com/api/v1"
	TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEsImxldmVsIjoyLCJleHAiOjE2Mzc5MDEzMTYsImlhdCI6MTYzNzg5NDExNiwibmJmIjoxNjM3ODk0MTE2LCJzdWIiOiIxIn0.fVyEBm16JVwSyvApFm_lKY7xahvpnksh7I7UR5F7blU"
)

func concatGetURL(url string, data map[string]string) string {
	if data == nil {
		return url
	}

	for k, v := range data {
		url = url + "&" + k + "=" + v
	}

	url = strings.Replace(url, "&", "?", 1)
	return url
}

func Get(api string, query map[string]string) error {
	client := &http.Client{}
	client.Timeout = 10 * time.Second
	req, err := http.NewRequest("GET", concatGetURL(HOST+api, query), nil)
	if err != nil {
		return err
	}
	req.Header.Set("token", TOKEN)
	req.Header.Set("uid", "1")
	req.Header.Set("Content-Type", "application/json")
	resp, err := client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	result, err := ioutil.ReadAll(resp.Body)
	fmt.Println(string(result), " error: ", err)
	return err
}

var postData = `{
    "list":[
        {
            "title":"发动机号",
            "key":"engine_no",
            "value":"FD9D01538",
            "type":"string"
        },
        {
            "title":"车辆型号",
            "key":"model_no",
            "value":"SQR7110S116",
            "type":"string"
        },
        {
            "title":"所有人",
            "key":"owner",
            "value":"dddddddd",
            "type":"string"
        },
        {
            "title":"车牌号",
            "key":"plate_no",
            "value":"川A78954",
            "type":"string"
        },
        {
            "title":"车身颜色",
            "key":"vehicle_body_color",
            "value":"",
            "type":"string"
        },
        {
            "title":"初次登记日期",
            "key":"initial_registration_date",
            "value":"2020-01-19 10:36:37",
            "type":"string"
        },
        {
            "title":"使用性质描述",
            "key":"use_property_detail",
            "value":"非营业个人",
            "type":"string"
        },
        {
            "title":"检验有效期止",
            "key":"inspection_result_effective_to",
            "value":"",
            "type":"string"
        },
        {
            "title":"出厂日期",
            "key":"release_date",
            "value":"2019-05-21 00:00:00",
            "type":"string"
        },
        {
            "title":"车辆状态",
            "key":"state",
            "value":"",
            "type":"string"
        },
        {
            "title":"车身颜色描述",
            "key":"vehicle_body_color_detail",
            "value":"金杏黄",
            "type":"string"
        },
        {
            "title":"轴距",
            "key":"wheel_base",
            "value":"2340",
            "type":"int"
        },
        {
            "title":"强制报废期止",
            "key":"compulsory_scrap_to",
            "value":"",
            "type":"string"
        },
        {
            "title":"发动机型号",
            "key":"engine_type",
            "value":"SQR472F",
            "type":"string"
        },
        {
            "title":"装备质量",
            "key":"unladen_mass",
            "value":"890",
            "type":"int"
        },
        {
            "title":"车架号",
            "key":"vin",
            "value":"LVVDB12A99D139899",
            "type":"string"
        },
        {
            "title":"核定载重量",
            "key":"approved_load",
            "value":"0",
            "type":"int"
        },
        {
            "title":"核定载客",
            "key":"approved_number",
            "value":"5",
            "type":"int"
        },
        {
            "title":"轴数",
            "key":"axle_number",
            "value":"2",
            "type":"int"
        },
        {
            "title":"后轮距",
            "key":"back_wheel_distance",
            "value":"1260",
            "type":"int"
        },
        {
            "title":"燃料种类描述",
            "key":"fuel_type_detail",
            "value":"汽油",
            "type":"string"
        },
        {
            "title":"总质量",
            "key":"gross_mass",
            "value":"1265",
            "type":"int"
        },
        {
            "title":"最近一次交强险投保年月",
            "key":"last_compulsory_insurance_date",
            "value":"2021-06",
            "type":"string"
        },
        {
            "title":"功率",
            "key":"rated_power",
            "value":"500",
            "type":"int"
        },
        {
            "title":"使用性质",
            "key":"use_property",
            "value":"U",
            "type":"string"
        },
        {
            "title":"品牌名",
            "key":"brand_name",
            "value":"宝马汽车",
            "type":"string"
        },
        {
            "title":"排量",
            "key":"displacement",
            "value":"1083",
            "type":"int"
        },
        {
            "title":"前轮距",
            "key":"front_wheel_distance",
            "value":"1295",
            "type":"int"
        },
        {
            "title":"燃料种类",
            "key":"fuel_type",
            "value":"汽油",
            "type":"string"
        },
        {
            "title":"首保日期",
            "key":"insurance_first_date",
            "value":"2009-05-27 00:00:00",
            "type":"string"
        },
        {
            "title":"号牌类型",
            "key":"plate_type",
            "value":"02",
            "type":"string"
        },
        {
            "title":"车辆状态描述",
            "key":"state_detail",
            "value":"",
            "type":"string"
        },
        {
            "title":"车辆类型",
            "key":"vehicle_type",
            "value":"K33",
            "type":"string"
        },
        {
            "title":"车辆类型描述",
            "key":"vehicle_type_detail",
            "value":"微型车",
            "type":"string"
        }
    ],
    "id":0,
    "search_code":"102"
}`

func Put(api string, data string) error {
	client := &http.Client{}
	client.Timeout = 10 * time.Second

	req, err := http.NewRequest("PUT", HOST+api, bytes.NewBuffer([]byte(data)))
	if err != nil {
		return err
	}
	req.Header.Set("token", TOKEN)
	req.Header.Set("uid", "1")
	req.Header.Set("Content-Type", "application/json")
	resp, err := client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	result, err := ioutil.ReadAll(resp.Body)
	fmt.Println(string(result), " error: ", err)
	return err
}

func Post(api string, data string) error {
	client := &http.Client{}
	client.Timeout = 10 * time.Second

	req, err := http.NewRequest("POST", HOST+api, bytes.NewBuffer([]byte(data)))
	if err != nil {
		return err
	}
	req.Header.Set("token", TOKEN)
	req.Header.Set("uid", "1")
	req.Header.Set("Content-Type", "application/json")
	resp, err := client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	result, err := ioutil.ReadAll(resp.Body)
	fmt.Println(string(result), " error: ", err)
	return err
}
func TestGetDb(t *testing.T) {
	err := Get("/data_editor/db", nil)
	fmt.Println("err:", err)
}

func TestGetTitle(t *testing.T) {
	var reqMap = map[string]string{
		"db_code": "100",
	}
	err := Get("/data_editor/title", reqMap)
	fmt.Println("err:", err)
}

/*
var keys = `[
    {
        "search_key":"plate_no",
        "search_name":"车牌号",
        "search_value":"川A12345"
    },
    {
        "search_key":"plate_type",
        "search_name":"号牌种类",
        "search_value":"02"
    }
]`*/

var image_b64 = "[encoded data, 131812 characters]"

func TestGetVehicle(t *testing.T) {
	var keys []gd_management.Search
	search := gd_management.Search{}
	search.SearchKey = "plate_no"
	search.SearchValue = "川A12345"
	keys = append(keys, search)
	search.SearchKey = "plate_type"
	search.SearchValue = "02"
	keys = append(keys, search)
	req1 := gd_management.ManagementGetDataSupplementReq{}
	req1.Keys = keys
	req1.SearchCode = "102"
	req, _ := json.Marshal(req1)

	err := Put("/data_editor/data", string(req))
	fmt.Println("err:", err)
}

func TestGetVehicleByLicense(t *testing.T) {
	var keys []gd_management.Search
	search := gd_management.Search{}
	search.SearchKey = "image_b64"
	search.SearchValue = image_b64
	keys = append(keys, search)
	req1 := gd_management.ManagementGetDataSupplementReq{}
	req1.Keys = keys
	req1.SearchCode = "104"
	req, _ := json.Marshal(req1)
	err := Put("/data_editor/data", string(req))
	fmt.Println("err:", err)
}

func TestUpdateVehicle(t *testing.T) {

	err := Post("/data_editor/data", postData)
	fmt.Println("err:", err)
}
