import Store from '@/store'
import {
	parseTime,
} from '@/utils/ruoyi'


export const ShipAnalog = {
	"主机转速": ["005", "045", "025", "05q"],
	"主机冷却水水温": ["000", "040", "020", "05c"],
	"主机滑油压力": ["004", "044", "024", "05g"],
	"主机润滑油温度": ["002", "042", "022", "05d"],
	"推进齿轮箱滑油压力": ["0MC", "0MG", "0ME", "05o"],
	"艉轴温度": ["01D", "05D", "03D", "没有"],
	"蓄电池电压": ["009", "049", "029", "05m"],
	"主机累计运行时间": ["00B", "04B", "02B", "05r"],
	"艉轴振动": ["00D", "04D", "02D", "没有"],
	"当日运转时数": ["0CG", "0CG", "0CG", "0CG"],
	//
	"主机启动/停止状态": ["01B", "05B", "03B", "05w"],
	"主机水温淡水高报警": ["01B", "05B", "03B", "05w"],
	"主机超速报警": ["01B", "05B", "03B", "05w"],
	"主机滑油油压低报警": ["01B", "05B", "03B", "05w"],
	"主机滑油油位低报警": ["01B", "05B", "03B", "05w"],
	"主机蓄电池电压低报警": ["01B", "05B", "03B", "05w"],
	"主机急停报警": ["01B", "05B", "03B", "05w"],
	"主机水温高（淡水）停车报警": ["01B", "05B", "03B", "05w"],
	"主机滑油油压低停车报警": ["01B", "05B", "03B", "05w"],
	"主机超速停车报警": ["01B", "05B", "03B", "05w"],
	"主机遥控 AC220 失电报警": ["01B", "05B", "03B", "05w"],
	"主机遥控 DC24 失电报警": ["01B", "05B", "03B", "05w"],
	"推进齿轮箱正/空/倒车状态": ["01B", "05B", "03B", "05w"],
	"推进齿轮箱滑油压力低报警": ["01B", "05B", "03B", "05w"],
	"推进齿轮箱错向报警": ["01B", "05B", "03B", "05w"],
	"艉轴油温高报警": ["01B", "05B", "03B", "05w"],

	//
	"发电机电压": ["06B", "0AB", "08B", "0SB"],
	"发电机电流": ["06A", "0AA", "08A", "0SA"],
	"发电机频率": ["06M", "0AM", "08M", "0SM"],
	"发电机转速": ["06H", "0AH", "08H", "0SH"],
	"发电机冷却水水温": ["060", "0A0", "080", "0S0"],
	"发电机滑油压力": ["064", "0A4", "084", "0S4"],
	"启动蓄电池电压": ["066", "0A6", "086", "0S6"],
	"发电机累计运行时间": ["068", "0A8", "088", "0S8"],
	//
	"发电机冷却水水温高报警": ["06N", "0AN", "08N", "0BN"],
	"发电机滑油压力低报警": ["06N", "0AN", "08N", "0BN"],
	"发电机过载报警": ["06N", "0AN", "08N", "0BN"],
	"启动蓄电池电压低报警": ["06N", "0AN", "08N", "0BN"],
	"发电机超速报警": ["06N", "0AN", "08N", "0BN"],
	//
	"舵机失电": ["Q03"],
	"舵机断相": ["Q03"],
	"舵机过载": ["Q03"],
	"舵机油柜油位低": ["Q03"],
	"舵机滤器堵塞": ["Q03"],
	"遥控主电失电": ["Q03"],
	"遥控备电失电": ["Q03"],
	"舵机综合报警": ["Q03"],
	//
	"电站电压": ["没有"],
	"电站电流": ["没有"],
	"岸电电压": ["0CA"],
	"岸电电流": ["0CB"],
	"岸电频率": ["0CC"],
	"经度": ["0a4"],
	"纬度": ["0a5"],
	"对地航速": ["0a9"],
	"船艏向": ["0aA"],
	"船舶横倾": ["0aG"],
	"船舶纵倾": ["0aH"],
	"船舶吃水": ["没有"],
	"油舱油位（高度）": ["0K1", "0K2"],
	"油舱库存（体积）": ["0K1", "0K2"],
	"油舱库存（重量）": ["0K1", "0K2"],
	"机舱环境温度": ["0c0"],
	"机舱供电电压": ["0c1"],
	"甲板环境温度": ["没有"],
	"污水瞬时流量": ["0R1"],
	"污水累积排放量": ["0R0"],
	"舵机角度": ["0C3"],
	"主配电板绝缘低报警": ["Q03"],
	"主配电板汇流排失电": ["Q03"],
	"机舱舱底水报警": ["Q03"],
	// "艉轴温度": ["01D", "05D", "03D", "没有"],
	// "污水排放": ["0R0"],

}


