var
  MiniMode:boolean;

function GetbgName:string;
var
  h,m,s,ms:word;
  str:string;
begin
  DecodeTime(now,h,m,s,ms);
  if (h>=19) or (h<6) then //晚上用黑色背景
    result:='black'
  else if ((h>=6) and (h<=7)) or ((h>=17) and (h<19)) then //早上蒙蒙亮和晚上黄昏时用灰色背景
    result:='gray'
  else//其它白天时间用蓝色背景
  begin
    result:='blue';
    str:='';
    str:=GetWeatherInfo(0,'Type');
    case str of
      'Cloudy', 
      'RainLight', 'Rain', 'RainHeavy', 'ShowerLight',
      'Shower', 'ShowerHeavy', 
      'LighteningLight', 'Lightening', 'Thunderstorms',
      'RainIcyLight', 'RainnIcy','RainIcyHeavy',
      'SnowRainMix', 'SnowLight', 'Snow', 'Snowflakes',
      'SnowHeavy', 'SnowWindy',
      'Haze', 'Smoke':
           result:='gray';
    end;
  end;
end;

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

function WeatherTypeTobg(aType:string):string;
var
  s:string;
begin
  Result:='';
  case aType of
    'Sunny': s:='sun';
	  'CloudyLightPartly': s:='sun';
    'CloudyPartly': s:='sun';
    'CloudyMostly': s:='sun';
    'SunnyHot': s:='sun';
    'Cloudy': s:='sun';
    'RainLight': s:='rainy';
    'Rain': s:='rainy';
    'RainHeavy': s:='rainy';
    'ShowerLight': s:='few_showers';
    'Shower': s:='few_showers';
    'ShowerHeavy': s:='few_showers';
    'LighteningLight': s:='thunderstorm';
    'Lightening': s:='thunderstorm';
    'Thunderstorms': s:='thunderstorm';
    'RainIcyLight': s:='rainy';
    'RainnIcy': s:='rainy';
    'RainIcyHeavy': s:='rainy';
    'SnowRainMix': s:='rainy';
    'SnowLight': s:='snow';
    'Snow': s:='snow';
    'Snowflakes': s:='snow';
    'SnowHeavy': s:='snow';
    'SnowWindy': s:='snow';
    'Hail': s:='hail';
    'Dust': s:='foggy';
    'Fog': s:='foggy';
    'Haze': s:='foggy';
    'Smoke': s:='foggy';
    'Windy': s:='windy';
    'Frigid': s:='snow';
    'Night_CloudyMostly': s:='moon-first';
    'Night_CloudyPartly': s:='moon-first';
    'Night_LightPartlyCloudy': s:='moon-first';
    'Night_Clear': s:='';
    'Night_Rain': s:='rainy';
    'Night_Snow': s:='snow';
    'Night_Thunderstorms': s:='thunderstorm';
    'NA': s:='';
  else
    s:='';
  end;
end;

function WeatherTypeToIcon(aType:string):string;
var
  s:string;
begin
  case aType of
    'Sunny': s:='';
	  'CloudyLightPartly': s:='partly-cloudy';
    'CloudyPartly': s:='partly-cloudy';
    'CloudyMostly': s:='Cloudy';
    'SunnyHot': s:='';
    'Cloudy': s:='cloudy';
    'RainLight': s:='rainy';
    'Rain': s:='rainy';
    'RainHeavy': s:='rainy';
    'ShowerLight': s:='few_showers';
    'Shower': s:='few_showers';
    'ShowerHeavy': s:='few_showers';
    'LighteningLight': s:='thunderstorm';
    'Lightening': s:='thunderstorm';
    'Thunderstorms': s:='thunderstorm';
    'RainIcyLight': s:='rainy';
    'RainnIcy': s:='rainy';
    'RainIcyHeavy': s:='rainy';
    'SnowRainMix': s:='rainy';
    'SnowLight': s:='snow';
    'Snow': s:='snow';
    'Snowflakes': s:='snow';
    'SnowHeavy': s:='snow';
    'SnowWindy': s:='snow';
    'Hail': s:='hail';
    'Dust': s:='foggy';
    'Fog': s:='foggy';
    'Haze': s:='foggy';
    'Smoke': s:='foggy';
    'Windy': s:='windy';
    'Frigid': s:='snow';
    '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;

function WeatherTypeToIndex(aType:string):string;
var
  s:string;
  i:Integer;
