<?xml version='1.0' encoding='UTF-8'?>
<macro name="keratometri" version="1.0">
  <resume language="lua">
    function tonmb(value)
      value = string.gsub(value, ',', '.')
      return tonumber(value)
    end

    function tostr(value)
      value = string.format('%.02f', value)
      value = string.gsub(value, '\\.', ',')
      return value
    end

    out = ''
    
    if ( getValue('keratometri.mangler.odxt') == '' )
    then

      rr1 = tonmb(getValue('keratometri.r1.odxt'))
      rr2 = tonmb(getValue('keratometri.r2.odxt'))
      rx1 = tonmb(getValue('keratometri.x1.odxt'))

      if ( rx1 &gt;= 0 and rx1 &lt; 90 ) then
        rx2 = rx1 + 90
      end
    
      if ( rx1 &gt;= 90 and rx1 &lt;= 180 ) then
        rx2 = rx1 - 90
      end

      rd1 = tonumber(string.format('%.02f', 337.5 / rr1))
      rd2 = tonumber(string.format('%.02f', 337.5 / rr2))

      if ( rd1 &lt; rd2 )
      then
        ry1 = tonumber(string.format('%.02f', rd1 - rd2))
        ry2 = rx1
      else
        ry1 = tonumber(string.format('%.02f', rd2 - rd1))
        ry2 = rx2
      end

      rr1 = tostr(rr1)
      rr2 = tostr(rr2)

      print (rr1)

      out = out .. 'Keratometri, o.dxt:\n' ..
      'Radius 1: ' .. rr1 .. ' mm (' .. rd1 .. ' D) i ' .. rx1 .. ' grader.\n' .. 
      'Radius 2: ' .. rr2 .. ' mm (' .. rd2 .. ' D) i ' .. rx2 .. ' grader.\n' ..
      'Cylinder: ' .. ry1 .. ' i ' .. ry2 .. ' grader\n\n'

    end

    if ( getValue('keratometri.mangler.osin') == '' )
    then

      lr1 = tonmb(getValue('keratometri.r1.osin'))
      lr2 = tonmb(getValue('keratometri.r2.osin'))
      lx1 = tonmb(getValue('keratometri.x1.osin'))

      if ( lx1 &gt;= 0 and lx1 &lt; 90 ) then
        lx2 = lx1 + 90
      end
    
      if ( lx1 &gt;= 90 and lx1 &lt;= 180 ) then
        lx2 = lx1 - 90
      end

      ld1 = tonumber(string.format('%.02f', 337.5 / lr1))
      ld2 = tonumber(string.format('%.02f', 337.5 / lr2))

      if ( ld1 &lt; ld2 )
      then
        ly1 = tonumber(string.format('%.02f', ld1 - ld2))
        ly2 = lx1
      else
        ly1 = tonumber(string.format('%.02f', ld2 - ld1))
        ly2 = lx2
      end

      lr1 = tostr(lr1)
      lr2 = tostr(lr2)

      print (lr1)

      out = out .. 'Keratometri, o.sin:\n' ..
      'Radius 1: ' .. lr1 .. ' mm (' .. ld1 .. ' D) i ' .. lx1 .. ' grader.\n' .. 
      'Radius 2: ' .. lr2 .. ' mm (' .. ld2 .. ' D) i ' .. lx2 .. ' grader.\n' ..
      'Cylinder: ' .. ly1 .. ' i ' .. ly2 .. ' grader'

    end

    return out
  </resume>
  <queries>
    <query class="keratometri" ttl="100000"/>
    <query service="pracro" class="missing_eye_odxt" ttl="1000000"/>
    <query service="pracro" class="missing_eye_osin" ttl="1000000"/>
  </queries>
  <maps>
    <map name="missing_eye_odxt_map">
      value = 0
      timestamp = 0
      source = 'pracro'

      if( missing_eye_odxt )
      then
        if( missing_eye_odxt.value and missing_eye_odxt.value == '' )
        then
          value = ''
          timestamp = missing_eye_odxt.timestamp
        else
          value = 'mangler'
          timestamp = missing_eye_odxt.timestamp
        end
      end
      return value, timestamp, source
    </map>
    <map name="missing_eye_osin_map">
      value = 0
      timestamp = 0
      source = 'pracro'
      
      if( missing_eye_osin )
      then
        if( missing_eye_osin.value and missing_eye_osin.value == '' )
        then
          value = ''
          timestamp = missing_eye_osin.timestamp
        else
          value = 'mangler'
          timestamp = missing_eye_osin.timestamp
        end
      end
      return value, timestamp, source
    </map>
  </maps>
  <scripts>
     <script language="lua" name="lt20gt0">
      if(value == '')
      then
        return false
      end

      value = string.gsub(value, ',', '.')
      if(tonumber(value) &gt;= 0 and tonumber(value) &lt;= 20)
      then
        return true
      else
        return false
      end
    </script>
    <script language="lua" name="lt180gt0">
      if(value == '')
      then
        return false
      end

      value = string.gsub(value, ',', '.')
      if(tonumber(value) &gt;= 0 and tonumber(value) &lt;= 180)
      then
        return true
      else
        return false
      end
    </script>
    <script language="lua" name="right_eye">
      if ( value == 'mangler' )
      then
        disable('right_eye_frame')
      else
        enable('right_eye_frame')
      end
      return true
    </script>
    <script language="lua" name="left_eye">
      if ( value == 'mangler' )
      then
        disable('left_eye_frame')
      else
        enable('left_eye_frame')
      end
      return true
    </script>
  </scripts>
  <widgets caption="Keratometri"
          layout="vbox">

    <frame layout="hbox">

      <!-- o.dxt -->
      <frame layout="vbox">
        <checkbox caption="Der er ikke målt på o.dxt"
		  name="keratometri.mangler.odxt" script="right_eye"
		  map="missing_eye_odxt_map"
		  truevalue="mangler" falsevalue="" value=""/>
	<frame name="right_eye_frame" caption="o.dxt" layout="hbox">
	  
	  <frame layout="vbox">
	    <label caption="R1:"/>
	    <lineedit name="keratometri.r1.odxt" regexp="[+-]{0,1}\d{1,2},\d{2}" script="lt20gt0" value=""/>
	  </frame>
	  <frame layout="vbox">
	    <label caption="R2:"/>
	    <lineedit name="keratometri.r2.odxt" regexp="[+-]{0,1}\d{1,2},\d{2}" script="lt20gt0" value=""/>
	  </frame>
	  <frame layout="vbox">
	    <label caption="X1 (fladeste akse):"/>
	    <lineedit name="keratometri.x1.odxt" regexp="[\d]+" script="lt180gt0" value=""/>
	  </frame>
	</frame>
      </frame>
      
      <!-- o.sin -->
      <frame layout="vbox">
	<checkbox caption="Der er ikke målt på o.sin"
		  name="keratometri.mangler.osin" script="left_eye"
		  map="missing_eye_osin_map"
		  truevalue="mangler" falsevalue="" value=""/>
	<frame name="left_eye_frame" caption="o.sin" layout="hbox">
	  
	  <frame layout="vbox">
	    <label caption="R1:"/>
	    <lineedit name="keratometri.r1.osin" regexp="\d{1,2},\d{2}" script="lt20gt0" value=""/>
	  </frame>
	  <frame layout="vbox">
	    <label caption="R2:"/>
	    <lineedit name="keratometri.r2.osin" regexp="\d{1,2},\d{2}" script="lt20gt0" value=""/>
	  </frame>
	  <frame layout="vbox">
	    <label caption="X1 (fladeste akse):"/>
	    <lineedit name="keratometri.x1.osin" regexp="[\d]+" script="lt180gt0" value=""/>
	  </frame>
	</frame>
      </frame>

    </frame>
    
    <!-- Knapper -->
    <frame layout="hbox">
      <spacer />
      <button caption="Gem" action="commit"/>
    </frame>
  </widgets>
</macro>