import {
  AdditiveBlending,
  Color,
  ColorRepresentation,
  DoubleSide,
  LineBasicMaterial,
  MeshStandardMaterial,
  RepeatWrapping,
  ShaderChunk,
  ShaderMaterial,
  TextureLoader,
} from "three";

//模型边线的材质
export const otherlineMaterial = new LineBasicMaterial({
  color: 0x3f8190,
  transparent: true,
  opacity: 0.2,
  blending: 2,
});

export function getBuildingsMaterial(
  color: ColorRepresentation | undefined,
  url: string,
  opacity?: any,
  repeat?: number
) {
  const texture = new TextureLoader().load(url);
  texture.needsUpdate = true; //使用贴图时进行更新
  texture.wrapS = texture.wrapT = RepeatWrapping;
  if (repeat) {
    texture.repeat.set(repeat, repeat);
  } else {
    texture.repeat.set(1000, 1000);
  }
  return new MeshStandardMaterial({
    map: texture,
    transparent: true,
    color: new Color(color) || "0xffffff",
    opacity: opacity || 0.7,
    blending: 2,
    // depthWrite: false,
    alphaTest: opacity,
    // side: 2,
    // depthTest: false,
  });
}

//底图材质
export function getBottomMaterial(
  color: ColorRepresentation | undefined,
  url: string,
  blending?: any,
  repeat?: number
) {
  const texture = new TextureLoader().load(url);
  texture.needsUpdate = true; //使用贴图时进行更新
  texture.wrapS = texture.wrapT = RepeatWrapping;
  if (repeat) {
    texture.repeat.set(repeat, repeat);
  } else {
    texture.repeat.set(1000, 1000);
  }
  return new MeshStandardMaterial({
    map: texture,
    transparent: true,
    color: new Color(color) || "0xffffff",
    blending: blending == 1 ? 1 : 2,
    // depthWrite: false,
    alphaTest: 0.8,
    opacity: 0.8,
    roughness: 0.2,
    metalness: 0.5,
  });
}

//模型材质
export function modelMaterial(content: any) {
  // console.log(content)
  const { img, repeat, color, ...params } = content;
  const texture = new TextureLoader().load(img);
  texture.needsUpdate = true; //使用贴图时进行更新
  texture.wrapS = texture.wrapT = RepeatWrapping;
  texture.repeat.set(repeat || 1, repeat || 1);
  return new MeshStandardMaterial({
    color: new Color(color) || "0xffffff",
    map: texture,
    ...params,
  });
}

//墙体材质
export function getWallMaterial(color: any, param?: any) {
  if (!param) param = {};
  param.depthTest = param.depthTest != undefined ? param.depthTest : true;
  param.depthWrite = param.depthWrite != undefined ? param.depthWrite : false;
  param.num = param.num != undefined ? param.num : 5;
  const vertexs = {
    normal_vertex: "\n  precision lowp float;\n  precision lowp int;\n  "
      .concat(
        ShaderChunk.fog_pars_vertex,
        "\n  varying vec2 vUv;\n  void main() {\n    vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );\n    vUv = uv;\n    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n    "
      )
      .concat(ShaderChunk.fog_vertex, "\n  }\n"),
  };
  const fragments = {
    rippleWall_fragment:
      "\n  precision lowp float;\n  precision lowp int;\n  uniform float time;\n  uniform float opacity;\n  uniform vec3 color;\n  uniform float num;\n  uniform float hiz;\n\n  varying vec2 vUv;\n\n  void main() {\n    vec4 fragColor = vec4(0.);\n    float sin = sin((vUv.y - time * hiz) * 10. * num);\n    float high = 0.92;\n    float medium = 0.4;\n    if (sin > high) {\n      fragColor = vec4(mix(vec3(.8, 1., 1.), color, (1. - sin) / (1. - high)), 1.);\n    } else if(sin > medium) {\n      fragColor = vec4(color, mix(1., 0., 1.-(sin - medium) / (high - medium)));\n    } else {\n      fragColor = vec4(color, 0.);\n    }\n\n    vec3 fade = mix(color, vec3(0., 0., 0.), vUv.y);\n    fragColor = mix(fragColor, vec4(fade, 1.), 0.85);\n    gl_FragColor = vec4(fragColor.rgb, fragColor.a * opacity * (1. - vUv.y));\n  }\n",
  };
  const material = new ShaderMaterial({
    uniforms: {
      time: {
        //@ts-ignore
        type: "pv2",
        value: 0,
      },
      color: {
        //@ts-ignore
        type: "uvs",
        value: new Color(color),
        // value: new Color('#0099FF')
      },
      opacity: {
        //@ts-ignore
        type: "pv2",
        value: param.opacity || 1,
      },
      num: {
        //@ts-ignore
        type: "pv2",
        value: param.num,
      },
      hiz: {
        //@ts-ignore
        type: "pv2",
        value: param.speed || 0.15,
      },
    },
    vertexShader: vertexs.normal_vertex,
    fragmentShader: fragments.rippleWall_fragment,
    blending: AdditiveBlending,
    transparent: !0,
    depthWrite: param.depthWrite,
    depthTest: param.depthTest,
    side: DoubleSide,
  });
  return material;
}
