1--	Part of FusionPBX
2--	Copyright (C) 2013 Mark J Crane <markjcrane@fusionpbx.com>
3--	All rights reserved.
4--
5--	Redistribution and use in source and binary forms, with or without
6--	modification, are permitted provided that the following conditions are met:
7--
8--	1. Redistributions of source code must retain the above copyright notice,
9--	  this list of conditions and the following disclaimer.
10--
11--	2. Redistributions in binary form must reproduce the above copyright
12--	  notice, this list of conditions and the following disclaimer in the
13--	  documentation and/or other materials provided with the distribution.
14--
15--	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16--	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17--	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
18--	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
19--	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20--	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21--	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22--	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23--	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24--	POSSIBILITY OF SUCH DAMAGE.
25
26--define function main menu
27	function tutorial (menu)
28		if (voicemail_uuid) then
29			--intro menu
30				if (menu == "intro") then
31					--clear the value
32						dtmf_digits = '';
33					--flush dtmf digits from the input buffer
34						session:flushDigits();
35					--play the tutorial press 1, to skip 2
36						if (session:ready()) then
37								if (string.len(dtmf_digits) == 0) then
38									dtmf_digits = macro(session, "tutorial_intro", 1, 3000, '');
39								end
40						end
41					--process the dtmf
42						if (session:ready()) then
43							if (dtmf_digits == "1") then
44								timeouts = 0;
45								tutorial("record_name");
46							elseif (dtmf_digits == "2") then
47								timeouts = 0;
48								tutorial("finish");
49							else
50								if (session:ready()) then
51									timeouts = timeouts + 1;
52									if (timeouts < max_timeouts) then
53										tutorial("intro");
54									else
55										timeouts = 0;
56										tutorial("finish");
57									end
58								end
59							end
60						end
61				end
62			--record name menu
63				if (menu == "record_name") then
64					--clear the value
65						dtmf_digits = '';
66					--flush dtmf digits from the input buffer
67						session:flushDigits();
68					--play the record name press 1
69						if (session:ready()) then
70								if (string.len(dtmf_digits) == 0) then
71									dtmf_digits = macro(session, "tutorial_to_record_name", 1, 100, '');
72								end
73						end
74					--skip the name and go to password press 2
75						if (session:ready()) then
76							if (string.len(dtmf_digits) == 0) then
77									dtmf_digits = macro(session, "tutorial_skip", 1, 3000, '');
78								end
79						end
80					--process the dtmf
81						if (session:ready()) then
82							if (dtmf_digits == "1") then
83								timeouts = 0;
84								record_name("tutorial");
85							elseif (dtmf_digits == "2") then
86								timeouts = 0;
87								tutorial("change_password");
88							else
89								if (session:ready()) then
90									timeouts = timeouts + 1;
91									if (timeouts < max_timeouts) then
92										tutorial("record_name");
93									else
94										tutorial("change_password");
95									end
96								end
97							end
98						end
99				end
100			--change password menu
101				if (menu == "change_password") then
102					--clear the value
103						dtmf_digits = '';
104					--flush dtmf digits from the input buffer
105						session:flushDigits();
106					--to change your password press 1
107						if (session:ready()) then
108								if (string.len(dtmf_digits) == 0) then
109									dtmf_digits = macro(session, "tutorial_change_password", 1, 100, '');
110								end
111						end
112					--skip the password and go to greeting press 2
113						if (session:ready()) then
114							if (string.len(dtmf_digits) == 0) then
115									dtmf_digits = macro(session, "tutorial_skip", 1, 3000, '');
116								end
117						end
118					--process the dtmf
119						if (session:ready()) then
120							if (dtmf_digits == "1") then
121								timeouts = 0;
122								change_password(voicemail_id, "tutorial");
123							elseif (dtmf_digits == "2") then
124								timeouts = 0;
125								tutorial("record_greeting");
126							else
127								if (session:ready()) then
128									timeouts = timeouts + 1;
129									if (timeouts < max_timeouts) then
130										tutorial("change_password");
131									else
132										tutorial("record_greeting");
133									end
134								end
135							end
136						end
137				end
138			--change greeting menu
139				if (menu == "record_greeting") then
140					--clear the value
141						dtmf_digits = '';
142					--flush dtmf digits from the input buffer
143						session:flushDigits();
144					--to record a greeting press 1
145						if (session:ready()) then
146								if (string.len(dtmf_digits) == 0) then
147									dtmf_digits = macro(session, "tutorial_record_greeting", 1, 100, '');
148								end
149						end
150					--skip the record greeting press 2. finishes the tutorial and routes to main menu
151						if (session:ready()) then
152							if (string.len(dtmf_digits) == 0) then
153									dtmf_digits = macro(session, "tutorial_skip", 1, 3000, '');
154								end
155						end
156					--process the dtmf
157						if (session:ready()) then
158							if (dtmf_digits == "1") then
159								timeouts = 0;
160								record_greeting(nil, "tutorial");
161							elseif (dtmf_digits == "2") then
162								timeouts = 0;
163								tutorial("finish");
164							else
165								if (session:ready()) then
166									timeouts = timeouts + 1;
167									if (timeouts < max_timeouts) then
168										tutorial("record_greeting");
169									else
170										tutorial("finish");
171									end
172								end
173							end
174						end
175				end
176				if (menu == "finish") then
177					--clear the value
178						dtmf_digits = '';
179					--flush dtmf digits from the input buffer
180						session:flushDigits();
181					--update play tutorial in the datebase
182						local sql = [[UPDATE v_voicemails
183							set voicemail_tutorial = 'false'
184							WHERE domain_uuid = :domain_uuid
185							AND voicemail_id = :voicemail_id
186							AND voicemail_enabled = 'true' ]];
187						local params = {domain_uuid = domain_uuid,
188							voicemail_id = voicemail_id};
189						if (debug["sql"]) then
190							freeswitch.consoleLog("notice", "[voicemail] SQL: " .. sql .. "; params:" .. json.encode(params) .. "\n");
191						end
192						dbh:query(sql, params);
193					--go to main menu
194						main_menu();
195				end
196		end
197	end
198