import * as THREE from "three";
import {
	OrbitControls
} from "three/examples/jsm/controls/OrbitControls";
import {
	RGBELoader
} from "three/examples/jsm/loaders/RGBELoader";
import {
	Reflector
} from "three/examples/jsm/objects/Reflector";
import {
	GLTFLoader
} from "three/examples/jsm/loaders/GLTFLoader";
import {
	DRACOLoader
} from "three/examples/jsm/loaders/DRACOLoader";
import {
	Line2
} from "three/examples/jsm/lines/Line2";


export let getStdMapMixin = {
	methods: {
		getStdMapKeys(std_map, delete_map = false) {
			let map_keys = [];
			const map_keys_vec = std_map.keys();
			for (let idx = 0; idx < map_keys_vec.size(); idx++)
				map_keys.push(map_keys_vec.get(idx));
			map_keys_vec.delete();
			if (delete_map) std_map.delete();
			return map_keys;
		},
		getStdMapEntries(std_map) {
			let map_entries = [];
			//key路段索引 ， std_map.get(key)车道编号
			for (let key of this.getStdMapKeys(std_map)) {
				map_entries.push([key, std_map.get(key)]);
			}
			return map_entries;
		},
		getStdVecEntries(std_vec, delete_vec = false, ArrayType = null) {
			//获取道路网格三位坐标点数据
			let entries = ArrayType ?
				new ArrayType(std_vec.size()) :
				new Array(std_vec.size());
			for (let idx = 0; idx < std_vec.size(); idx++) {
				entries[idx] = std_vec.get(idx);
			}

			if (delete_vec) std_vec.delete();
			return entries;
		},
		isValid(rgba) {
			return !(rgba[0] == 1 && rgba[1] == 1 && rgba[2] == 1 && rgba[3] == 1);
		},
		encodeUInt32(ui32) {
			let rgba = new Float32Array(4);
			rgba[0] = (Math.trunc(ui32) % 256) / 255;
			rgba[1] = (Math.trunc(ui32 / 256) % 256) / 255;
			rgba[2] = (Math.trunc(ui32 / 256 / 256) % 256) / 255;
			rgba[3] = (Math.trunc(ui32 / 256 / 256 / 256) % 256) / 255;
			return rgba;
		},
		decodeUInt32(rgba) {
			return (
				Math.round(rgba[0] * 255) +
				Math.round(rgba[1] * 255) * 256 +
				Math.round(rgba[2] * 255) * 256 * 256 +
				Math.round(rgba[3] * 255) * 256 * 256 * 256
			);
		},
		applyVertexColors(buffer_attribute, color, offset, count) {
			//buffer_attribute.itemSize颜色宽度
			const colors = new Float32Array(count * buffer_attribute.itemSize);
			for (
				let i = 0; i < count * buffer_attribute.itemSize; i += buffer_attribute.itemSize
			) {
				colors[i] = color.r;
				colors[i + 1] = color.g;
				colors[i + 2] = color.b;
				// colors[i] =0;
				// colors[i + 1] = 0;
				// colors[i + 2] = 255;
			}
			//根据颜色偏移设置改变颜色值
			buffer_attribute.array.set(colors, offset * buffer_attribute.itemSize);
		},
		getTextCanvas(content) {
			var width = 500;
			var height = 500;
			var canvas = document.createElement('canvas');
			canvas.setAttribute("width", width);
			canvas.setAttribute("height", height);
			var ctx = canvas.getContext('2d');
			ctx.width = width;
			ctx.height = height;
			ctx.fillStyle = '#FFFFFF';
			ctx.fillRect(0, 0, width, height);
			ctx.font = '320px Microsoft YaHei';
			ctx.fillStyle = '#000000';
			ctx.textAlign = 'center';

			ctx.fillText(content, ctx.width / 2, ctx.height / 2 + 50, ctx.width - 20);

			return canvas;
		},
		get_geometry(odr_meshunion) {
			const geom = new THREE.BufferGeometry();
			geom.setAttribute(
				"position",
				new THREE.Float32BufferAttribute(
					this.getStdVecEntries(odr_meshunion.vertices, true).flat(),
					3
				)
			);
			//获取uv坐标
			geom.setAttribute(
				"st",
				new THREE.Float32BufferAttribute(
					this.getStdVecEntries(odr_meshunion.st_coordinates, true).flat(),
					2
				)
			);
			//设置点颜色
			geom.setAttribute(
				"color",
				new THREE.Float32BufferAttribute(
					new Float32Array(geom.attributes.position.count * 3),
					3
				)
			);
			//设置几何体唯一编号
			geom.setAttribute(
				"id",
				new THREE.Float32BufferAttribute(
					new Float32Array(geom.attributes.position.count * 4),
					4
				)
			);
			// geom.setAttribute('ids', new THREE.Float32BufferAttribute(new Float32Array(geom.attributes.position.count * 4), 4));
			//设置几何体索引数据
			geom.setIndex(this.getStdVecEntries(odr_meshunion.indices, true));
			geom.computeVertexNormals();
			return geom;
		}
	},
}