package customization

import (
	"encoding/json"
	"errors"
	"fmt"
	http2 "net/http"
	"net/url"
	"netease_control_center/globals"
	"netease_control_center/interfaces/http"
	"netease_control_center/interfaces/log"
	"strings"
	"time"
)

const (
	ChinaTelecomUrl = "http://pdm.tydevice.com"
	ChinaTelecomTUI = "18000120"
)

var (
	ChinaTelecomHasReport = false
)

type ChinaTelecomReq struct {
	VER       string `json:"VER"`
	CTEI      string `json:"CTEI"`
	MAC       string `json:"MAC"`
	IP        string `json:"IP"`
	UPLINKMAC string `json:"UPLINKMAC"`
	LINK      string `json:"LINK"`
	FWVER     string `json:"FWVER"`
	DATE      string `json:"DATE"`
}

type ChinaTelecomRsp struct {
	StatusCode int    `json:"Status Code"`
	ErrMsg     string `json:"errmsg"`
	ErrCode    int    `json:"errcode"`
}

func ChinaTelecomReport() error {
	if ChinaTelecomHasReport == true {
		return nil
	}

	var req ChinaTelecomReq
	var rsp ChinaTelecomRsp
	var final string
	defer func() {
		log.Printf(log.DebugLog, "China telecom post, body(%s), ret(%+v)\n", final, rsp)
	}()

	req.VER = "01"
	req.CTEI = globals.CTEI
	req.MAC = globals.WifiMac
	req.IP = globals.WifiIp
	req.UPLINKMAC = ""
	req.LINK = "2"
	req.FWVER = *globals.GitTag
	req.DATE = time.Now().Format("2006-01-02 15:04:05")

	if data, err := json.Marshal(req); err == nil {
		final = "jsonstr:" + string(data)
		tmp := make(url.Values)

		tmp.Set("jsonstr", string(data))
		final = fmt.Sprintf("%+v", tmp)

		if response, err := http.GetDefaultClient().PostForm(ChinaTelecomUrl, tmp); err == nil {
			if err = json.NewDecoder(response.Body).Decode(&rsp); err == nil && rsp.StatusCode == 200 {
				ChinaTelecomHasReport = true
				return nil
			} else {
				log.Println(log.ErrorLog, "Decode err:", err, rsp)
				return errors.New(fmt.Sprintf("err:%+v, ret:%+v", err, rsp))
			}
		} else {
			log.Println(log.ErrorLog, "Post err:", err)
			return err
		}
	} else {
		log.Println(log.ErrorLog, "json err:", err)
		return err
	}
}

func ChinaTelecomReport_Get() error {
	if ChinaTelecomHasReport == true {
		return nil
	}

	var req ChinaTelecomReq
	var rsp ChinaTelecomRsp
	var final string
	defer func() {
		log.Printf(log.DebugLog, "China telecom post, body(%s), ret(%+v)\n", final, rsp)
	}()

	req.VER = "01"
	req.CTEI = globals.CTEI
	req.MAC = globals.WifiMac
	req.IP = globals.WifiIp
	req.UPLINKMAC = ""
	req.LINK = "2"
	req.FWVER = *globals.GitTag
	req.DATE = time.Now().Format("2006-01-02 15:04:05")

	if data, err := json.Marshal(req); err == nil {
		final = "jsonstr:" + string(data)
		tmp := make(url.Values)

		tmp.Set("jsonstr", string(data))
		final = fmt.Sprintf("%+v", tmp)

		req, _ := http2.NewRequest("POST", ChinaTelecomUrl, strings.NewReader(tmp.Encode()))
		q := req.URL.Query()
		q.Add("jsonstr", string(data))
		req.URL.RawQuery = q.Encode()

		if response, err := http.GetDefaultClient().Do(req); err == nil {
			if err = json.NewDecoder(response.Body).Decode(&rsp); err == nil && rsp.StatusCode == 200 {
				ChinaTelecomHasReport = true
				return nil
			} else {
				log.Println(log.ErrorLog, "Decode err:", err)
				return errors.New(fmt.Sprintf("err:%+v, ret:%+v", err, rsp))
			}
		} else {
			log.Println(log.ErrorLog, "Post err:", err)
			return err
		}
	} else {
		log.Println(log.ErrorLog, "json err:", err)
		return err
	}
}
