import ac
import acsys
import math
import time
import threading
import configparser
import os
import platform
import sys
import json
import random

if platform.architecture()[0] == "64bit":
	sysdir = os.path.dirname(__file__)+'/stdlib64'
else:
	sysdir = os.path.dirname(__file__)+'/stdlib'

sys.path.insert(0, sysdir)
os.environ['PATH'] = os.environ['PATH'] + ";."

from sim_info import info
from random import random
from array import *

car_name = ""

gear_info = 0
stock_boost = 0
max_boost = 0
turbo_count = 1
set_boost_by_gear = 0
banner_clicked = 0
banner_img = 0

time0 = 0
time1 = 0
time2 = 0
time3 = 0

isLoaded = False

def acMain(ac_version):
   global appWindow, isLoaded, car_name, gearR, gearN, gear1, gear2, gear3, gear4, gear5, gear6, banner, stock_boost
   global info_text, info_text2
   
   car_name = ac.getCarName(0)   
   if "ecu_pro" in car_name:
      return 
   
   appWindow = ac.newApp("D&O Boost by Gear")
   ac.setTitle(appWindow, "")   
   ac.setSize(appWindow, 380, 280)
   ac.drawBorder(appWindow, 0)
   ac.setIconPosition(appWindow, 0, -10000)   
   #ac.setBackgroundOpacity(appWindow,0)

   banner = ac.addLabel(appWindow, "");
   ac.setPosition(banner, 0, 0)
   ac.setSize(banner, 380, 80)
   ac.addOnClickedListener(banner, fun_banner)
   
   ac.setBackgroundOpacity(banner,0)


   info_text = ac.addLabel(appWindow, "D&O Boost By Gear")
   ac.setPosition(info_text, 115, 80)
   ac.setSize(info_text, 380, 80)
   #ac.setBackgroundTexture(logo,"apps/python/Boost_by_gear/data/a_00000.png")
   ac.setVisible(info_text, 1)
   ac.setFontSize(info_text, 15)

   info_text2 = ac.addLabel(appWindow, "Set turbo pressure(psi):")
   ac.setPosition(info_text2, 132, 97)
   ac.setSize(info_text2, 380, 80)
   #ac.setBackgroundTexture(logo,"apps/python/Boost_by_gear/data/a_00000.png")
   ac.setVisible(info_text2, 1)
   ac.setFontSize(info_text2, 10)
   
   gearN = ac.addSpinner(appWindow, "N")
   ac.setPosition(gearN, 50, 170)
   ac.setSize(gearN, 100, 20)
   ac.setValue(gearN, 0)
   ac.setRange(gearN, 0, 100)  
   ac.setStep(gearN, 1)             
   
   gearR = ac.addSpinner(appWindow, "R")
   ac.setPosition(gearR, 50, 130)
   ac.setSize(gearR, 100, 20)
   ac.setValue(gearR, 0)
   ac.setRange(gearR, 0, 100)  
   ac.setStep(gearR, 1)             

   gear1 = ac.addSpinner(appWindow, "1st")
   ac.setPosition(gear1, 50, 210)
   ac.setSize(gear1, 100, 20)
   ac.setValue(gear1, 0)
   ac.setRange(gear1, 0, 100)  
   ac.setStep(gear1, 1)             

   gear2 = ac.addSpinner(appWindow, "2nd")
   ac.setPosition(gear2, 50, 250)
   ac.setSize(gear2, 100, 20)
   ac.setValue(gear2, 0)
   ac.setRange(gear2, 0, 100)  
   ac.setStep(gear2, 1)   

   gear3 = ac.addSpinner(appWindow, "3rd")
   ac.setPosition(gear3, 200, 130)
   ac.setSize(gear3, 100, 20)
   ac.setValue(gear3, 0)
   ac.setRange(gear3, 0, 100)  
   ac.setStep(gear3, 1)

   gear4 = ac.addSpinner(appWindow, "4th")
   ac.setPosition(gear4, 200, 170)
   ac.setSize(gear4, 100, 20)
   ac.setValue(gear4, 0)
   ac.setRange(gear4, 0, 100)  
   ac.setStep(gear4, 1)

   gear5 = ac.addSpinner(appWindow, "5th")
   ac.setPosition(gear5, 200, 210)
   ac.setSize(gear5, 100, 20)
   ac.setValue(gear5, 0)
   ac.setRange(gear5, 0, 100)  
   ac.setStep(gear5, 1)

   gear6 = ac.addSpinner(appWindow, "6th")
   ac.setPosition(gear6, 200, 250)
   ac.setSize(gear6, 100, 20)
   ac.setValue(gear6, 0)
   ac.setRange(gear6, 0, 100)  
   ac.setStep(gear6, 1)
   
   stock_boost = info.static.maxTurboBoost * 1.1 

   ac.ext_storeLua("banner_clicked", banner_clicked)

   isLoaded = True
      
   return "Road_dyno"
   
