/*
RSK World – Tower Defense 3D (Mage Tower v2)
For support, licensing, or custom development inquiries: help@rskworld.in • Phone/WhatsApp: +91 9330539277
Website: RSK World • Business Inquiries: help@rskworld.in
🤝 Contributing: Suggestions and improvements are welcome!
*/

import * as THREE from 'three';
import { Projectile } from './Projectile.js';

export class MageTower {
	constructor(position) {
		this.position = position.clone();
		this.level = 1;
		this.damageMultiplier = 1.0;
		this.range = 8.0;
		this.fireCooldown = 1.2;
		this._cd = 0;
		this.group = this._createMesh();
		this.group.position.copy(this.position);
		this._targets = [];
	}

	_createMesh() {
		const g = new THREE.Group();
		
		const plinth = new THREE.Mesh(
			new THREE.CylinderGeometry(1.2, 1.4, 0.4, 8), 
			new THREE.MeshStandardMaterial({ color: 0x1e1b4b, roughness: 0.8 })
		);
		plinth.position.y = 0.2; plinth.castShadow = true; plinth.receiveShadow = true; g.add(plinth);
		
		const spire = new THREE.Mesh(
			new THREE.ConeGeometry(0.8, 2.5, 4),
			new THREE.MeshStandardMaterial({ color: 0x312e81, metalness: 0.5, roughness: 0.2 })
		);
		spire.position.y = 1.65; spire.castShadow = true; g.add(spire);
		
		const crystalGeo = new THREE.OctahedronGeometry(0.5);
		const crystalMat = new THREE.MeshStandardMaterial({ 
			color: 0x60a5fa, 
			emissive: 0x2563eb, 
			emissiveIntensity: 2,
			metalness: 0.8, 
			roughness: 0.1,
			transparent: true,
			opacity: 0.9
		});
		this._crystal = new THREE.Mesh(crystalGeo, crystalMat);
		this._crystal.position.y = 3.5;
		g.add(this._crystal);

		const ringGeo = new THREE.TorusGeometry(0.7, 0.05, 8, 32);
		const ringMat = new THREE.MeshBasicMaterial({ color: 0x3b82f6 });
		this._ring = new THREE.Mesh(ringGeo, ringMat);
		this._ring.rotation.x = Math.PI / 2;
		this._ring.position.y = 3.5;
		g.add(this._ring);

		// Level Indicator
		this._levelIndicators = new THREE.Group();
		this._levelIndicators.position.y = 4.5;
		g.add(this._levelIndicators);
		this._updateLevelVisuals();

		return g;
	}

	_updateLevelVisuals() {
		if (!this._levelIndicators) return;
		while(this._levelIndicators.children.length) this._levelIndicators.remove(this._levelIndicators.children[0]);
		for (let i = 0; i < this.level; i++) {
			const dot = new THREE.Mesh(
				new THREE.SphereGeometry(0.1, 8, 8),
				new THREE.MeshBasicMaterial({ color: 0x60a5fa })
			);
			dot.position.set(Math.cos(i * 1.5) * 0.4, 0, Math.sin(i * 1.5) * 0.4);
			this._levelIndicators.add(dot);
		}
	}

	addTo(scene) { scene.add(this.group); }

	_updateTargets(enemies) {
		this._targets = [];
		const myPos = this.group.position;
		for (const e of enemies) {
			if (!e.alive || e.reachedEnd) continue;
			const dist = myPos.distanceTo(e.group.position);
			if (dist <= this.range) {
				this._targets.push(e);
			}
		}
	}

	_tryFire(spawnCb) {
		if (this._targets.length === 0 || this._cd > 0) return;
		
		this._crystal.scale.set(1.5, 1.5, 1.5);
		setTimeout(() => this._crystal && this._crystal.scale.set(1, 1, 1), 100);

		const dmg = Math.round(15 * this.damageMultiplier);
		for (const target of this._targets) {
			target.takeDamage(dmg);
			// Apply Slow Effect!
			if (target.speed > 1.0) {
				const originalSpeed = target.speed;
				target.speed *= 0.6; // 40% slow
				setTimeout(() => { if (target.alive) target.speed = originalSpeed; }, 2000);
			}
			
			const origin = new THREE.Vector3();
			this._crystal.getWorldPosition(origin);
			const proj = new Projectile(origin, target, { damage: 0, speed: 18 });
			proj.group.children[0].material.color.set(0x3b82f6);
			spawnCb(proj);
		}
		
		this._cd = this.fireCooldown;
	}

	update(delta, enemies, spawnCb) {
		this._cd = Math.max(0, this._cd - delta);
		
		if (this._crystal) {
			this._crystal.rotation.y += delta * 2;
			this._crystal.position.y = 3.5 + Math.sin(Date.now() * 0.003) * 0.2;
			this._ring.rotation.z -= delta * 1.5;
			this._ring.position.y = this._crystal.position.y;
		}

		if (this._levelIndicators) {
			this._levelIndicators.rotation.y += delta * 3;
			if (this._levelIndicators.children.length !== this.level) this._updateLevelVisuals();
		}

		this._updateTargets(enemies);
		this._tryFire(spawnCb);
	}
}
