import * as React from 'react';
import Request from '../utils/request';
type ChanleState = 0 | 1;
export enum chanleStatus {
	offline = 0,
	online,
}
export const chanleState = {
	0: '离线',
	1: '在线',
};

export interface ChanleProps {
	channel_no: string;
	name: string;
	id: number;
	state: ChanleState;
}

/**
 * @description 获取可视化所有的通道列表
 */
export const useChanleList = () => {
	const [state, setState] = React.useState<{
		loading: boolean;
		hasMore: boolean;
		dataSource: ({ checked: Boolean } & ChanleProps)[];
		total: number;
		page: number;
		preParams: { page: number; sn?: string; channel_no?: string };
		preDataSource: ChanleProps[];
	}>({
		loading: false,
		dataSource: [],
		total: 0,
		page: 1,
		hasMore: false,
		preParams: { page: 1 },
		preDataSource: [],
	});
	const request = (params?: {
		page: number;
		sn?: string;
		channel_no?: string; // 通道名称(后端通过这个字段去模糊匹配通道号和名称)
	}) => {
		//当 sn和channel_no没有发生变化 代表需要在原来的数据上新增数据，否则直接进行替换
		const isResetData =
			params?.sn !== state.preParams.sn ||
			params?.channel_no !== state.preParams.channel_no;
		setState((preState) => ({
			...preState,
			loading: true,
			preParams: { ...preState.preParams, ...params },
			dataSource: isResetData ? [] : preState.dataSource,
		}));
		Request.sendRequest({
			url: '/v1/device/vedio_channel_list',
			params,
		})
			.then((data: any) => {
				setState((preState) => ({
					...preState,
					...data,
					loading: false,
					hasMore: data.has_more,
					dataSource: isResetData
						? data.list
						: preState.dataSource.concat(data.list),
				}));
			})
			.catch((error) => {
				setState((preState) => ({ ...preState, loading: false }));
			});
	};
	return [state, request, setState] as const;
};
export const useChangeChnale = () => {
	const [state, setState] = React.useState({
		loading: false,
	});
	const request = (data: { id: number; name: string }) => {
		setState((preState) => ({ ...preState, loading: true }));
		return Request.sendRequest({
			url: '/v1/device/vedio_channel',
			method: 'put',
			data: data,
		}).finally(() => {
			setState((preState) => ({ ...preState, loading: false }));
		});
	};
	return { state, request } as const;
};

/**
 * @description 获取管理后台所有的通道列表
 */
export const useAdminChanleList = () => {
	const [state, setState] = React.useState<{
		loading: boolean;
		dataSource: ChanleProps[];
		total: number;
		page: number;
	}>({
		loading: false,
		dataSource: [],
		total: 0,
		page: 1,
	});
	const request = (params: {
		page: number;
		sn?: string;
		channel_no?: string; // 通道名称(后端通过这个字段去模糊匹配通道号和名称)
	}) => {
		setState((preState) => ({
			...preState,
			loading: true,
		}));
		Request.sendRequest({
			url: '/v1/device/vedio_channel_list',
			params,
		})
			.then((data: any) => {
				setState((preState) => ({
					...preState,
					...data,
					loading: false,
					dataSource: data.list,
				}));
			})
			.catch((error) => {
				setState((preState) => ({ ...preState, loading: false }));
			});
	};
	return [state, request] as const;
};
/**
 * @description 获取未绑定得通道列表
 */
export const useUnBindChanles = () => {
	const [state, setState] = React.useState<{
		loading: boolean;
		dataSource: ChanleProps[];
	}>({
		loading: false,
		dataSource: [],
	});
	const request = () => {
		setState((preState) => ({
			...preState,
			loading: true,
		}));
		Request.sendRequest({
			url: '/v1/device/vedio_channel_unbind',
		})
			.then((data: any) => {
				setState((preState) => ({
					...preState,
					...data,
					loading: false,
					dataSource: data.list,
				}));
			})
			.catch((error) => {
				setState((preState) => ({ ...preState, loading: false }));
			});
	};
	return { state, request } as const;
};
export interface IliveUrlProps {
	flvurl: string;
	hlsurl: string;
	rtcurl: string;
	rtmpurl: string;
	vediourl: string;
	sessionid: string;
}
/**
 * @description 请求通道的播放地址
 */
