#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=C:\Program Files\AutoIt3\Aut2Exe\Icons\AutoIt_Main_v10_256x256_RGB-A.ico
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_UseUpx=y
#AutoIt3Wrapper_Compile_Both=y
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <IE.au3>
#include <MsgBoxConstants.au3>
#include <array.au3>
#include <WinAPISys.au3>
Global $otd, $vzl, $vzls = 0, $vzlss = 0, $speed, $oIE, $max
Global $ostrongs, $srrs = False
Global $obg, $obgn, $obgp
Global $act = 1
sr()
;~ MsgBox(0,$otd,$vzl)
ent()

ref()
povt()

Func act()
	$act = _WinAPI_GetIdleTime()+$act/2
EndFunc
;~ _IELoadWait($oIE)
;~ enter()
;~ _IELoadWait($oIE)

;~ _IEAction($oIE,"refresh")
;~ _IELoadWait($oIE)
Func ref()
if StringInStr(_IEBodyReadText($oIE), "Произошла неизвестная ошибка") Then
	_IENavigate($oIE, "https://my.yota.ru/selfcare/devices")
EndIf
if StringInStr(_IEBodyReadText($oIE), "Не удается") Then
	_IENavigate($oIE, "https://my.yota.ru/selfcare/devices")
EndIf
$url = _IEPropertyGet($oIE, "locationurl")
if StringInStr($url, "changeOffer") Then _IENavigate($oIE, "https://my.yota.ru/selfcare/devices")
if $url <> "https://my.yota.ru/selfcare/devices" And $url <> "https://my.yota.ru/selfcare/login" Then
	_IENavigate($oIE, "https://my.yota.ru/selfcare/devices")
Elseif Not $srrs Then
	_IEAction($oIE,"refresh")
	povt()
EndIf

$url = _IEPropertyGet($oIE, "locationurl")
if $url <> "https://my.yota.ru/selfcare/devices" And $url <> "https://my.yota.ru/selfcare/login" Then
	_IENavigate($oIE, "https://my.yota.ru/selfcare/devices")
EndIf

EndFunc
Func ent()
ConsoleWrite("entering"&@CRLF)
$errc = 0
While 1
Sleep(250)
$oIE = _IECreate("my.yota.ru", 1, 1, 1, 0)
$aProcessList = ProcessList("iexplore.exe")
if $aProcessList[0][0] > 2 Then
	For $i = 1 To $aProcessList[0][0]
		ProcessClose($aProcessList[$i][1])
	Next
EndIf
$1 = WinWait("","Internet Explorer", 5)
if $1 > 0 Then WinSetState($1,"", @SW_MINIMIZE)
if $errc > 5 Then
	_IEQuit($oIE)
	$aProcessList = ProcessList("iexplore.exe")
	if $aProcessList[0][0] > 0 Then
		For $i = 1 To $aProcessList[0][0]
			ProcessClose($aProcessList[$i][1])
		Next
	EndIf
$oIE = _IECreate("my.yota.ru", 1, 1, 1, 0)
$1 = WinWait("","Internet Explorer", 5)
if $1 > 0 Then WinSetState($1,"", @SW_MINIMIZE)
_IELoadWait($oIE)
Sleep(300)
_IELoadWait($oIE)
$errc = 0
EndIf
$url = _IEPropertyGet($oIE, "locationurl")
if StringInStr($url, "changeOffer") Then _IENavigate($oIE, "https://my.yota.ru/selfcare/devices")
ConsoleWrite("create"&@CRLF)
if @error <> 0 Then ContinueLoop
_IEGetObjById($oIE, "logo")
if @error Then
enter()
ConsoleWrite("entering"&@CRLF)
EndIf
_IELoadWait($oIE)
_IEGetObjById($oIE, "logo")
if @error = 0 Then ExitLoop
$errc += 1
WEnd
ConsoleWrite("entered"&@CRLF)
_IELoadWait($oIE)
Sleep(1000)
_IELoadWait($oIE)
EndFunc
AdlibRegister("sr",1000)
AdlibRegister("act",1000)
AdlibRegister("srr", 5000)
AdlibRegister("srrs", 15000)
AdlibRegister("ref", 60000)
AdlibRegister("povt", 50000)
;~ 			_IELinkClickByIndex($oIE, 22)

Func povt()
$hWnd = WinWait("Windows Internet Explorer", "Чтобы еще раз отобразить эту веб-страницу", 3)
if $hWnd Then
 ControlClick($hWnd, "", "[CLASS:Button; INSTANCE:1]", "", 2)
EndIf
EndFunc

While 1
	Sleep(250)
WEnd

Func sr()
$text = WinGetText("BitMeter")
$n = StringInStr($text, "отдал: ")
$stl = StringLen($text)
$otd1 = StringMid($text, $n+7)
$vzl1 = StringMid($text, 9, $n-10)
$otd = Number($otd1)
$vzl = Number($vzl1)
if $vzls < $vzl Then
	$vzls = $vzl
Else
	$vzls = Floor(($vzls + $vzl)/2)
EndIf
;~ ConsoleWrite($vzlss & @CRLF)
EndFunc

Func srr()
	Local $ispeed
	$vzlss = Floor(( $vzlss + $vzls + $vzl)/3)
	if $act < 15000 Then $vzlss +=15
	if Not $srrs Then
	Switch $vzlss
		Case 0 to 15
			$ispeed = 0
		Case 15 to 80
			$ispeed = 1.0
		Case 80 to 200
			$ispeed = 2.1
		Case 200 to 300
			$ispeed = 3.1
		Case 300 to 400
			$ispeed = 4.1
		Case 400 to 500
			$ispeed = 5.0
		Case 500 to 600
			$ispeed = 6.4
		Case 600 to 900
			$ispeed = 8.5
		Case 900 to 1050
			$ispeed = 10.0
		Case 1050 to 1300
			$ispeed = 12.0
		Case 1300 to 1700
			$ispeed = 15.0
		Case Else
			$ispeed = 20
	EndSwitch
	if $ispeed > $speed Then srrs()
	EndIf