export const HostAnalog = {
	"主机转速": ShipAnalog["主机转速"],
	"主机冷却水水温": ShipAnalog["主机冷却水水温"],
	"主机滑油压力": ShipAnalog["主机滑油压力"],
	"主机润滑油温度": ShipAnalog["主机润滑油温度"],
	"推进齿轮箱滑油压力": ShipAnalog["推进齿轮箱滑油压力"],
	"艉轴温度": ShipAnalog["艉轴温度"],
	"蓄电池电压": ShipAnalog["蓄电池电压"],
	"主机累计运行时间": ShipAnalog["主机累计运行时间"],
	"艉轴振动": ShipAnalog["艉轴振动"],
	"当日运转时数": ShipAnalog["当日运转时数"],
}

export const DynamoAnalog = {
	"发电机电压": ShipAnalog["发电机电压"],
	"发电机电流": ShipAnalog["发电机电流"],
	"发电机频率": ShipAnalog["发电机频率"],
	"发电机转速": ShipAnalog["发电机转速"],
	"发电机冷却水水温": ShipAnalog["发电机冷却水水温"],
	"发电机滑油压力": ShipAnalog["发电机滑油压力"],
	"启动蓄电池电压": ShipAnalog["启动蓄电池电压"],
	"发电机累计运行时间": ShipAnalog["发电机累计运行时间"],
}

