#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#先在discourse.csv設定音檔和選項音檔，在discoursewav檔案夾中放相應音檔提供播放，此程式無錄音功能，僅呈現問題。
#pyinstaller -F --icon=qq.ico jdiscourse.py

import random
import tkinter
from tkinter import *
import tkinter.font as font
from tkinter import messagebox
import sounddevice as sd
import soundfile as sf
import pandas


fs = 22050
BACKGROUND_COLOR = "#c9c88b"
FOREGROUND_COLOR = "black"
current_card={}
to_show={}
itemno=0
showno=0
wavpath='discoursewav'
wavfilename=''


window= Tk()
myFont1 = font.Font(size=20,weight="bold")
#myFont2 = font.Font(family='文鼎標楷注音',size=50,weight="bold") #設定字型
myFont2 = font.Font(family = "Comic Sans MS",size = 50,weight = "bold")
window.title("段落理解")
swidth = window.winfo_screenwidth()-10
sheight = window.winfo_screenheight()-10
print("Screen width:", swidth)
print("Screen height:", sheight)

window.geometry(f'{swidth}x{sheight}')
window.config(padx=50, pady=50, bg = BACKGROUND_COLOR)
window.resizable(False,False) #Make window non-resizable, optimal view
mycanvas= Canvas(width=30, height=20)
mycanvas.place(x=swidth-100, y=0) # put itemno place
choose1_button = Button(window,text='') 
choose2_button = Button(window,text='') 
choose3_button = Button(window,text='') 
choose4_button = Button(window,text='') 

word1='因錄音時間不定，本程式不錄音，需另用錄音機錄音以蒐集語音。'
word2='先按[下一題] 聽題目，之後按下問題按鈕，聽問題並回答。'
label = Label(window, text=str(word1), font=("Ariel", 16))
label.place(x=10, y=0)
labe2 = Label(window, text=str(word2), font=("Ariel", 16))
labe2.place(x=10, y=30)
showno_text=mycanvas.create_text(9,8, text=str(showno), font=("Ariel", 12), width = 10)

try:
    data=pandas.read_csv("discourse.csv")
except FileNotFoundError:
    original_data= pandas.read_csv("discourse.csv")
    #print(original_data)
    to_show=original_data.to_dict(orient="records")
    total_num=len(to_show)
else:
    to_show=data.to_dict(orient="records")
    total_num=len(to_show)
    
    
def nextsti():
    global current_card, itemno, showno,card_word,total_num,wavfilename,choose1,choose2,choose3,choose4
    choose1_button.destroy()
    choose2_button.destroy()
    choose3_button.destroy()
    choose4_button.destroy()
    showno += 1
    current_card= random.choice(to_show) #random items from list to show
    print(current_card)
    
    itemno=current_card["itemno"]
    wavfilename=current_card["wavfile"]
    choose1=current_card["choice1"]
    choose2=current_card["choice2"]
    choose3=current_card["choice3"]
    choose4=current_card["choice4"]
    playsti()
    mycanvas.itemconfig(showno_text, text=str(showno))
    showchoice()
    if len(to_show) >= 2:
        to_show.remove(current_card)
        data=pandas.DataFrame(to_show)
        print(len(to_show))        
    else:
        next_button["state"] = DISABLED
             
def playsti():
    global wavfilename
    data, fs = sf.read(wavpath+'/'+wavfilename, dtype='int16')
    sd.play(data, fs)
    sd.wait()
def playsti2(qwavfilename):
    data2, fs = sf.read(wavpath+'/'+qwavfilename, dtype='int16')
    sd.play(data2, fs)
    sd.wait()
    
def showchoice(): 
    global choose1,choose2,choose3,choose4, choose1_button,choose2_button,choose3_button,choose4_button
    choose1_button = Button(window,text='問題一',font= myFont2,highlightthickness=0,command=resp1) 
    choose1_button.place(x=5, y=0, width=swidth/2.7, height=sheight/2.6)
    
    choose2_button = Button(window,text='問題二',font= myFont2,highlightthickness= 0, command=resp2) 
    choose2_button.place(x=swidth/2-25, y=0, width=swidth/2.7, height=sheight/2.6)
  
    choose3_button = Button(window,text='問題三',font= myFont2,highlightthickness= 0, command=resp3) 
    choose3_button.place(x=5, y=sheight/2-20, width=swidth/2.7, height=sheight/2.6)
        
    choose4_button = Button(window,text='問題四',font= myFont2,highlightthickness= 0, command=resp4 )
    choose4_button.place(x=swidth/2-25, y=sheight/2-20, width=swidth/2.7, height=sheight/2.6)
     
def resp1():
    global choose1
    playsti2(choose1)
    choose1_button.destroy()
    
def resp2():
    global choose2
    playsti2(choose2)
    choose2_button.destroy()
    

def resp3():
    global choose3
    playsti2(choose3)
    choose3_button.destroy()
    

def resp4():
    global choose4
    playsti2(choose4)
    choose4_button.destroy()
    if next_button["state"] == DISABLED:
        messagebox.showinfo('The end', '段落理解作業完畢，請檢視記錄檔!')
 
next_button = Button(window,text='下一題',highlightthickness= 1,bg = "#85cced", font=myFont1, command=nextsti) 
next_button['font'] = myFont1
next_button.place(x=swidth-240, y=sheight-200, width=140, height=100)

window.mainloop()