var
  lstWords:TStrings;
  iIndex:integer;
  tts:Variant;
  AutoSay:boolean;
  OrderType:integer; //0:默认顺序 1:随机 2:不重复随机

procedure ShowWords(isNext:boolean);
var
  s:string;
begin
  case OrderType of
    1: //随机
      begin
        Randomize;
        iIndex:=round(random * lstWords.count);
      end;
    2: //随机不重复
      begin
        if lstWords.Count<=1 then
        begin
          lstWords.LoadFromFile(WidgetPath+GetSetting('Dict','word4.txt'));
        end
        else if (iIndex>=1) and (iIndex<lstWords.Count) then
          lstWords.Delete(iIndex);
        Randomize;
        iIndex:=round(random * lstWords.count);
      end;
    else //默认顺序
      begin
        if isNext then inc(iIndex)
          else iIndex:=iIndex-1;
      end;
  end;
  if iIndex>=lstWords.count then iIndex:=1
    else if iIndex<=0 then iIndex:=lstWords.count-1;
  s:=lstWords.strings[iIndex];
  BeginUpdate;
  lblWord.caption:=StrBefore(#9,s);
  lblDesc.caption:=StrAfter(#9,s);
  if Docking then
    lblDesc.hint:=lblWord.caption+#13#10+lblDesc.caption;
  EndUpdate;
  if AutoSay then
    tts.Speak(lblWord.caption);
  //showtrayhint('Count',lstWords.count);
end;

function LoadSAPI:boolean;
begin
  try
    tts:=CreateOleObject('SAPI.SpVoice');
    result:=true;
  except
    result:=false;
  end;
end;

procedure imgsoundClick(Sender: TObject);
begin
  BeginUpdate;
  if AutoSay then
  begin
    imgSound.ImageStyle.Opacity:=158
    AutoSay:=false;
    imgSound.Hint:='启用自动语音朗读';
  end
  else
  begin
    imgSound.ImageStyle.Opacity:=255;
    AutoSay:=true;
    if not LoadSAPI then
    begin
      AutoSay:=false;
      imgSound.ImageStyle.Opacity:=158;
    end
    else
      imgSound.Hint:='停止自动语音朗读';
  end;
  if AutoSay then SetSetting('AutoSay',1)
    else SetSetting('AutoSay',0);
  //imgSound.NeedRefresh:=true;
  EndUpdate;
  nAutoSay.checked:=AutoSay;
end;

procedure nDict4Click(Sender: TObject);
var
  fn:string;
begin
  case TMenuItem(Sender).Tag of
    0: //英语四级
      fn:='word4.txt';
    1: //英语六级
      fn:='word6.txt';
    2: //职称英语
      fn:='wordzc.txt';
    else
      fn:='word4.txt';
  end;
  SetSetting('Dict',fn);
  SetSettingInt('Index',1);
  lstWords.LoadFromFile(WidgetPath+fn);
  iIndex:=0;
end;

//--- Widget鼠标滚轮事件，当在Widget上进行滚轮操作时会触发此事件 ---
procedure OnWidgetMouseWheel(WheelDelta);
begin
  if WheelDelta>0 then
    ShowWords(false)
  else
    ShowWords(true);
end;

//--- Widget移入边栏事件，当Widget被移到边栏上时会触发此事件 ---
procedure OnWidgetDock;
begin
  BeginUpdate;
  Widget.Width:=136;
  Widget.Height:=58;
  imgbg.width:=130;
  imgbg.height:=58;
  lblWord.width:=100;
  lblDesc.width:=lblWord.width;
  lblDesc.height:=32;
  EndUpdate;
end;

//--- Widget移出边栏事件，当Widget被移出边栏时会触发此事件 ---
procedure OnWidgetUnDock;
begin
  BeginUpdate;
  Widget.Width:=310;
  Widget.Height:=57;
  imgbg.width:=300;
  imgbg.height:=38;
  lblWord.width:=266;
  lblDesc.width:=lblWord.width;
  lblDesc.height:=16;
  lblDesc.hint:='';
  EndUpdate;
end;

procedure tmrTimer(Sender: TObject);
begin
  ShowWords(true);
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
var
  s:string;
  i:integer;
begin
  nOrderType_RandomNoRepeat.tag:=2;
  nOrderType_Random.tag:=1;
  iIndex:=GetSettingInt('Index',0);
  lstWords:=TStringList.Create;
  s:=LowerCase(GetSetting('Dict','word4.txt'));
  lstWords.LoadFromFile(WidgetPath+s);
  case s of
    'word4.txt': nDict4.checked:=true;
    'word6.txt': nDict6.checked:=true;
    'wordzc.txt': nDictzc.checked:=true;
  end;
  OrderType:=GetSettingInt('OrderType',2);
  case OrderType of
    0: nOrderType_Random.checked:=true;
    1: nOrderType_Order.checked:=true;
  end;
  AutoSay:=GetSettingInt('AutoSay',0)=1;
  nAutoSay.checked:=AutoSay;
  
  BeginUpdate;
  if AutoSay then
  begin
    if not LoadSAPI then
      AutoSay:=false
    else
    begin
      imgSound.ImageStyle.Opacity:=255;
    end;
  end;
  i:=GetSettingInt('bgIndex',2);
  if i<>2 then
  	imgbg.FileName:=WidgetPath+'images\bg'+IntToStr(i)+'.png';
  EndUpdate;
  
  tmr.Interval:=GetSettingInt('Speed',5)*1000;
  tmrTimer(nil);
  tmr.Enabled:=GetSettingInt('AutoPlay',1)=1;
  nAutoPlay.checked:=tmr.Enabled;
  nDict4.OnClick:='nDict4Click';
  nDict6.OnClick:='nDict4Click';
  nDictzc.OnClick:='nDict4Click';
end;

//--- Widget关闭事件，当关闭Widget时会触发此事件 ---
procedure OnWidgetClose;
begin
  lstWords.Free;
  SetSetting('Index',iIndex);
end;

procedure imgPlayClick(Sender: TObject);
begin
  tmr.Enabled:=not tmr.Enabled;
  if tmr.Enabled then
  begin
    imgPlay.FileName:=WidgetPath+'Pause.png';
    SetSetting('AutoPlay',1);
  end
  else
  begin
    imgPlay.FileName:=WidgetPath+'Play.png';
    SetSetting('AutoPlay',0);
  end;
  nAutoPlay.checked:=tmr.Enabled;
end;

procedure nAutoSayClick(Sender: TObject);
begin
  imgsoundClick(nil);
end;

procedure nOrderType_RandomNoRepeatClick(Sender: TObject);
begin
  OrderType:=TMenuItem(Sender).Tag;
  SetSettingInt('OrderType',OrderType);
  //showtrayhint('ordertype',ordertype);
end;

procedure nAutoPlayClick(Sender: TObject);
begin
  tmr.Enabled:=nAutoPlay.checked;
  if nAutoPlay.checked then
    SetSetting('AutoPlay',1)
  else
    SetSetting('AutoPlay',0);
end;

procedure nSkin0Click(Sender: TObject);
var
  i:integer;
begin
  i:=TMenuItem(Sender).tag;
  imgbg.FileName:=WidgetPath+'images\bg'+IntToStr(i)+'.png';
  SetSettingInt('bgIndex',i);
end;