begin
  Result:='';
  case aType of
    'Sunny': i:=32;
	  'CloudyLightPartly': i:=34;
    'CloudyPartly': i:=28;
    'CloudyMostly': i:=26;
    'SunnyHot': i:=36;
    'Cloudy': i:=26;
    'RainLight': i:=11;
    'Rain': i:=11;
    'RainHeavy': i:=11;
    'ShowerLight': i:=9;
    'Shower': i:=9;
    'ShowerHeavy': i:=9;
    'LighteningLight': i:=3;
    'Lightening': i:=3;
    'Thunderstorms': i:=3;
    'RainIcyLight': i:=7;
    'RainnIcy': i:=7;
    'RainIcyHeavy': i:=7;
    'SnowRainMix': i:=7;
    'SnowLight': i:=41;
    'Snow': i:=41;
    'Snowflakes': i:=15;
    'SnowHeavy': i:=16;
    'SnowWindy': i:=16;
    'Hail': i:=16;
    'Dust': i:=19;
    'Fog': i:=22;
    'Haze': i:=23;
    'Smoke': i:=22;
    'Windy': i:=23;
    'Frigid': i:=41;
    'Night_CloudyMostly': i:=26;
    'Night_CloudyPartly': i:=27;
    'Night_LightPartlyCloudy': i:=28;
    'Night_Clear': i:=32;
    'Night_Rain': i:=11;
    'Night_Snow': i:=41;
    'Night_Thunderstorms': i:=3;
    'NA': i:=45;
  else
    i:=-1;
  end;
  try
    Result:=inttostr(i);
  except

  end;
end;

//更新皮肤中的天气显示信息
procedure WeatherChange;
var
  s,sIcon:string;
  h,m:integer;
begin
  Beginupdate;
  s:=GetWeatherInfo(0,'tmp');
  if s<>'' then
    lblTmp.caption:=s+'°'
  else
    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:=GetWeatherDayDesc(0);
  imgw.hint:=imgw0.Hint;
  imgw1.Hint:=GetWeatherDayDesc(1);
  imgw2.Hint:=GetWeatherDayDesc(2);
  imgw3.Hint:=GetWeatherDayDesc(3);

  DecodeTime(now,h,m,m,m);
  if MiniMode then
    s:='docked'
  else
    s:='undocked';
  if (h<6) or (h>=18) then
    imgw.FileName:=WidgetPath+'images\icons\'+s+'_moon-first-quarter.png'
  else
    imgw.FileName:=WidgetPath+'images\icons\'+s+'_sun.png';
  sIcon:=WeatherTypeToIcon(GetWeatherInfo(0,'Type'));
  if sIcon='' then
    imgw0.FileName:=''
  else
    imgw0.FileName:=WidgetPath+'images\icons\'+s+'_'+sIcon+'.png';
  imgw1.FileName:=WidgetPath+'images\small\'+WeatherTypeToIndex(GetWeatherInfo(1,'Type'))+'.png';
  imgw2.FileName:=WidgetPath+'images\small\'+WeatherTypeToIndex(GetWeatherInfo(2,'Type'))+'.png';
  imgw3.FileName:=WidgetPath+'images\small\'+WeatherTypeToIndex(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:=67;
    imgbg.FileName:=WidgetPath+'images\'+GetbgName+'docked-base.png';
    imghighlight.FileName:=WidgetPath+'images\'+GetbgName+'docked-highlight-01.png';
    imghighlight.left:=3;
    imghighlight.top:=3;
    imgRefresh.Top:=35;
    imgRefresh.left:=115;
    imgInfo.Top:=50;
    imgInfo.left:=115;
    lbltmp.left:=45;
    lbltmp.top:=5;
    lbltmp.width:=80;
    lbltmp.FontStyle.font.size:=14;
    lblCity.left:=38;
    lblCity.top:=40;
    lblCity.width:=80;
  end
  else
  begin
    Widget.Width:=264;
    Widget.Height:=194;
    imgbg.FileName:=WidgetPath+'images\'+GetbgName+'-base.png';
    imghighlight.FileName:=WidgetPath+'images\'+GetbgName+'-highlight-01.png';
    imghighlight.left:=13;
    imghighlight.top:=13;
    imgRefresh.Top:=145;
    imgRefresh.left:=230;
    imgInfo.Top:=160;
    imgInfo.left:=230;
    lbltmp.left:=100;
    lbltmp.top:=12;
    lbltmp.width:=140;
    lbltmp.FontStyle.font.size:=18;
    lblCity.left:=140;
    lblCity.top:=60;
    lblCity.width:=100;
  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;

