package controllers

import (
	"XT_New/enums"
	"XT_New/service"
	"encoding/json"
	"fmt"
	"strings"
	"time"

	"github.com/astaxie/beego"
)

type PublicApiController struct {
	BaseAPIController
}

func PublicApiRegistRouters() {
	beego.Router("/api/public/getadviceconfig", &PublicApiController{}, "Get:GetDoctorAdviceConfig")
	beego.Router("/api/public/servertime", &PublicApiController{}, "Get:GetServerTime")
	beego.Router("/api/app/release", &PublicApiController{}, "Get:AppRelease")
	beego.Router("/api/get", &PublicApiController{}, "Get:GetJson")

}

func (c *PublicApiController) GetDoctorAdviceConfig() {
	drugs, _, _ := service.GetPublicDrugDics()
	drugways, _, _ := service.GetPublicDrugWayDics()
	efs, _, _ := service.GetPublicExecutionFrequencyDics()

	c.ServeSuccessJSON(map[string]interface{}{
		"drugs":    drugs,
		"drugways": drugways,
		"efs":      efs,
	})
}

func (c *PublicApiController) GetServerTime() {
	timeNow := time.Now()
	timeNowStamp := timeNow.Unix()
	timeNowStr := timeNow.Format("2006-01-02 15:04:05")
	c.ServeSuccessJSON(map[string]interface{}{
		"time":      timeNowStr,
		"timestamp": timeNowStamp,
	})

}

func (c *PublicApiController) AppRelease() {
	// appId := c.GetString("appid")
	version := c.GetString("version")
	appType, _ := c.GetInt64("app_type", 0)

	appVersion, err := service.GetAppVersionByAppType(appType)
	if err != nil {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}

	if appVersion == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"version": nil,
			"state":   2,
		})
		return
	}

	state := strings.Compare(version, appVersion.Version)
	// state := 1

	if state < 0 {
		c.ServeSuccessJSON(map[string]interface{}{
			"version": appVersion,
			"state":   appVersion.Status,
		})
		return
	}
	c.ServeSuccessJSON(map[string]interface{}{
		"version": nil,
		"state":   2,
	})
	return

}

func (this *PublicApiController) GetJson() {
	type Global struct {
		DeviceSN        string
		Version         string
		Timestamp       string
		InterfaceSource string
	}
	type RBPResultModel struct {
		Sys         string
		Dia         string
		HR          string
		MeasureTime string
	}

	type ScannerResultModel struct {
		Code string
	}

	type IDCardResultModel struct {
		IDCardNo         string
		UserName         string
		Age              string
		Female           string
		Male             string
		SocialSecurityNo string
	}

	type JsonTemp struct {
		Global             Global
		RBPResultModel     RBPResultModel
		ScannerResultModel ScannerResultModel
		IDCardResultModel  IDCardResultModel
	}

	var person JsonTemp
	err3 := json.Unmarshal([]byte(this.GetString("params")), &person)
	fmt.Println(err3)

	fmt.Println(person.Global)

	//temp := JsonTemp{
	//	Global:Global{
	//		DeviceSN:"1122",
	//		Version:"1122",
	//		Timestamp:"1222",
	//		InterfaceSource:"1233",
	//	},
	//	RBPResultModel: RBPResultModel{
	//		Sys:"12",
	//		Dia:"22",
	//		HR:"23",
	//		MeasureTime:"123",
	//	},
	//	ScannerResultModel:ScannerResultModel{
	//		Code:"1233",
	//	},
	//	IDCardResultModel:IDCardResultModel{
	//		IDCardNo:"123",
	//		UserName:"123",
	//		Age:"123",
	//		Female:"123",
	//		Male:"123",
	//		SocialSecurityNo:"123",
	//	},
	//}
	//
	//jsonBytes, err := json.Marshal(temp)
	//if err != nil {
	//	fmt.Println(err)
	//}
	//fmt.Println(string(jsonBytes))

}
