var
  MiniMode:boolean;

function GetbgName:string;
var
  h,m,s,ms:word;
  str:string;
  isNight:boolean;
begin
  DecodeTime(now,h,m,s,ms);
  
  if (h>=19) or (h<=6) then //晚上用黑色背景
    isNight:=true;
  else
    isNight:=false;
    
  str:=GetWeatherInfo(0,'Type');
  if not MiniMode then
  begin
    case str of
      'Sunny','SunnyHot','Windy':
        s:='bg-sun';
  	  'Cloudy','CloudyLightPartly','CloudyPartly','CloudyMostly', 'Hail','Dust','Fog','Haze','Smoke':
  	    s:='bg-cloud';
      'Night_Rain','Night_Thunderstorms','Rain','RainLight','RainHeavy','ShowerLight','Shower','ShowerHeavy','LighteningLight','Lightening','Thunderstorms':
        s:='bg-rain';
      'Night_Snow','RainIcyLight','RainnIcy','RainIcyHeavy','SnowRainMix','SnowLight','Snow','Snowflakes','SnowHeavy','SnowWindy','Frigid':
        s:='bg-snow';
  
      'Night_CloudyMostly','Night_CloudyPartly','Night_LightPartlyCloudy':
        s:='bg-NightCloud';
      
      'Night_Clear': s:='bg-Night';
      'NA': s:='';
    else
      s:='';
    end;
  end
  else
  begin
    case str of
      'Sunny','SunnyHot','Windy':
        s:='bgMini-sun';
  	  'Cloudy','CloudyLightPartly','CloudyPartly','CloudyMostly', 'Hail','Dust','Fog','Haze','Smoke':
  	    s:='bgMini-sun';
      'Night_Rain','Rain','RainLight','ShowerLight','Shower':
        s:='bgMini-rain';
      'Night_Thunderstorms','RainHeavy','ShowerHeavy','LighteningLight','Lightening','Thunderstorms':
        s:='bgMini-BigRain';
      'RainIcyLight','RainnIcy','RainIcyHeavy','SnowRainMix','SnowLight','Snow','Snowflakes','SnowHeavy','SnowWindy','Frigid':
        s:='bgMini-snow';
      'Night_Snow':
        s:='bgnMini-snow';
      'Night_CloudyMostly','Night_CloudyPartly','Night_LightPartlyCloudy','Night_Clear':
        s:='bgnMini-Clear';
      'NA': s:='';
    else
      s:='';
    end;
  end;

  if MiniMode then
    case s of
      'bg-sun':    s:='bgMini-Sun';
      'bg-cloud':  s:='bgMini-Sun';
      'bg-rain':   s:='bgMini-Rain';
      'bg-snow':   s:='bgMini-Snow';
      'bg-NightCloud': s:='bgnMini-Clear';
      'bg-Night':   s:='bgnMini-Clear';
    end;
  result:=s;
end;

//每十分钟刷一次背景，以保证背景能自动更换
procedure OntmrTimer;
begin
  BeginUpdate;
  if MiniMode then
    s:='docked'
  else
    s:='undocked';
  imgbg.FileName:=WidgetPath+'images\'+GetbgName+s+'-base.png';
  EndUpdate;
end;

function WeatherTypeToIcon(aType:string):string;
var
  s:string;
begin
  case aType of
    'Sunny','SunnyHot': s:='s-sun';
	  'Cloudy','CloudyLightPartly','CloudyPartly','CloudyMostly': s:='s-cloud';
    
    'Rain','RainLight','RainHeavy','ShowerLight','Shower','ShowerHeavy','LighteningLight','Lightening','Thunderstorms': s:='s-rain';

    
    'RainIcyLight','RainnIcy','RainIcyHeavy','SnowRainMix','SnowLight','Snow','Snowflakes','SnowHeavy','SnowWindy','Frigid': s:='s-snow';
    'Hail','Dust','Fog','Haze','Smoke': s:='foggy';
    'Windy': s:='windy';

    'Night_CloudyMostly': s:='partly-cloudy';
    'Night_CloudyPartly': s:='partly-cloudy';
    'Night_LightPartlyCloudy': s:='partly-cloudy';
    'Night_Clear': s:='';
    'Night_Rain': s:='rainy';
    'Night_Snow': s:='snow';
    'Night_Thunderstorms': s:='thunderstorm';
    'NA': s:='';
  else
    s:='';
  end;
  result:=s;
end;

//更新皮肤中的天气显示信息
procedure WeatherChange;
var
  s,sIcon:string;
  h,m:integer;
