﻿use contacts,math,phone,time;

function VratJmeno(cislo)
  kontakty=contacts.findnr(cislo,12);
  if len(kontakty) = 0 then
    return cislo;
  end;

  kontakt=contacts.get(kontakty[0]);
  return kontakt['fname']+' '+kontakt['name'];
end;

while true do
  try
    hovor=phone.new();
    if hovor['incoming'] then
      zacatek=time.get();
      case phone.state(phone.idle | phone.active)
        in phone.active: phone.state(phone.idle);
        in phone.idle:
          delka=time.get()-zacatek;
          print time.str(zacatek,'hh:mm:ss')+' '+VratJmeno(hovor['number'])+': '+str(math.round(delka,1))+' vteřin';
      end;
    end;
  catch e by   // m-shell bug?
    print time.str(time.get(),'hh:mm:ss')+' Krátké prozvonění';
  end
end;