def acUpdate(deltaT):
   global gear_info, set_boost_by_gear, max_boost, turbo_count, stock_boost, banner, time0, time1
   global time2, time3, banner_clicked, banner_img

   if isLoaded == False:
      return  
   
   time0 = round(time.time() * 1000)

   if ((time1 + 33) < time0):
      #banner      
      banner_img += 1 
      if banner_img > 80:
         banner_img = 0
      if int(banner_img) < 10:
         ac.setBackgroundTexture(banner,"apps/python/D&O_Boost_by_gear/data/banner/a_0000{}.png".format(banner_img))
      else:
         ac.setBackgroundTexture(banner,"apps/python/D&O_Boost_by_gear/data/banner/a_000{}.png".format(banner_img))

      time1 = round(time.time() * 1000)

	  
   time2 = round(time.time() * 1000)

   if ((time3 + 400) < time0):
      if banner_clicked == 1:
         banner_clicked = 0
	  
      time3 = round(time.time() * 1000)
	  
   if ac.ext_loadLua("turbo_count") > 0: 
      #turbo_count = ac.ext_loadLua("turbo_count")   
      max_boost = (stock_boost * 14.5038) + 0.1
	  
   gear_info = int(ac.getCarState(0, acsys.CS.Gear))   

   ac.setRange(gearR, 0, max_boost) 
   ac.setRange(gearN, 0, max_boost) 
   ac.setRange(gear1, 0, max_boost) 
   ac.setRange(gear2, 0, max_boost)
   ac.setRange(gear3, 0, max_boost) 
   ac.setRange(gear4, 0, max_boost) 
   ac.setRange(gear5, 0, max_boost) 
   ac.setRange(gear6, 0, max_boost)    
   #c.setRange(rpm_bar2, 2000, old_rpm_limiter)

   #extraF = ac.ext_loadLua("extraF")
   if gear_info == 0:
     set_boost_by_gear = ac.getValue(gearR)
   elif gear_info == 1:
     set_boost_by_gear = ac.getValue(gearN)
   elif gear_info == 2:
     set_boost_by_gear = ac.getValue(gear1)
   elif gear_info == 3:
     set_boost_by_gear = ac.getValue(gear2)
   elif gear_info == 4:
     set_boost_by_gear = ac.getValue(gear3)
   elif gear_info == 5:
     set_boost_by_gear = ac.getValue(gear4)
   elif gear_info == 6:
     set_boost_by_gear = ac.getValue(gear5)
   elif gear_info > 6:
     set_boost_by_gear = ac.getValue(gear6)	 
	 
   ac.ext_storeLua("set_boost_by_gear", set_boost_by_gear)
   #ac.log(str(gear_info))


def fun_banner(*args):
   global banner_clicked, banner_status
   banner_clicked += 1   
   ac.ext_storeLua("banner_clicked", banner_clicked)   