export const ShipAnalogDescription = {
	"主机转速": {
		"unit": "RPM",
		"min": 0,
		"max": 3600,
	},
	"主机冷却水水温": {
		"min": 0,
		"max": 120,
		"unit": "℃",
	},
	"主机滑油压力": {
		"min": 0,
		"max": 600,
		"unit": "kPa",
	},
	"主机润滑油温度": {
		"min": 0,
		"max": 120,
		"unit": "℃",
	},
	"推进齿轮箱滑油压力": {
		"min": 0,
		"max": 120,
		"unit": "kPa",
	},
	"艉轴温度": {
		"min": 0,
		"max": 120,
		"unit": "℃",
	},
	"蓄电池电压": {
		"min": 0,
		"max": 36,
		"unit": "V",
	},
	"主机累计运行时间": {
		"min": 0,
		"max": 120,
		"unit": "hr",
	},
	//
	"当日运转时数": {
		"unit": "hr",
	},
	//
	"主机启动/停止状态": ["01B", "05B", "03B", "05w"],
	"主机水温淡水高报警": ["01B", "05B", "03B", "05w"],
	"主机超速报警": ["01B", "05B", "03B", "05w"],
	"主机滑油油压低报警": ["01B", "05B", "03B", "05w"],
	"主机滑油油位低报警": ["01B", "05B", "03B", "05w"],
	"主机蓄电池电压低报警": ["01B", "05B", "03B", "05w"],
	"主机急停报警": ["01B", "05B", "03B", "05w"],
	"主机水温高（淡水）停车报警": ["01B", "05B", "03B", "05w"],
	"主机滑油油压低停车报警": ["01B", "05B", "03B", "05w"],
	"主机超速停车报警": ["01B", "05B", "03B", "05w"],
	"主机遥控 AC220 失电报警": ["01B", "05B", "03B", "05w"],
	"主机遥控 DC24 失电报警": ["01B", "05B", "03B", "05w"],
	"推进齿轮箱正/空/倒车状态": ["01B", "05B", "03B", "05w"],
	"推进齿轮箱滑油压力低报警": ["01B", "05B", "03B", "05w"],
	"推进齿轮箱错向报警": ["01B", "05B", "03B", "05w"],
	"艉轴油温高报警": ["01B", "05B", "03B", "05w"],
	//
	"发电机电压": {
		"min": 0,
		"max": 500,
		"unit": "V",
	},
	"发电机电流": {
		"min": 0,
		"max": 5,
		"unit": "A",
	},
	"发电机频率": {
		"min": 0,
		"max": 5,
		"unit": "Hz",
	},
	"发电机转速": {
		"min": 0,
		"max": 3600,
		"unit": "RPM",
	},
	"发电机冷却水水温": {
		"min": 0,
		"max": 150,
		"unit": "℃",
	},

	"发电机滑油压力": {
		"min": 0,
		"max": 500,
		"unit": "kPa",
	},
	"启动蓄电池电压": {
		"min": 0,
		"max": 36,
		"unit": "V",
	},

	"发电机累计运行时间": {
		"min": 0,
		"max": 120,
		"unit": "hr",
	},
	//
	"发电机冷却水水温高报警": ["06N", "0AN", "08N", "0BN"],
	"发电机滑油压力低报警": ["06N", "0AN", "08N", "0BN"],
	"发电机过载报警": ["06N", "0AN", "08N", "0BN"],
	"启动蓄电池电压低报警": ["06N", "0AN", "08N", "0BN"],
	"发电机超速报警": ["06N", "0AN", "08N", "0BN"],
	//
	"舵机失电": ["Q03"],
	"舵机断相": ["Q03"],
	"舵机过载": ["Q03"],
	"舵机油柜油位低": ["Q03"],
	"舵机滤器堵塞": ["Q03"],
	"遥控主电失电": ["Q03"],
	"遥控备电失电": ["Q03"],
	"舵机综合报警": ["Q03"],
	//
	"电站电压": ["没有"],
	"电站电流": ["没有"],
	"岸电电压": {
		"min": 0,
		"max": 480,
		"unit": 'v',
	},
	"岸电电流": {
		"min": 0,
		"max": 36,
		"unit": 'A',
	},
	"岸电频率": {
		"min": 0,
		"max": 120,
		"unit": 'Hz',
	},
	"经度": {
		"unit": '°',
	},
	"纬度": {
		"unit": '°',
	},
	"对地航速": {
		"unit": 'kn',
	},
	"船艏向": {
		"unit": '°',
	},
	"船舶横倾": {
		"unit": '°',
	},
	"船舶纵倾": {
		"unit": '°',
	},
	"船舶吃水": ["没有"],
	"油舱油位（高度）": {
		"min": 0,
		"max": 3,
		"unit": 'm',
	},
	"油舱库存（体积）": {
		"min": 0,
		"max": 15000,
		"unit": 'L',
	},
	"油舱库存（重量）": {
		"min": 0,
		"max": 15000,
		"unit": 'kg',
	},
	"机舱环境温度": {
		"min": 0,
		"max": 50,
		"unit": '℃',
	},
	"机舱供电电压": {
		"min": 0,
		"max": 48,
		"unit": 'v',
	},
	"甲板环境温度": ["没有"],
	"污水瞬时流量": {
		"min": 0,
		"max": 50,
		"unit": 'L/h',
	},
	"污水累积排放量": {
		"unit": 'L',
	},
	"舵机角度": {
		"min": -40,
		"max": 40,
		"unit": '°',
	},
	"主配电板绝缘低报警": ["Q03"],
	"主配电板汇流排失电": ["Q03"],
	"机舱舱底水报警": ["Q03"],
	"艉轴振动": {
		"min": 0,
		"max": 25,
		"unit": "mm/s",
	},
	"艉轴温度": {
		"min": 0,
		"max": 120,
		"unit": "℃",
	},
	// "污水排放": {
	//   "unit": 'L',
	// },
}

