package protocol

import "time"

type Pack_ITSDev struct {
	Header
	Data ITSDevConfig `json:"data"`
}

func (o *Pack_ITSDev) EnCode(gid string, seq uint64, itsdevconfig *ITSDevConfig) (string, error) {
	o.SetHeaderData(gid, gid, seq)
	o.Data = *itsdevconfig
	return json.MarshalToString(o)
}

func (o *Pack_ITSDev) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

type VehicleSpeed struct {
	Plate     string `json:"plate"`
	Time      BJTime `json:"time"`
	Type      int8   `json:"type"`
	Speed     int    `json:"speed"`
	Direction uint   `json:"direction"`
}

type Pack_VehicleSpeed struct {
	Header
	Data VehicleSpeed `json:"data"`
}

func (o *Pack_VehicleSpeed) EnCode(id, gid string, seq uint64, vs *VehicleSpeed) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data = *vs
	return json.MarshalToString(o)
}

func (o *Pack_VehicleSpeed) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

//卡口行车统计信息
type VehicleStatic struct {
	StaticTime        time.Time       `json:"statictime"`
	TollgateID        string          `json:"tollgateid"`
	SliceVehicleSpeed []*VehicleSpeed `json:"vehiclespeed"` //车速度
	MapDirection      map[int8]uint   `json:"direction"`    //按照方向统计
	MapProvince       map[string]uint `json:"province"`     //按照省份统计，如湘
	MapProvinceCity   map[string]uint `json:"provincecity"` //按照省市统计，如湘A
	MapVehicleType    map[int8]uint   `json:"vehicletype"`  //按照车辆类型统计，如大客车、小客车等
}

func NewVehicleStatic() *VehicleStatic {
	obj := VehicleStatic{
		SliceVehicleSpeed: make([]*VehicleSpeed, 0, 1024),
		MapDirection:      make(map[int8]uint),
		MapProvince:       make(map[string]uint),
		MapProvinceCity:   make(map[string]uint),
		MapVehicleType:    make(map[int8]uint),
	}
	return &obj
}

func (o *VehicleStatic) Reset() {
	o.StaticTime = time.Time{}
	o.TollgateID = ""
	o.SliceVehicleSpeed = make([]*VehicleSpeed, 0, 1024)
	o.MapDirection = make(map[int8]uint)
	o.MapProvince = make(map[string]uint)
	o.MapProvinceCity = make(map[string]uint)
	o.MapVehicleType = make(map[int8]uint)
}

func (o *VehicleStatic) AddDirection(direction int8) {
	increInt8Key(o.MapDirection, direction)
}
func (o *VehicleStatic) AddProvince(province string) {
	increStringKey(o.MapProvince, province)
}
func (o *VehicleStatic) AddProvinceCity(provincecity string) {
	increStringKey(o.MapProvinceCity, provincecity)
}
func (o *VehicleStatic) AddVehicleSpeed(data *VehicleSpeed) {
	o.SliceVehicleSpeed = append(o.SliceVehicleSpeed, data)
}
func (o *VehicleStatic) AddVehicleType(vtype int8) {
	increInt8Key(o.MapVehicleType, vtype)
}

func increInt8Key(m map[int8]uint, k int8) {
	if a, ok := m[k]; ok {
		m[k] = a + 1
	} else {
		m[k] = 1
	}
}

func increStringKey(m map[string]uint, k string) {
	if a, ok := m[k]; ok {
		m[k] = a + 1
	} else {
		m[k] = 1
	}
}

type Pack_VehicleStatic struct {
	Header
	Data VehicleStatic `json:"data"`
}

func (o *Pack_VehicleStatic) EnCode(id, gid string, seq uint64, vs *VehicleStatic) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data = *vs
	return json.MarshalToString(o)
}

func (o *Pack_VehicleStatic) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

type ITSState struct {
	Time  string `json:"time"`
	State uint8  `json:"state"` //0 在线，1离线
}

type Pack_ITSState struct {
	Header
	Data ITSState `json:"data"`
}

func (o *Pack_ITSState) EnCode(id, gid string, seq uint64, changetime time.Time, state uint8) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.Time = changetime.Format("2006-01-02 15:04:05")
	o.Data.State = state
	return json.MarshalToString(o)
}

func (o *Pack_ITSState) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}
