/*
 * Copyright 2014-2015 See AUTHORS file.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.kotcrab.vis.editor.ui.scene.entityproperties.components;

import com.artemis.Entity;
import com.badlogic.gdx.files.FileHandle;
import com.kotcrab.vis.editor.module.InjectModule;
import com.kotcrab.vis.editor.module.project.FileAccessModule;
import com.kotcrab.vis.editor.module.project.ShaderCacheModule;
import com.kotcrab.vis.runtime.assets.ShaderAsset;
import com.kotcrab.vis.runtime.component.ShaderComponent;
import com.kotcrab.vis.runtime.util.autotable.ATSelectFileHandler;

/** @author Kotcrab */
public class ShaderATSelectFileHandler implements ATSelectFileHandler {
	@InjectModule private FileAccessModule fileAccess;
	@InjectModule private ShaderCacheModule shaderCache;

	@Override
	public void applyChanges (Entity entity, FileHandle file) {
		ShaderComponent shader = entity.getComponent(ShaderComponent.class);

		String vert = null;
		String frag = null;

		if (file.extension().equals("vert")) {
			vert = fileAccess.relativizeToAssetsFolder(file);
			frag = fileAccess.relativizeToAssetsFolder(file.sibling(file.nameWithoutExtension() + ".frag"));
		}

		if (file.extension().equals("frag")) {
			frag = fileAccess.relativizeToAssetsFolder(file);
			vert = fileAccess.relativizeToAssetsFolder(file.sibling(file.nameWithoutExtension() + ".vert"));
		}

		shader.asset = new ShaderAsset(vert, frag);
		shader.shader = shaderCache.get(shader.asset);
	}

	@Override
	public String getLabelValue (Entity entity) {
		ShaderAsset asset = entity.getComponent(ShaderComponent.class).asset;
		if (asset == null) return "<no shader>";
		return asset.getPathWithoutExtension().substring("shader/".length());
	}
}