begin
  Beginupdate;
  lblTmp.caption:=format('%s~%s°',[GetWeatherInfo(0,'tmplow'),GetWeatherInfo(0,'tmphigh')]);
  lblcity.caption:=GetWeatherInfo(0,'City');
  lblDesc0.caption:=GetWeatherInfo(0,'desc');

  s:='dddd';
  lbld1.caption:=FormatDateTime(s,now+1);
  lblw1h.caption:=GetWeatherInfo(1,'tmphigh')+'°';
  lblw1l.caption:=GetWeatherInfo(1,'tmplow')+'°';
  lbld2.caption:=FormatDateTime(s,now+2);
  lblw2h.caption:=GetWeatherInfo(2,'tmphigh')+'°';
  lblw2l.caption:=GetWeatherInfo(2,'tmplow')+'°';
  lbld3.caption:=FormatDateTime(s,now+3);
  lblw3h.caption:=GetWeatherInfo(3,'tmphigh')+'°';
  lblw3l.caption:=GetWeatherInfo(3,'tmplow')+'°';
  imgw0.Hint:='--- 今日天气 ---'#13#10+GetWeatherDayDesc(0);
  imgw1.Hint:=GetWeatherDayDesc(1);
  imgw2.Hint:=GetWeatherDayDesc(2);
  imgw3.Hint:=GetWeatherDayDesc(3);

  DecodeTime(now,h,m,m,m);
  imgbg.FileName:=WidgetPath+'images\'+GetbgName+'.png';
  imgw1.FileName:=WidgetPath+'images\'+WeatherTypeToIcon(GetWeatherInfo(1,'Type'))+'.png';
  imgw2.FileName:=WidgetPath+'images\'+WeatherTypeToIcon(GetWeatherInfo(2,'Type'))+'.png';
  imgw3.FileName:=WidgetPath+'images\'+WeatherTypeToIcon(GetWeatherInfo(3,'Type'))+'.png';
  EndUpdate;
end;

procedure SetMiniMode(value:boolean);
begin
  if docking and not value then
  begin
    nMiniMode.checked:=MiniMode;
    exit;
  end;
  MiniMode:=value;
  nMiniMode.checked:=MiniMode;
  BeginUpdate;
  WeatherChange;
  if value then
  begin
    Widget.Width:=136;
    Widget.Height:=65;
    imgbg.FileName:=WidgetPath+'images\'+GetbgName+'.png';
    imgRefresh.Top:=32;
    imgRefresh.left:=111;
    imgInfo.Top:=46;
    imgInfo.left:=111;
    lbltmp.left:=45;
    lbltmp.top:=5;
    lbltmp.width:=80;
    lbltmp.FontStyle.font.size:=14;
    lblCity.left:=35;
    lblCity.top:=38;
    lblCity.width:=80;
    imgw0.width:=64;
    imgw0.height:=64;
  end
  else
  begin
    Widget.Width:=242;
    Widget.Height:=160;
    imgbg.FileName:=WidgetPath+'images\'+GetbgName+'.png';
    imgRefresh.Top:=130;
    imgRefresh.left:=212;
    imgInfo.Top:=145;
    imgInfo.left:=212;
    lbltmp.left:=80;
    lbltmp.top:=12;
    lbltmp.width:=140;
    lbltmp.FontStyle.font.size:=18;
    lblCity.left:=100;
    lblCity.top:=60;
    lblCity.width:=120;
    imgw0.width:=128;
    imgw0.height:=128;
  end;
  lbld1.visible:=not value;
  lblDesc0.visible:=lbld1.visible;
  lblw1h.visible:=lbld1.visible;
  lblw1l.visible:=lbld1.visible;
  lbld2.visible:=lbld1.visible;
  lblw2h.visible:=lbld1.visible;
  lblw2l.visible:=lbld1.visible;
  lbld3.visible:=lbld1.visible;
  lblw3h.visible:=lbld1.visible;
  lblw3l.visible:=lbld1.visible;
  imgw1.visible:=lbld1.visible;
  imgw2.visible:=lbld1.visible;
  imgw3.visible:=lbld1.visible;
  EndUpdate;
  SetSettingInt('MiniMode',MiniMode);
end;

//--- Widget移入边栏事件，当Widget被移到边栏上时会触发此事件 ---
procedure OnWidgetDock;
begin
  SetMiniMode(true);
end;

//--- Widget移出边栏事件，当Widget被移出边栏时会触发此事件 ---
procedure OnWidgetUnDock;
begin
  SetMiniMode(false);
end;

procedure ChangeView;
begin
  SetMiniMode(not MiniMode);
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
begin
  MiniMode:=GetSettingInt('MiniMode',0)=1;
  BeginUpdate;
  WeatherChange;
  SetMiniMode(MiniMode);
  EndUpdate;
end;

//--- Widget关闭事件，当关闭Widget时会触发此事件 ---
procedure OnWidgetClose;
begin
  
end;

//--- 天气刷新事件，当天气成功刷新后会触发此事件 ---
procedure OnRefreshWidgetWeatherInfo;
begin
  WeatherChange;
end;

