import { EditorState, convertFromRaw, convertToRaw, CompositeDecorator } from 'draft-js';
import { message } from 'antd';
import { stateToHTML } from 'draft-js-export-html';
import moment from 'moment';
import { isJSON } from '@/webPublic/one_stop_public/copy';

function findLinkEntities(contentBlock, callback, contentState) {
	contentBlock.findEntityRanges(
		(character) => {
			const entityKey = character.getEntity();
			return entityKey !== null && contentState.getEntity(entityKey).getType() === 'LINK';
		},
		function() {
			console.log(arguments);
			callback(...arguments);
		},
	);
}

const Link = (props) => {
	const { url } = props.contentState.getEntity(props.entityKey).getData();

	return (
		<a href={url} target="_blank">
			{props.children}
		</a>
	);
};

const decorator = new CompositeDecorator([
	{
		strategy: findLinkEntities,
		component: Link,
	},
]);

export function changeHtml(msg) {
	var msg = msg.replace(/<\/?[^>]*>/g, ''); //去除HTML Tag
	msg = msg.replace(/[|]*\n/, ''); //去除行尾空格
	msg = msg.replace(/&npsp;/gi, ''); //去掉npsp
	return msg;
}

export function changeToDraftState(blocks) {
	if (blocks == null || blocks == '') {
		return EditorState.createEmpty(decorator);
	}
	let x;
	if (typeof blocks === 'string' && blocks) {
		try {
			x = JSON.parse(blocks);
		} catch (e) {
			return EditorState.createEmpty(decorator);
		}
	} else {
		x = blocks;
	}

	if (!x.blocks) {
		return EditorState.createEmpty(decorator);
	}

	const b = convertFromRaw(x);
	return EditorState.createWithContent(b, decorator);
}

export function changeFromDraftState(editorState) {
	const x = editorState.getCurrentContent();
	const blocks = JSON.stringify(convertToRaw(x));
	const content = stateToHTML(x);

	return { content, blocks };
}

export function changeToDraftState2(blocks) {
	const b = convertFromRaw(blocks);
	return EditorState.createWithContent(b, decorator);
}

export function changeFromDraftState2(editorState) {
	if (editorState == null) return;
	const x = editorState.getCurrentContent();
	const blocks = convertToRaw(x);

	return blocks;
}

export function preHandle(values) {
	for (var key in values) {
		if (!values[key]) continue;
		if (key.indexOf('$') > -1) {
			var xx = key.split('$');
			for (var i = 0; i < xx.length; i++) {
				if (values[key][i] instanceof moment) {
					values[xx[i]] = values[key][i].valueOf();
				} else {
					values[xx[i]] = values[key][i];
				}
			}
			delete values[key];
		} else {
			if (values[key] != null) {
				if (values[key] instanceof moment) {
					values[key] = values[key].valueOf();
				} else if (values[key] instanceof Date) {
					values[key] = values[key].valueOf();
				} else if (values[key] instanceof Boolean) {
					values[key] = values[key] ? 0 : 1;
				} else if (values[key] instanceof Object && key != 'defaultValues') {
					//处理子表单中的日期值
					let objValues = values[key];
					for (let k in objValues) {
						if (k != '' && objValues[k] instanceof Object) {
							let childObj = objValues[k];
							for (let j in childObj) {
								if (childObj[j] != null) {
									if (childObj[j] instanceof moment) {
										values[key][k][j] = values[key][k][j].valueOf();
									} else if (childObj[j] instanceof Date) {
										values[key][k][j] = values[key][k][j].valueOf();
									} else if (childObj[j] instanceof Boolean) {
										values[key][k][j] = values[key][k][j] ? 0 : 1;
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