export function getHostExist(analog) {
	let keys = Object.keys(HostAnalog)
	let host0 = false
	let host1 = false
	let host2 = false
	let host3 = false
	for (let item of keys) {
		// console.log(
		//   HostAnalog[item][0],
		//   HostAnalog[item][1],
		//   HostAnalog[item][2],
		//   HostAnalog[item][3],
		//   analog[HostAnalog[item][0]],
		//   analog[HostAnalog[item][1]],
		//   analog[HostAnalog[item][2]],
		//   analog[HostAnalog[item][3]],
		// )
		if (analog[HostAnalog[item][0]] !== undefined) {
			host0 = true
		}
		if (analog[HostAnalog[item][1]] !== undefined && HostAnalog[item][1] != "0CG") {
			host1 = true
		}
		if (analog[HostAnalog[item][2]] !== undefined && HostAnalog[item][2] != "0CG") {
			host2 = true
		}
		if (analog[HostAnalog[item][3]] !== undefined && HostAnalog[item][3] != "0CG") {
			host3 = true
		}
	}
	return [host0, host1, host2, host3]
}

export function getHostName(hostExist) {
	let hostSum = hostExist.filter(item => item == true).length
	if (hostSum == 1) {
		return ["1号主机", "主机", "主机", "主机"]
	} else if (hostSum == 2) {
		return ["1号主机", "主机", "2号主机", "主机"]
	} else if (hostSum == 3) {
		return ["1号主机", "2号主机", "3号主机", "主机"]
	} else if (hostSum == 4) {
		return ["1号主机", "2号主机", "3号主机", "4号主机"]
	}
}


export function getDynamoExist(analog) {
	let keys = Object.keys(DynamoAnalog)
	let dynamo0 = false
	let dynamo1 = false
	let dynamo2 = false
	let dynamo3 = false
	for (let item of keys) {
		// console.log(
		//   DynamoAnalog[item][0],
		//   DynamoAnalog[item][1],
		//   DynamoAnalog[item][2],
		//   DynamoAnalog[item][3],
		//   analog[DynamoAnalog[item][0]],
		//   analog[DynamoAnalog[item][1]],
		//   analog[DynamoAnalog[item][2]],
		//   analog[DynamoAnalog[item][3]],
		// )
		if (analog[DynamoAnalog[item][0]] !== undefined) {
			dynamo0 = true
		}
		if (analog[DynamoAnalog[item][1]] !== undefined) {
			dynamo1 = true
		}
		if (analog[DynamoAnalog[item][2]] !== undefined) {
			dynamo2 = true
		}
		if (analog[DynamoAnalog[item][3]] !== undefined) {
			dynamo3 = true
		}
	}
	// console.log(`---------------------------------------------------`)
	return [dynamo0, dynamo1, dynamo2, dynamo3]
}

export function getDynamoName(dynamoExist) {
	let dynamoSum = dynamoExist.filter(item => item == true).length
	if (dynamoSum == 1) {
		return ["1号发电机", "发电机", "发电机", "发电机"]
	} else if (dynamoSum == 2) {
		return ["1号发电机", "发电机", "2号发电机", "发电机"]
	} else if (dynamoSum == 3) {
		return ["1号发电机", "2号发电机", "3号发电机", "发电机"]
	} else if (dynamoSum == 4) {
		return ["1号发电机", "2号发电机", "3号发电机", "4号发电机"]
	}
}


export function getAnalogData(analog, name, index) {
	let value = analog[ShipAnalog[name][index]]

	if (name == "主机转速" && value == null) {
		value = 0
	} else if (name == "当日运转时数" && value != null) {
		value = (Number(value) / 3600).toFixed(2)
	}

	if (name == "发电机电压" && value == null) {
		value = 0
	} else if (name == "发电机电流" && value == null) {
		value = 0
	} else if (name == "发电机频率" && value == null) {
		value = 0
	} else if (name == "发电机转速" && value == null) {
		value = 0
	}
	return {
		...ShipAnalogDescription[name],
		value,
		name
	}
}


export const isOffline = (analog) =>
	new Date().getTime() - new Date(parseTime(analog.time)).getTime() >
	Store.state.data.timeout;

export const isOnline = (analog) =>
	new Date().getTime() - new Date(parseTime(analog.time)).getTime() <=
	Store.state.data.timeout;