export const useChanleLiveUrl = () => {
	const [state, setState] = React.useState<{
		loading: boolean;
		dataSource: IliveUrlProps;
	}>({
		loading: false,
		dataSource: {
			flvurl: '',
			hlsurl: '',
			rtcurl: '',
			rtmpurl: '',
			vediourl: '',
			sessionid: '',
		},
	});

	const request = (params: { channel_no: string }) => {
		return new Promise<IliveUrlProps>((resolve, reject) => {
			setState((preState) => ({ ...preState, loading: true }));
			Request.sendRequest({
				url: '/v1/camera/real_play',
				params,
			})
				.then((data: any) => {
					setState((preState) => ({ ...preState, dataSource: data }));
					resolve(data);
				})
				.catch((error) => {
					reject(error);
				});
		});
	};
	return [state, request] as const;
};
/**
 * @description 设备下的摄像头(通道)
 */
export const useDeviceChanles = () => {
	const [state, setState] = React.useState<{
		loading: boolean;
		dataSource: {
			channel_no: string;
			name: string;
			id: number;
			state: ChanleState;
		};
	}>({
		loading: false,
		dataSource: {
			channel_no: '',
			name: '',
			id: 0,
			state: 0,
		},
	});

	const request = (params: { sn: string }) => {
		setState((preState) => ({ ...preState, loading: true }));
		Request.sendRequest({
			url: '/v1/device/vedio_channel',
			params,
		})
			.then((data: any) => {
				setState((preState) => ({
					...preState,
					dataSource: data,
					loading: false,
				}));
			})
			.catch((error) => {
				setState((preState) => ({ ...preState, loading: false }));
			});
	};
	return [state, request] as const;
};
export const VideoStatus = {
	0: '待确认',
	1: '已确认',
	2: '非本项目',
};
export const VideoState = {
	0: '离线',
	1: '在线',
};
export interface VideorecordProps {
	id: number;
	ip: string;
	name: string;
	sn: string;
	state: keyof typeof VideoState;
	status: keyof typeof VideoStatus;
	type_name: string;
	type_code: number;
}
/**
 * @description 视频设备列表
 */
export const usVideoDeviceList = () => {
	const [state, setState] = React.useState<{
		loading: boolean;
		dataSource: VideorecordProps[];
		total: number;
		page: number;
	}>({
		loading: false,
		dataSource: [],
		total: 0,
		page: 1,
	});
	const request = (params: { page: number; filter: string }) => {
		setState((preState) => ({ ...preState, loading: true }));
		Request.sendRequest({
			url: '/v1/device/vedio_list',
			params,
		})
			.then((data: any) => {
				setState((preState) => ({
					...preState,
					...data,
					loading: false,
					dataSource: data.list,
				}));
			})
			.catch((error) => {
				setState((preState) => ({ ...preState, loading: false }));
			});
	};
	return { state, request } as const;
};

/**
 * @description 保持心跳
 */
export const useVideoKeppLive = () => {
	const request = (params: { session_id: string }) => {
		return Request.sendRequest({
			url: '/v1/camera/ping',
			params,
		});
	};
	return { request } as const;
};
type VideoDirection =
	| 'up'
	| 'down'
	| 'left'
	| 'right'
	| 'leftup'
	| 'leftdown'
	| 'rightup'
	| 'rightdown'
	| 'zoomin'
	| 'zoomout'
	| 'focusfar'
	| 'focusnear'
	| 'setpos'
	| 'callpos'
	| 'delpos'
	| 'stop';

export type PTzParmas = {
	channel_no: string;
	ptz_type?: VideoDirection;
	ptz_param?: number;
};

/**
 * @description 摄像头Ptz控制
 */
export const useVideoControllerPtz = () => {
	const request = (params: PTzParmas) => {
		return Request.sendRequest({
			url: '/v1/camera/ptz',
			params,
		});
	};
	return { request } as const;
};
