package common

import (
	"math/rand"
	"strconv"
	"strings"
	"time"
)

// Operation type 操作类型
const (
	OperationDefault          = iota
	OperationLogin            //登录
	OperationLogout           //注销
	OperationCreate           //新增
	OperationUpdate           //修改
	OperationRemove           //删除
	OperationImport           //导入导出
	OperationLightStrategy    //开关灯控策略
	OperationOrderCreate      //工单发起
	OperationOrderChange      //工单转派
	OperationOrderHandle      //工单处理
	OperationProgramPublish   //节目发布待审核
	OperationProgramResult    //节目处理结果
	OperationStrategyRelation //策略关联
	OperationControl          //操作
	OperationAlarmHandle      //告警处理

	OperationSuccess = 0
	OperationFail    = 1
)

const (
	ModuleTypeDefault        = iota //操作记录模块
	ModuleTypeDevice                //设备台账管理
	ModuleTypeInfoBar               //信息发布系统
	ModuleTypeOrder                 //工单运维
	ModuleTypeNotification          //通知公告
	ModuleTypeLightStrategy         //照明策略
	ModuleTypeLighting              //智慧照明系统-智能照明
	ModuleTypeSystem                //系统管理
	ModuleTypeAlarm                 //告警管理
	ModuleTypeOperation             //运营分析
	ModuleTypeRecord                //记录处理
	ModuleTypeWisdomLighting        //智能感应照明系统-灯随车走照明控制
)

// deviceType 设备类型 1** 交通 2** 农业
const (
	DeviceTypeDefault        = 0   //--
	DeviceTypeCamera         = 100 //摄像头
	DeviceTypeLightControl   = 101 //灯控
	DeviceTypeInfoBoard      = 102 //信息屏
	DeviceTypeSwitchBox      = 103 //配电箱
	DeviceTypeGateway        = 104 //网关
	DeviceTypeOptoSensor     = 105 // 环境监测
	DeviceTypeZigbee         = 106 //ZigBee
	DeviceTypeAlarmTerminal  = 107 //一键告警终端
	DeviceTypeAlarmServer    = 108 //一键告警服务端
	DeviceTypeBridgeSensor   = 109 //桥梁传感器
	DeviceTypeBridge         = 110 //桥梁
	DeviceTypeIPBroadcast    = 111 //ip音柱
	DeviceTypeManholeCover   = 112 //井盖
	DeviceTypeGarbage        = 113 //垃圾桶
	DeviceTypeLampPoleGroup  = 114 //灯杆分组
	DeviceTypeLampPole       = 115 //灯杆
	DeviceTypeCaptureUnit    = 116 //抓拍单元
	DeviceTypePoint          = 117 //卡口
	DeviceTypeGarbageGroup   = 118 //垃圾桶分组
	DeviceTypeLightStrategy  = 119 //灯控策略
	DeviceTypeOnDemandGroup  = 120 //灯随车走分组
	DeviceTypeOnDemandSensor = 121 //灯随车走传感器
	DeviceTypeTransformer    = 122 //变压器
)

var mLocation *time.Location

func init() {
	loc, err := time.LoadLocation("Asia/Shanghai")
	if err != nil {
		mLocation = time.FixedZone("CST", 8*3600)
	} else {
		mLocation = loc
	}
}

func GetDeviceObject(id int, name string) string {
	return strconv.Itoa(id) + "(" + name + ")"
}

func StringToInt(id string) int {
	if id != "" {
		id, err := strconv.Atoi(id)
		if err == nil {
			return id
		}
	}
	return -1
}

func RandomString(n int) string {
	var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
	rand.Seed(time.Now().Unix())
	b := make([]rune, n)
	for i := range b {
		b[i] = letters[rand.Intn(len(letters))]
	}
	return string(b)
}

func RandomString2(n int) string {
	var letters = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
	rand.Seed(time.Now().Unix())
	b := make([]rune, n)
	for i := range b {
		b[i] = letters[rand.Intn(len(letters))]
	}
	return string(b)
}

func StringToInt64Array(str string) []int64 {
	tmp := strings.Split(str, ",")
	var result []int64
	for _, t := range tmp {
		i, _ := strconv.ParseInt(t, 10, 64)
		result = append(result, i)
	}
	return result
}

func StringToIntArray(str string) []int {
	tmp := strings.Split(str, ",")
	var result []int
	for _, t := range tmp {
		i, _ := strconv.Atoi(t)
		result = append(result, i)
	}
	return result
}

func MlParseTime(strTime string) (time.Time, error) {
	if strings.Contains(strTime, ".") {
		return time.ParseInLocation("2006-01-02 15:04:05.000", strTime, mLocation)
	}
	return time.ParseInLocation("2006-01-02 15:04:05", strTime, mLocation)
}

func IsAdmin(roleId int64) bool {
	return roleId == 1
}