///<reference path=".#ts/client.d.ts" />

var pages = {
	title: 'Mobile Landing',
	href: '/',
	layout: 'default',
	template: 'sportsHome',
	metaTitle: 'SPORTSBET.IO – your bitcoin sportsbook',
	metaDescription: 'Sportsbet.io',
	onBeforeAction: function() {
		Session.set('activeTab', 'sport');
		this.next();
	},
	subs: {
		landingRego: {
			title: 'Register',
			href: '/landing-register',
			layout: 'landinglayout',
			template: 'landingRegister',
			metaTitle: 'SPORTSBET.IO – Win tickets to final game of EURO 2016',
			metaDescription: 'Sportsbet.io',
		},
		sportHome: {
			handle: 'sportHome',
			title: 'Sports Home',
			layout: 'default',
			template: 'sportsHome',
			href: '/sports',
			onBeforeAction: function() {
				Session.set('footerVisible', false);
				Session.set('activeTab', 'sport');
				this.next();
			}
		},
		login: {
			handle: 'login',
			title: 'Sign In',
			href: '/login',
			template: 'login',
			layout: 'default',
			onBeforeAction: function () {
				Session.set('menuExpanded', false);
				Session.set('footerVisible', false);
				if (Meteor.userId()) {
					Pages.redirect('/');
				}
				else {
					this.next();
				}
			}
		},
		register: {
			handle: 'register',
			title: 'Sign Up',
			href: '/register',
			template: 'register',
			layout: 'default',
			onBeforeAction: function() {
				Session.set('menuExpanded', false);
				Session.set('footerVisible', false);

				Countries.IpClient.getConnectionCountry((err, res) => {
					if (res.code == 'US' || res.code == 'UM') {
						Session.set('clientCountry', res.code);
						Modals.open('gbError');
					}
				});

				if (Session.get('clientCountry') !== 'US' && Session.get('clientCountry') !== 'UM' && Session.get('clientCountry') !== 'UK') {
					this.next();
				}
			},
			onAfterAction: function() {

			}
		},
		forgotPassword: {
			handle: 'forgotPassword',
			title: 'Forgot Password',
			href: '/forgot-password',
			template: 'forgotPassword',
			layout: 'default',
			subs: {
				reset: {
					handle: 'reset',
					title: 'Reset',
					href: '/forgot-password/reset/:token',
					inSitemap: false,
					template: 'forgotPassword',
					onBeforeAction: function () {
						Session.set('footerVisible', false);
						if (Meteor.userId()) {
							Pages.redirect('/');
						}
						else {
							if (!this.params.token) {
								return;
							}
							Session.set('resetToken', this.params.token);
							this.next();
						}
					}
				}
			}
		},
		live: {
			title: 'Live',
			handle: 'sportsLive',
			href: '/sports/live',
			template: 'sportsLive',
			layout: 'default',
			onBeforeAction() {
				Session.set('firstLoad', false);
				Session.set('live', true);
				Session.set('activeTab', 'live');
				Session.set('footerVisible', false);
				this.next();
			}
		},
		upcoming: {
			title: 'Upcoming',
			handle: 'sportsUpcoming',
			href: '/sports/upcoming',
			template: 'sportsUpcoming',
			layout: 'default',
			onBeforeAction() {
				Session.set('firstLiveLoad', false);
				Session.set('live', true);
				Session.set('activeTab', 'live');
				Session.set('footerVisible', false);
				this.next();
			}
		},
		event: {
			title: 'Event',
			//href: '/sports/:live/event/:event',
			href: '/sports/:live/:param1?/:param2?/:param3?/:param4?/:param5?/:param6?/event/:event',
			template: 'sportsEventDetails',
			layout: 'default',
			onBeforeActionRecursive: function () {
				Session.set('opened-event', this.params.event);
				Session.set('live', this.params.live === 'live');
				Session.set('activeTab', this.params.live === 'live' ? 'live' : 'sport');
				Session.set('footerVisible', false);
				this.next();
			}
		},
		sportsCategoryDetails: {
			title: 'Category Details',
			href: '/sports/:live/:param1?/:param2?/:param3?/:param4?/:param5?/:param6?/events/:category',
			template: 'sportsCategoryDetails',
			layout: 'default',
			onBeforeActionRecursive: function () {
				Session.set('firstPageLoad', false);
				Session.set('pageDataLimit', 9);
				Session.set('isLive', this.params.live === 'live');
				Session.set('opened-category', this.params.category);
				Session.set('activeTab', this.params.live == 'live' ? 'live' : 'sport');
				Session.set('footerVisible', false);
				this.next();
			},
			onAfterAction: function() {
				let category = normaliseStr(this.params.param1);
				let isLive = this.params.live == 'live' ? 'Live In-Play' : '';

				/* There is no "Country" parameter in the url that I can rely on. The structure is set up in our backend and the 'Country'
				 is just a category that has been set up. It may be called "England" or it may be called "WTA" or anything else. It would be
				 in the same position in the URL as the 'Country' but it wouldn't be an actual country. */

				// This is in case the URL looks like: /soccer/england/premier-league
				if (this.params.param3) {
					let league = normaliseStr(this.params.param3);

					// Would display: "Premier League Bitcoin Betting Odds at Sportsbet.io"
					setMeta('title', `${league} Bitcoin ${isLive} Betting Odds at Sportsbet.io`);

					// Would display: "Premier League Bitcoin Betting from Sportsbet.io, All the Soccer Betting Odds Available on Desktop & Mobile.
					setMeta('description', `${league} ${isLive} Bitcoin Betting from Sportsbet.io, All the ${category} Betting Odds Available on Desktop & Mobile!`);
				}
				else {

					// This is in case the URL looks like /soccer/england
					if (this.params.param2) {
						let league = normaliseStr(this.params.param2)

						// Would display: "England Soccer Bitcoin Betting from Sportsbet.io, All the Soccer Betting Odds Available on Desktop & Mobile"
						setMeta('description', `${league} ${category} ${isLive} Bitcoin Betting from Sportsbet.io, All the ${category} Betting Odds Available on Desktop & Mobile!`);

						// Would display: "Bitcoin England Soccer Betting Odds at Sportsbet.io"
						setMeta('title', `Bitcoin ${league} ${category} ${isLive} Betting Odds at Sportsbet.io`);
					}

					// This is in case the URL looks like /soccer
					else {
						// Would display: "Bitcoin Soccer Betting Odds at at Sportsbet.io"
						setMeta('title', `Bitcoin ${category} ${isLive} Betting Odds at Sportsbet.io`);

						// Would display: "Soccer Bitcoin Betting from Sportsbet.io, All the Soccer Betting Odds Available on Desktop & Mobile."
						setMeta('description', `${category} ${isLive} Bitcoin Betting from Sportsbet.io, All the ${category} Betting Odds Available on Desktop & Mobile!`);
					}

				}
			}
		},
		sportsBrowseCategory: {
			title: 'Browse Category',
			href: '/sports/:live/:param1?/:param2?/:param3?/:param4?/:param5?/:param6?/browse/:category',
			template: 'sportsBrowseCategory',
			layout: 'default',
			onBeforeActionRecursive: function () {
				Session.set('opened-category', this.params.category);
				Session.set('footerVisible', false);
				this.next();
			},
			onAfterAction: function() {
				let category = normaliseStr(this.params.param1);
				let isLive = this.params.live == 'live' ? 'Live In-Play' : '';

				/* There is no "Country" parameter in the url that I can rely on. The structure is set up in our backend and the 'Country'
				 is just a category that has been set up. It may be called "England" or it may be called "WTA" or anything else. It would be
				 in the same position in the URL as the 'Country' but it wouldn't be an actual country. */

				// This is in case the URL looks like: /soccer/england/premier-league
				if (this.params.param3) {
					let league = normaliseStr(this.params.param3);

					// Would display: "Premier League Bitcoin Betting Odds at Sportsbet.io"
					setMeta('title', `${league} Bitcoin ${isLive} Betting Odds at Sportsbet.io`);

					// Would display: "Premier League Bitcoin Betting from Sportsbet.io, All the Soccer Betting Odds Available on Desktop & Mobile.
					setMeta('description', `${league} ${isLive} Bitcoin Betting from Sportsbet.io, All the ${category} Betting Odds Available on Desktop & Mobile!`);
				}
				else {

					// This is in case the URL looks like /soccer/england
					if (this.params.param2) {
						let league = normaliseStr(this.params.param2)

						// Would display: "England Soccer Bitcoin Betting from Sportsbet.io, All the Soccer Betting Odds Available on Desktop & Mobile"
						setMeta('description', `${league} ${category} ${isLive} Bitcoin Betting from Sportsbet.io, All the ${category} Betting Odds Available on Desktop & Mobile!`);

						// Would display: "Bitcoin England Soccer Betting Odds at Sportsbet.io"
						setMeta('title', `Bitcoin ${league} ${category} ${isLive} Betting Odds at Sportsbet.io`);
					}

					// This is in case the URL looks like /soccer
					else {
						// Would display: "Bitcoin Soccer Betting Odds at at Sportsbet.io"
						setMeta('title', `Bitcoin ${category} ${isLive} Betting Odds at Sportsbet.io`);

						// Would display: "Soccer Bitcoin Betting from Sportsbet.io, All the Soccer Betting Odds Available on Desktop & Mobile."
						setMeta('description', `${category} ${isLive} Bitcoin Betting from Sportsbet.io, All the ${category} Betting Odds Available on Desktop & Mobile!`);
					}

				}
			}
		},

		bettingHistory: {
			handle: 'bettinghistory',
			title: 'Betting History',
			href: '/sports/betting-history',
			template: 'sportsBettingHistory',
			layout: 'default',
			onBeforeAction: function() {
				Session.set('menuExpanded', false);
				Session.set('footerVisible', false);
				requireLogin();
				this.next();
			}
		},
		activeBets: {
			handle: 'activeBets',
			title: 'Active Bets',
			href: '/sports/active-bets',
			template: 'sportsActiveBets',
			layout: 'default',
			onBeforeAction: function() {
				Session.set('menuExpanded', false);
				Session.set('footerVisible', false);
				requireLogin();
				this.next();
			}
		},
		casino: {
			handle: 'casino',
			title: 'Casino',
			href: '/casino',
			template: 'casino',
			layout: 'default',
			onBeforeAction: function() {
				Session.set('menuName', 'popular-games');
				Session.set('activeTab', 'casino');
				Session.set('footerVisible', false);
				this.next();
			},
			subs: {
				casinoMenu: {
					handle: 'casinoMenu',
					title: i18n('casinoGames'),
					href: '/casino/:menuName',
					icon: 'casino',
					template: 'casino',
					inSitemap: false,
					onBeforeAction: function () {
						Session.set('menuName', this.params.menuName);
						Session.set('footerVisible', false);
						this.next();
					}
				},
				casinoGame: {
					handle: 'casinoGame',
					title: 'Casino Game',
					href: '/casino/:menuName/:handle/:fun?',
					template: 'casinoGame',
					inSitemap: false,
					notFoundTemplate: 'gameNotFound',
					onBeforeAction: function () {
						Session.set('splashTemplate', null);
						Session.set('gameName', this.params.handle ? this.params.handle.toLowerCase() : this.params.menuName.toLowerCase());
						Session.set('menuName', this.params.menuName);

						if (this.params.handle == 'player-props-dst') {
							Session.set('activeTab', 'playerProps');
						}

						Session.set('footerVisible', false);
						if (this.params.fun === 'fun') {
							Session.set('forFun', this.params.fun === 'fun');
						}
						else {
							Session.set('forFun', false);
						}
						this.next();
					}
				}

			}
		},
		deposit: {
			handle: 'deposit',
			title: 'Deposit',
			href: '/deposit',
			template: 'deposit',
			layout: 'default',
			onBeforeAction: function() {
				Session.set('menuExpanded', false);
				Session.set('footerVisible', false);
				requireLogin();
				this.next();
			}
		},
		withdraw: {
			handle: 'withdraw',
			title: 'Withdraw',
			href: '/withdraw',
			template: 'withdraw',
			layout: 'default',
			onBeforeAction: function() {
				Session.set('menuExpanded', false);
				Session.set('footerVisible', false);
				requireLogin();
				this.next();
			}
		},
		history: {
			handle: 'history',
			title: 'history',
			href: '/History',
			template: 'transactions',
			layout: 'default',
			onBeforeAction: function() {
				Session.set('menuExpanded', false);
				Session.set('footerVisible', false);
				requireLogin();
				this.next();
			}
		},
		chat: {
			handle: 'chat',
			title: 'Sportsbet support Chat',
			href: '/support',
			template: 'chat',
			onBeforeAction: function() {
				Session.set('menuExpanded', false);
				Session.set('footerVisible', false);
				this.next();
			}
		},
		about: setContentPages('about'),
		// about: {
		// 	handle: 'about',
		// 	title: 'About',
		// 	href: '/about',
		// 	template: 'contentPage',
		// 	onBeforeAction: function() {
		// 		Session.set('menuExpanded', false);
		// 		Session.set('contentMenuSectionHandle', 'about');
		// 		Session.set('pageName', 'general-sportsbook-rules');
		//
		// 		requireLogin();
		// 		this.next();
		// 	}
		// },
		promotions: setContentPages('promotions'),
		// leaderboard: {
		// 	handle: 'leaderboard',
		// 	title: 'leaderboard',
		// 	href: '/leaderboard',
		// 	template: 'Leaderboard',
		// 	onBeforeAction: function() {
		// 		Session.set('menuExpanded', false);
		// 		Session.set('footerVisible', false);
		// 		this.next();
		// 	}
		// },
		// lotteries: {
		// 	handle: 'lotteries',
		// 	title: 'lotteries',
		// 	href: '/lotteries',
		// 	template: 'Lotteries',
		// 	onBeforeAction: function() {
		// 		// this.next();
		// 		Pages.go('/leaderboard');
		// 	}
		// },
		loto: {
			handle: 'lotteries',
			title: 'lotteries',
			href: '/lotteries',
			template: 'Lotteries',
			onBeforeAction: function() {
				Session.set('showHistory', false);
				Session.set('menuExpanded', false);
				Session.set('footerVisible', false);
				this.next();
				//Pages.go('/leaderboard');
			},
			subs: {
				lotteryTickets: {
					handle: 'lotteryTickets',
					title: 'Lottery - tickets',
					href: '/lotteries/tickets',
					template: 'lotteryUserTickets',
					onBeforeAction: function () {
						Session.set('footerVisible', false);
						this.next();
					}
				},
				lotteryHistory: {
					handle: 'lotteryHistory',
					title: 'Lottery - history',
					href: '/lotteries/history',
					template: 'lotteries',
					onBeforeAction: function () {
						Session.set('showHistory', true);
						Session.set('footerVisible', false);
						this.next();
					}
				},
				lottery: {
					handle: 'lottery',
					title: 'Lottery',
					href: '/lotteries/:lottery',
					template: 'lotteryInfo',
					inSitemap: false,
					onBeforeAction: function () {
						Session.set('lotteryHandle', this.params.lottery);
						Session.set('lotteryInstance', false);
						Session.set('footerVisible', false);
						this.next();
					},
					subs: {
						instanceTickets: {
							handle: 'lottery',
							title: 'Lottery - tickets',
							href: '/lotteries/:lottery/tickets/',
							template: 'lotteryCurrentUserTickets',
							inSitemap: false,
							onBeforeAction: function () {
								Session.set('lotteryHandle', this.params.lottery);
								Session.set('lotteryInstance', false);
								Session.set('footerVisible', false);
								this.next();
							},
						},
						instanceHistory: {
							handle: 'lottery',
							title: 'Lottery - history',
							href: '/lotteries/:lottery/history/',
							template: 'lotteryHistory',
							inSitemap: false,
							onBeforeAction: function () {
								Session.set('lotteryHandle', this.params.lottery);
								Session.set('lotteryInstance', false);
								Session.set('footerVisible', false);
								this.next();
							},
							subs: {
								lotteryInstance: {
									handle: 'lotteryInstance',
									title: 'Lottery',
									href: '/lotteries/:lottery/history/:instance',
									template: 'lotteryInfo',
									inSitemap: false,
									onBeforeAction: function () {
										Session.set('lotteryHandle', this.params.lottery);
										Session.set('lotteryInstance', this.params.instance);
										Session.set('footerVisible', false);
										this.next();
									},
								}
							}
						},
					}
				},
				fill: {
					handle: 'fill',
					title: 'Fill Ticket',
					href: '/lotteries/:lottery/fill',
					template: 'lotteryMain',
					inSitemap: false,
					onBeforeAction() {
						Session.set('fillLottery', this.params.lottery);
						Session.set('footerVisible', false);
						this.next();
					},
				}
			}
		},
		leaderboard: {
			handle: 'leaderboard',
			title: 'Leaderboard',
			href: '/leaderboard/:handle',
			template: 'leaderboard',
			layout: 'default',
			// waitOn: function () {
			// 	return [
			// 		this.subscribe('betting/Leaderboards', {handle: this.params.handle}),
			// 	];
			// },
			onBeforeAction: function() {
				Session.set('opened-leaderboard', this.params.handle);
				// var lb = Betting.Leaderboard.db.findOne({handle: this.params.handle});
				// Session.set('opened-leaderboard', lb._id);
				// if (lb._id) {
				// 	this.subscribe('betting/LeaderboardUsers', {leaderboard: lb._id}, {limit: +lb.participantLimit, sort: {points: -1}}, ['user']);
				// 	this.subscribe('betting/LeaderboardPrizes', {leaderboard: lb._id});
				// }

				this.next();
			}
		},
		props: {
			title: 'Player Props',
			href: '/player-props',
			template: 'playerProps',
			layout: 'default',
			onBeforeAction: function() {
				Session.set('activeTab', 'playerProps');
				this.next();
			}
		},
		wof: {
			handle: 'wof',
			title: 'wof',
			href: '/wof',
			template: 'wof',
			layout: 'default'
		},
	}
};

