import * as React from 'react';
import Request from '../utils/request';
export interface DeviceListItem {
	id: number;
	apply_time: string;
	approve_time: string;
	key: string;
	name: string;
	project_name: string;
	provider_name: string;
	safety_record_no: string;
	sn: string;
	social_code: string;
	state: number;
	status: number;
	type_code: number;
	type_name: string;
	channel_no: string;
	channel_name: string;
}
export interface DeviceListParams {
	page?: number;
	filter?: string;
	pageSize?: number;
	status?: -1 | 0 | 1 | 2; // 设备状态(-1 全部 0 未审核 1 已通过 2 拒绝)
}

/**
 *
 *
 * @description 设备列表
 */
export const useDeviceList = () => {
	const [state, setState] = React.useState<{
		loading: boolean;
		dataSource: DeviceListItem[];
		total: number;
		page: number;
	}>({
		loading: false,
		dataSource: [],
		total: 0,
		page: 1,
	});
	const request = (params?: DeviceListParams) => {
		setState((preState) => {
			return {
				...preState,
				loading: true,
			};
		});
		Request.sendRequest({
			url: '/v1/device/list',
			params: {
				...params,
			},
		})
			.then((data: any) => {
				setState((preState) => {
					return {
						...preState,
						...data,
						loading: false,
						dataSource: data.list,
					};
				});
			})
			.catch((error) => {
				setState((preState) => ({ ...preState, loading: false }));
			});
	};
	return { state, request } as const;
};
/**
 * @description 设备绑定摄像头
 */
export const useBindChanle = () => {
	const [state, setState] = React.useState({
		loading: false,
	});
	const request = (data: { channel_id: number; device_id: number }) => {
		setState((preState) => ({ ...preState, loading: true }));
		return Request.sendRequest({
			url: '/v1/camera/bind_device',
			method: 'PUT',
			data,
		})
			.then(() => {})
			.finally(() => {
				setState((preState) => ({ ...preState, loading: false }));
			});
	};
	return { state, request } as const;
};
/**
 * @description 解绑摄像头
 */
export const useUnBindChanle = () => {
	const [state, setState] = React.useState({
		loading: false,
	});
	const request = (data: { device_id: number; device_code: number }) => {
		setState((preState) => ({ ...preState, loading: true }));
		return Request.sendRequest({
			url: '/v1/camera/unbind_device',
			method: 'PUT',
			data,
		})
			.then(() => {})
			.finally(() => {
				setState((preState) => ({ ...preState, loading: false }));
			});
	};
	return { state, request } as const;
};
/**
 * @description 设备类型列表
 */
export const useDeviceStatistics = () => {
	const [state, setState] = React.useState<{
		loading: boolean;
		dataSource: {
			offline: number;
			online: number;
			total: number;
			device_type: string;
		}[];
	}>({
		loading: false,
		dataSource: [],
	});
	const request = () => {
		setState((preState) => ({ ...preState, loading: true }));
		Request.sendRequest({
			url: '/v1/project/device_statistics',
		})
			.then((data: any) => {
				setState((preState) => ({
					...preState,
					loading: false,
					dataSource: data.list.map((element) => {
						return {
							...element,
							offline: element.total - element.online,
						};
					}),
				}));
			})
			.catch((error) => {
				setState((preState) => ({ ...preState, loading: false }));
			});
	};
	return [state, request] as const;
};
type WarningSearchState = 0 | 1 | 2;

/**
 * @description 预警信息
 */
export const useWarningList = () => {
	const [state, setState] = React.useState<{
		loading: boolean;
		deepData: {
			type: WarningSearchState;
			sn: string;
			page: number;
			timestamp: number;
		};
		preveDataSorce: {
			name: string;
			time: string;
			reason: string;
			id: number;
		}[];
		dataSource: { name: string; time: string; reason: string; id: number }[];
		hasMore: boolean;
		total: number;
		page: number;
	}>({
		loading: false,
		deepData: {
			type: 0,
			page: 1,
			sn: '',
			timestamp: 0,
		},
		hasMore: false,
		preveDataSorce: [],
		dataSource: [],
		total: 0,
		page: 1,
	});
	const request = (params: {
		page: number;
		filter: WarningSearchState;
		sn?: string;
		page_size?: number;
	}) => {
		setState((preState) => {
			const { sn = '', filter } = params;
			return {
				...preState,
				loading: true,
				preveDataSorce:
					preState.deepData.type !== filter || preState.deepData.sn !== sn
						? []
						: preState.preveDataSorce,
				deepData: {
					...preState.deepData,
					page: params.page,
					type: params.filter,
					sn: params.sn || '',
				},
			};
		});
		Request.sendRequest({
			url: '/v1/alarm/list',
			params: {
				...params,
				timestamp: state.deepData.timestamp,
			},
		})
			.then((data: any) => {
				setState((preState) => {
					return {
						...preState,
						loading: false,
						total: data.total,
						page: data.page,
						hasMore: data.has_more,
						deepData: { ...preState.deepData, timestamp: data.timestamp },
						preveDataSorce: preState.preveDataSorce.concat(data.list),
						dataSource: preState.preveDataSorce.concat(data.list),
					};
				});
			})
			.catch((error) => {
				setState((preState) => ({ ...preState, loading: false }));
			});
	};
	return [state, request] as const;
};
export const DeviceStatus = {
	0: '待确认',
	1: '已确认',
	2: '非本项目',
};
export const Devicestate = {
	0: '离线',
	1: '在线',
};
interface deviceProps {
	id: number;
	name: string;
	project_name: string;
	sn: string;
	provider_name: string;
}

/**
 * @description 设备审核
 */
export const usPutDevice = () => {
	const [state, setState] = React.useState({
		loading: false,
	});
	const request = (data: {
		id: number;
		status: boolean;
		device_code: number;
	}) => {
		return new Promise((resolve, reject) => {
			setState((preState) => {
				return {
					...preState,
					loading: true,
				};
			});
			Request.sendRequest({
				url: '/v1/project/device_approve',
				method: 'PUT',
				data,
			})
				.then((data: any) => {
					resolve(data);
					setState((preState) => {
						return {
							...preState,
							loading: false,
						};
					});
				})
				.catch((error) => {
					reject(error);
					setState((preState) => ({ ...preState, loading: false }));
				});
		});
	};
	return { state, request } as const;
};
