/*
 * Verrückte Turing-Maschinen — A turing complete game
 * Copyright (C) 2016-2018 kcf <vidofnir@folksprak.org>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

namespace mod_vtm
	{
		
		export namespace mod_manifestation
			{
				
				export namespace mod_store
					{
						
						export namespace mod_game
							{
								
								/**
								 * @author kcf <vidofnir@folksprak.org>
								 */
								export type type_game =
									{
										model : mod_vtm.mod_model.mod_game.type_game;
									}
								;
								
								
								/**
								 * @author kcf <vidofnir@folksprak.org>
								 */
								export function create
									(
										model : mod_vtm.mod_model.mod_game.type_game
									)
									: type_game
									{
										return {
											"model": model
										};
									}
								
								
								/**
								 * @author kcf <vidofnir@folksprak.org>
								 */
								export function create_extended
									(
										model : mod_vtm.mod_model.mod_game.type_game
									)
									: type_manifestation<mod_vtm.mod_model.mod_game.type_game>
									{
										return {
											"kind": "store_game",
											"data": create(model)
										};
									}
								
								
								/**
								 * @author kcf <vidofnir@folksprak.org>
								 */
								function view(game : type_game) : void
									{
									}
								
								
								/**
								 * @author kcf <vidofnir@folksprak.org>
								 */
								function bind(game : type_game) : void
									{
										mod_vtm.mod_model.mod_game.listen
											(
												game.model,
												"change_task",
												(data) =>
													{
														// console.info("change_task", data);
														let task : mod_vtm.mod_model.mod_task.type_task = mod_vtm.mod_model.mod_game.task_read(game.model);
														let id : string = mod_vtm.mod_model.mod_task.id(task);
														let key : string = ("vtm_" + id);
														// 
														if (key in localStorage)
															{
																let item : string = localStorage.getItem(key);
																let world_ : any = JSON.parse(item);
																let world : mod_vtm.mod_model.mod_world.type_world = mod_vtm.mod_model.mod_world.import_(world_);
																mod_vtm.mod_model.mod_game.world_set(game.model, world, false);
															}
														else
															{
																mod_vtm.mod_model.mod_game.world_clear(game.model);
																// nothing tun
															}
													}
											)
										;
										mod_vtm.mod_model.mod_game.listen
											(
												game.model,
												"change_world",
												(data) =>
													{
														let task : mod_vtm.mod_model.mod_task.type_task = mod_vtm.mod_model.mod_game.task_read(game.model);
														let id : string = mod_vtm.mod_model.mod_task.id(task);
														let key : string = ("vtm_" + id);
														// 
														let world : mod_vtm.mod_model.mod_world.type_world = mod_vtm.mod_model.mod_game.world_read(game.model);
														let world_ : any = mod_vtm.mod_model.mod_world.export_(world);
														let item : string = JSON.stringify(world_);
														localStorage.setItem(key, item);
													}
											)
										;
										mod_vtm.mod_model.mod_game.listen
											(
												game.model,
												"change_token",
												(data) =>
													{
														// console.info("change_token", data);
													}
											)
										;
										mod_vtm.mod_model.mod_game.listen
											(
												game.model,
												"change_mode",
												(data) =>
													{
														// console.info("change_mode", data);
													}
											)
										;
									}
								
								
								/**
								 * @author kcf <vidofnir@folksprak.org>
								 */
								lib_trait.attend<signature_manifestation<mod_vtm.mod_model.mod_game.type_game, void>>
									(
										trait_manifestation,
										"store_game",
										{
											"view": (manifestation) => view(manifestation.data),
											"bind": (manifestation) => bind(manifestation.data),
										}
									)
								;
								
							}
						
					}
				
			}
		
	}