this.requireLogin = requireLogin;
function setContentPages(handle) {
	var camelHandle = camelize(handle);
	if (camelHandle == 'bitcoinInfo') {
		camelHandle = 'bitcoinInformation';
	}


	var pgSettings = {
		handle: camelHandle,
		title: function () {
			return i18n(camelHandle);
		},
		href: '/' + handle,
		icon: 'logo',
		template: 'contentPage',

		onBeforeAction: function () {
			if(camelHandle == 'promotions') {
				Pages.redirect('/promotions/welcome-offer-for-mobile-punters');
			}
			Session.set('background', '//cdn.coingaming.io/bitcasino/images/backgrounds/casino-bg.jpg');
			Session.set('contentMenuSectionHandle', handle);
			Session.set('pageName', undefined);
			Session.set('headerMenu', 'content');
			Session.set('menuExpanded', false);
			Session.set('footerVisible', false);
			this.next();
		},
		subs: {}
	};

	pgSettings.subs[camelHandle + 'Page'] = {
		handle: camelHandle + 'Page',
		href: '/' + handle + '/:pageName',
		template: 'contentPage',
		notFoundTemplate: '404',
		onBeforeAction: function () {
			Session.set('menuExpanded', false);
			Session.set('pageName', this.params.pageName);
			Session.set('contentMenuSectionHandle', handle);
			Session.set('headerMenu', 'content');
			Session.set('footerVisible', false);
			this.next();
		},
		onAfterAction: function () {
			Tracker.nonreactive(() => {
				var page = Content.fetchLocalizedPage({handle: this.params.pageName});
				if (page) {
					page.setMeta()
				};
			});
		},
	};
	return pgSettings;
}

function camelize(str) {
	return str.replace('-', ' ').replace(/(?:^\w|[A-Z]|\b\w)/g, function (letter, index) {
		return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
	}).replace(/\s+/g, '');
}

function requireLogin() {
	if (!Meteor.userId()) {
		Pages.redirect('login');
	}
}
this.pages = <any>pages;