EndFunc

Func srrs()
$srrs = True
	Switch $vzlss
		Case 0 to 15
			$speed = 0
		Case 15 to 80
			$speed = 1.0
		Case 80 to 200
			$speed = 2.1
		Case 200 to 300
			$speed = 3.1
		Case 300 to 400
			$speed = 4.1
		Case 400 to 500
			$speed = 5.0
		Case 500 to 600
			$speed = 6.4
		Case 600 to 900
			$speed = 8.5
		Case 900 to 1050
			$speed = 10.0
		Case 1050 to 1300
			$speed = 12.0
		Case 1300 to 1700
			$speed = 15.0
		Case Else
			$speed = 20
	EndSwitch
ConsoleWrite("$speed="&$speed&@CRLF)
_IELoadWait($oIE)
$iispeed = _IETagNameGetCollection($oIE, "strong", 1)
$err = @error
$ostrongs = _IETagNameGetCollection($oIE, "strong", 4)
if @error > 0 Then $err = @error
if $err Then
	ent()
	$srrs = False
	Return
EndIf
ConsoleWrite("$ostrongs="& $ostrongs.innertext & @CRLF)
ConsoleWrite("$iispeed="& $iispeed.innertext & @CRLF)
if Not @error Then
$iispeed = $iispeed.innertext
if Number($iispeed)<>$speed Then
		if $iispeed==64 Then $iispeed = 0
		if $iispeed==320 Then $iispeed = 0
		if $iispeed>320 Then $iispeed = 1
		if $iispeed=="Макс." Then $iispeed = 20
		$iispeed = Floor($iispeed)
ConsoleWrite("$iispeed="& $iispeed & @CRLF)
		$speed = Floor($speed)
ConsoleWrite("$speed="&$speed&@CRLF)
		if $iispeed < $speed Then _IELinkClickByIndex($oIE, 21)
		if $iispeed > $speed Then _IELinkClickByIndex($oIE, 20)
	While 1
		Sleep(250)
		$ostrongs = _IETagNameGetCollection($oIE, "strong", 4)
		$ispeed = $ostrongs.innertext
		if $ispeed=="Макс." Then $ispeed = 20
		if $ispeed==64 Then $ispeed = 0
		if $ispeed==320 Then $ispeed = 0
		if $ispeed>320 Then
			$ispeed = $ispeed/1000
		Else
			$ispeed = Floor($ispeed)
		EndIf
		$speed = Floor($speed)
		ConsoleWrite("$ispeed="& $ispeed & @CRLF)
		if $ispeed < $speed Then _IELinkClickByIndex($oIE, 21)
		if $ispeed > $speed Then _IELinkClickByIndex($oIE, 20)
		if $ispeed = $speed Then
			Sleep(1000)
			_IELinkClickByIndex($oIE, 22)
			ConsoleWrite(@error & @CRLF)
			ExitLoop
		EndIf
	WEnd
EndIf
Else
	ConsoleWrite(@ScriptLineNumber & " err "& $err & @CRLF)
EndIf
Sleep(5000)
$srrs = False

EndFunc

Func enter()
$obgn=_IEGetObjByName($oIE, "IDToken1")
if @error Then Return
$obgp=_IEGetObjByName($oIE, "IDToken2", 1)
;~ $obgs=_IEGetObjById($oIE, "doSubmitLoginForm")
;~ MsgBox(0,"",_IEFormElementGetValue($obgn))
_IEFormElementSetValue($obgn, "login")
_IEFormElementSetValue($obgp, "pass")
_IEImgClick($oIE, "white-button-arrow-right.png", "src")
EndFunc
;~ MsgBox(0,"",_IEFormElementGetValue($obgp))



$oLinks = _IELinkGetCollection($oIE)
;~ $iNumLinks = @extended
;~ $sTxt = $iNumLinks & " links found" & @CRLF & @CRLF
;~ $i = 0
;~ For $oLink In $oLinks
;~     $sTxt &= $i & ", " & $oLink.href & ", " & $oLink.innertext & @CRLF
;~ 	$i+=1
;~ Next
;~ MsgBox($MB_SYSTEMMODAL, "Link Info", $sTxt)

_IELinkClickByIndex($oIE, 21)

$ostrongs = _IETagNameGetCollection($oIE, "strong")

;~ $ostrongs[1].innertext



;~ подтвердить index - 17

;~ кнопки минус плюс index - 20,21
;~ скорость index 1,4

_IEQuit($oIE)

;~ $oLinks = _IELinkGetCollection($oIE)
;~ $iNumLinks = @extended
;~ $sTxt = $iNumLinks & " links found" & @CRLF & @CRLF
;~ For $oLink In $oLinks
;~     $sTxt &= $oLink.href & @CRLF
;~ Next
;~ MsgBox($MB_SYSTEMMODAL, "Link Info", $sTxt)
;~ MsgBox(0,"",_IEBodyReadText($obg))

;~ $oSubmit = _IEGetObjByName($oIE, "submitExample")
;~ _IEAction($oSubmit, "click")

;~ '<div class="speed"><strong>1.0</strong>'

;~ '<div class="decrease"><a class="icon" href="#"></a></div>'

;~ '<div class="increase"><a class="icon" href="#"></a></div>'