property App_Name : "■■■ 禁煙マラソンカウンター ■■■" & return & return & return
property Begin_Date : ""
property Cigarette_Day : ""
property Cigarette_Price : ""

on run
	if Begin_Date = "" then
		my Begin_No_Smoking()
	else
		my Current_No_Smoking()
	end if
end run

to Begin_No_Smoking()
	tell application "Finder"
		display dialog App_Name & "禁煙開始に向けて設定します。" & return & "いくつかの質問に答えてください。" & return & return & "(※全て半角数字で入力して下さい)" buttons {"キャンセル", "設定する"} default button 2 with icon 1
		set num to text returned of (display dialog App_Name & "【Q1】" & return & "何日前から禁煙を始めましたか?" & return & return & "本日から禁煙を始める場合は「0」のままで次へお進み下さい。" default answer "0" buttons {"日前"} default button 1 with icon 1)
		set Begin_Date to (current date) - num * days
		repeat
			set How_Many to text returned of (display dialog App_Name & "【Q2】" & return & "1日にタバコを何本吸っていましたか?" default answer "" buttons {"本"} default button 1 with icon 1)
			try
				set Cigarette_Day to How_Many as number
				if Cigarette_Day ウ 1 and How_Many does not contain "." then exit repeat
			end try
		end repeat
		repeat
			set How_Much to text returned of (display dialog App_Name & "【Q3】" & return & "1箱いくらのタバコを吸っていましたか?" default answer "" buttons {"円"} default button 1 with icon 1)
			try
				set Cigarette_Price to How_Much as number
				if Cigarette_Price ウ 1 and How_Much does not contain "." then exit repeat
			end try
		end repeat
		set Set_Menu to button returned of (display dialog App_Name & "【禁煙開始日時】" & return & Begin_Date & return & return & "1日の喫煙数:" & How_Many & "本" & return & "タバコの値段:1箱" & How_Much & "円" & return & return & "上記設定でよろしいですか?" buttons {"やり直し", "禁煙開始"} default button 2 with icon 1)
		if Set_Menu = "やり直し" then my Begin_No_Smoking()
	end tell
end Begin_No_Smoking

to Current_No_Smoking()
	tell application "Finder"
		with timeout of 3600 seconds
			set K_Seconds to (current date) - Begin_Date
			set ret to my Make_ymdhm(K_Seconds)
			set Dialog_1 to "禁煙時間 " & Y of ret & "年 " & mo of ret & "月" & d of ret & "日 " & h of ret & "時間" & mi of ret & "分"
			set Dialog_2 to "吸わなかった煙草 " & K_Seconds div (86400 / Cigarette_Day) & "本"
			set Dialog_3 to "浮いたタバコ代 " & (round (K_Seconds div (86400 / Cigarette_Day)) * Cigarette_Price / 20) & "円"
			set ret2 to my Make_ymdhm(K_Seconds div (86400 / Cigarette_Day) * 330)
			set Dialog_4 to "延びた寿命  " & (Y of ret2) * 365 + (mo of ret2) * 30 + (d of ret2) & "日 " & h of ret2 & "時間" & mi of ret2 & "分"
			set Dialog_5 to "禁煙継続時間 " & Y of ret & "年 " & mo of ret & "月" & d of ret & "日 " & h of ret & "時間" & mi of ret & "分"
			set Result_1 to button returned of (display dialog App_Name & Dialog_5 & return & Dialog_2 & return & Dialog_3 & return & Dialog_4 buttons {"禁煙失敗", "クリップ"} default button 2 with icon 1)
			if Result_1 = "禁煙失敗" then
				set Delete_Data to button returned of (display dialog App_Name & "禁煙に失敗しましたか?" & return & "残念ですが、今までのデータを消去します。" buttons {"消去しない", "消去する"} default button 1 with icon 1)
				if Delete_Data = "消去する" then
					set Begin_Date to ""
					set Cigarette_Day to ""
					set Cigarette_Price to ""
				end if
			else
				set the clipboard to Dialog_1 & return & Dialog_2 & return & Dialog_3 & return & Dialog_4
			end if
		end timeout
	end tell
end Current_No_Smoking

to Make_ymdhm(sec)
	set years to 60 * 60 * 24 * 365
	set months1 to 60 * 60 * 24 * 30
	set ret to {Y:0, mo:0, d:0, h:0, mi:0}
	set Y of ret to sec div years
	set mo of ret to (sec - (Y of ret) * years) div months1
	set d of ret to (sec - (mo of ret) * months1 - (Y of ret) * years) div days
	set h of ret to (sec - (mo of ret) * months1 - (Y of ret) * years - (d of ret) * days) div hours
	set mi of ret to (sec - (mo of ret) * months1 - (Y of ret) * years - (d of ret) * days - (h of ret) * hours) div minutes
	return ret
end Make_ymdhm