import os
import sys
import time
import tkinter

import django
from PIL import Image, ImageTk

sys.path.append('..')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'futplus.settings')
django.setup()

from sbc.visual_buttons import Uniqe_XBOXS_Buttons

import pygetwindow as gw

wins = gw.getWindowsWithTitle('Xbox')

xbox_win = None
for win in wins:
    if win.title == 'Xbox':
        xbox_win = win
        xbox_win.maximize()

if xbox_win is None:
    raise Exception('please open xbox app first')

master = tkinter.Tk()
master.attributes("-topmost", True)
master.title("dualshock control")
master.geometry("550x347")
img1 = tkinter.PhotoImage(file="fromtop.png")
img2 = tkinter.PhotoImage(file="fromfront.png")
master.wm_attributes('-transparentcolor', '#fff568')
ps4_buttons = Uniqe_XBOXS_Buttons()
ps4_buttons.reset_to_default()
label = tkinter.Label(
    master,
    image=img1
)
label.place(x=0, y=100)

label = tkinter.Label(
    master,
    image=img2
)
label.place(x=0, y=0)


def sharebutt():
    print("nadarim")


shareimage = ImageTk.PhotoImage(Image.open('share.png'))
sharebtn = tkinter.Button(master, image=shareimage, command=sharebutt, borderwidth=0, highlightthickness=0)
sharebtn.place(x=153, y=139)


def l2butt():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.l2()


l2image = ImageTk.PhotoImage(Image.open('l2.png'))
l2btn = tkinter.Button(master, image=l2image, command=l2butt, borderwidth=0, highlightthickness=0)
l2btn.place(x=71, y=2)


def l1butt():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.l1()


l1image = ImageTk.PhotoImage(Image.open('l1.png'))
l1btn = tkinter.Button(master, image=l1image, command=l1butt, borderwidth=0, highlightthickness=0)
l1btn.place(x=74, y=70)


def optionbutt():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.option()


optionimage = ImageTk.PhotoImage(Image.open('option.png'))
optionbtn = tkinter.Button(master, image=optionimage, command=optionbutt, borderwidth=0, highlightthickness=0)
optionbtn.place(x=381, y=139)


def r2butt():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.r2()


r2image = ImageTk.PhotoImage(Image.open('r2.png'))
r2btn = tkinter.Button(master, image=r2image, command=r2butt, borderwidth=0, highlightthickness=0)
r2btn.place(x=406, y=2)


def r1butt():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.r1()


r1image = ImageTk.PhotoImage(Image.open('r1.png'))
r1btn = tkinter.Button(master, image=r1image, command=r1butt, borderwidth=0, highlightthickness=0)
r1btn.place(x=407, y=70)


def upbutt():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.up()


upimage = ImageTk.PhotoImage(Image.open('top.png'))
upbtn = tkinter.Button(master, image=upimage, command=upbutt, borderwidth=0, highlightthickness=0)
upbtn.place(x=90, y=164)


def botbutt():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.down()


botimage = ImageTk.PhotoImage(Image.open('bot.png'))
botbtn = tkinter.Button(master, image=botimage, command=botbutt, borderwidth=0, highlightthickness=0)
botbtn.place(x=89, y=225)


def leftbutt():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.left()


leftimage = ImageTk.PhotoImage(Image.open('left.png'))
leftbtn = tkinter.Button(master, image=leftimage, command=leftbutt, borderwidth=0, highlightthickness=0)
leftbtn.place(x=58, y=195)


def rightbutt():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.right()


rightimage = ImageTk.PhotoImage(Image.open('right.png'))
rightbtn = tkinter.Button(master, image=rightimage, command=rightbutt, borderwidth=0, highlightthickness=0)
rightbtn.place(x=117, y=196)


def trianglebutt():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.triangle()


triangleimage = ImageTk.PhotoImage(Image.open('mangle.png'))
trianglebtn = tkinter.Button(master, image=triangleimage, command=trianglebutt, borderwidth=0, highlightthickness=0)
trianglebtn.place(x=434, y=151)


def crossbutt():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.cross()


crossimage = ImageTk.PhotoImage(Image.open('cross.png'))
crossbtn = tkinter.Button(master, image=crossimage, command=crossbutt, borderwidth=0, highlightthickness=0)
crossbtn.place(x=434, y=233)


def squarebutt():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.square()


squareimage = ImageTk.PhotoImage(Image.open('squre.png'))
squarebtn = tkinter.Button(master, image=squareimage, command=squarebutt, borderwidth=0, highlightthickness=0)
squarebtn.place(x=393, y=191)


def circlebutt():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.circle()


crcleimage = ImageTk.PhotoImage(Image.open('circle.png'))
circlebtn = tkinter.Button(master, image=crcleimage, command=circlebutt, borderwidth=0, highlightthickness=0)
circlebtn.place(x=474, y=193)


def psbutt():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.ps()


psimage = ImageTk.PhotoImage(Image.open('ps.png'))
psbtn = tkinter.Button(master, image=psimage, command=psbutt, borderwidth=0, highlightthickness=0)
psbtn.place(x=264, y=274)


def l3butt():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.l3()


l3image = ImageTk.PhotoImage(Image.open('l3.png'))
l3btn = tkinter.Button(master, image=l3image, command=l3butt, borderwidth=0, highlightthickness=0)
l3btn.place(x=178, y=273)


def ls1():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.press_and_release_left_joystick(0, 32767)


lj1image = ImageTk.PhotoImage(Image.open('lj1.png'))
lshock1btn = tkinter.Button(master, image=lj1image, command=ls1, borderwidth=0, highlightthickness=0)
lshock1btn.place(x=183, y=254)


def ls2():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.press_and_release_left_joystick(10000, 32767)


lj2image = ImageTk.PhotoImage(Image.open('lj2.png'))
shock2btn = tkinter.Button(master, image=lj2image, command=ls2, borderwidth=0, highlightthickness=0)
shock2btn.place(x=196, y=257)


def ls3():
    # todo : fix all joystick from 0 - 255 to -32767 - 32767
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.press_and_release_left_joystick(32767, 10000)


lj3image = ImageTk.PhotoImage(Image.open('lj3.png'))
lshock3btn = tkinter.Button(master, image=lj3image, command=ls3, borderwidth=0, highlightthickness=0)
lshock3btn.place(x=206, y=270)


def ls4():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.press_and_release_left_joystick(32767, 0)


lj4image = ImageTk.PhotoImage(Image.open('lj4.png'))
shock4btn = tkinter.Button(master, image=lj4image, command=ls4, borderwidth=0, highlightthickness=0)
shock4btn.place(x=207, y=281)


def ls5():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.press_and_release_left_joystick(32767, -10000)


lj5image = ImageTk.PhotoImage(Image.open('lj5.png'))
lshock5btn = tkinter.Button(master, image=lj5image, command=ls5, borderwidth=0, highlightthickness=0)
lshock5btn.place(x=205, y=292)


def ls6():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.press_and_release_left_joystick(10000, -32767)


lj6image = ImageTk.PhotoImage(Image.open('lj6.png'))
lshock6btn = tkinter.Button(master, image=lj6image, command=ls6, borderwidth=0, highlightthickness=0)
lshock6btn.place(x=195, y=305)


def ls7():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.press_and_release_left_joystick(0, -32767)


lj7image = ImageTk.PhotoImage(Image.open('lj7.png'))
lshock7btn = tkinter.Button(master, image=lj7image, command=ls7, borderwidth=0, highlightthickness=0)
lshock7btn.place(x=183, y=307)


def ls8():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.press_and_release_left_joystick(-10000, -32767)


lj8mage = ImageTk.PhotoImage(Image.open('lj8.png'))
lshock8btn = tkinter.Button(master, image=lj8mage, command=ls8, borderwidth=0, highlightthickness=0)
lshock8btn.place(x=169, y=305)


def ls9():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.press_and_release_left_joystick(-32767, -10000)


lj9image = ImageTk.PhotoImage(Image.open('lj9.png'))
lshock9btn = tkinter.Button(master, image=lj9image, command=ls9, borderwidth=0, highlightthickness=0)
lshock9btn.place(x=157, y=294)


def ls10():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.press_and_release_left_joystick(-32767, 0)


lj10image = ImageTk.PhotoImage(Image.open('lj10.png'))
lshock10btn = tkinter.Button(master, image=lj10image, command=ls10, borderwidth=0, highlightthickness=0)
lshock10btn.place(x=154, y=281)


def ls11():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.press_and_release_left_joystick(-32767, 10000)


lj11mage = ImageTk.PhotoImage(Image.open('lj11.png'))
lshock11btn = tkinter.Button(master, image=lj11mage, command=ls11, borderwidth=0, highlightthickness=0)
lshock11btn.place(x=157, y=268)


def ls12():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.press_and_release_left_joystick(-10000, 32767)


lj12mage = ImageTk.PhotoImage(Image.open('lj12.png'))
lshock12btn = tkinter.Button(master, image=lj12mage, command=ls12, borderwidth=0, highlightthickness=0)
lshock12btn.place(x=167, y=256)


def r3butt():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.r3()


r3image = ImageTk.PhotoImage(Image.open('r3.png'))
r3btn = tkinter.Button(master, image=r3image, command=r3butt, borderwidth=0, highlightthickness=0)
r3btn.place(x=354, y=270)


def rs1():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.press_and_release_right_joystick(0, 32767)


rj1image = ImageTk.PhotoImage(Image.open('rj1.png'))
rshock1btn = tkinter.Button(master, image=rj1image, command=rs1, borderwidth=0, highlightthickness=0)
rshock1btn.place(x=359, y=256)


def rs2():
    ps4_buttons.press_and_release_right_joystick(150, 90)


rj2image = ImageTk.PhotoImage(Image.open('rj2.png'))
rshock2btn = tkinter.Button(master, image=rj2image, command=rs2, borderwidth=0, highlightthickness=0)
rshock2btn.place(x=378, y=260)


def rs3():
    ps4_buttons.press_and_release_right_joystick(230, 150)


rj3image = ImageTk.PhotoImage(Image.open('rj3.png'))
rshock3btn = tkinter.Button(master, image=rj3image, command=rs3, borderwidth=0, highlightthickness=0)
rshock3btn.place(x=385, y=270)


def rs4():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.press_and_release_right_joystick(32767, 0)


rj4image = ImageTk.PhotoImage(Image.open('rj4.png'))
rshock4btn = tkinter.Button(master, image=rj4image, command=rs4, borderwidth=0, highlightthickness=0)
rshock4btn.place(x=385, y=282)


def rs5():
    ps4_buttons.press_and_release_right_joystick(220, 100)


rj5image = ImageTk.PhotoImage(Image.open('rj5.png'))
rshock5btn = tkinter.Button(master, image=rj5image, command=rs5, borderwidth=0, highlightthickness=0)
rshock5btn.place(x=384, y=295)


def rs6():
    ps4_buttons.press_and_release_right_joystick(140, 20)


rj6image = ImageTk.PhotoImage(Image.open('rj6.png'))
rshock6btn = tkinter.Button(master, image=rj6image, command=rs6, borderwidth=0, highlightthickness=0)
rshock6btn.place(x=378, y=304)


def rs7():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.press_and_release_right_joystick(0, -32767)


rj7image = ImageTk.PhotoImage(Image.open('rj7.png'))
rshock7btn = tkinter.Button(master, image=rj7image, command=rs7, borderwidth=0, highlightthickness=0)
rshock7btn.place(x=363, y=306)


def rs8():
    ps4_buttons.press_and_release_right_joystick(100, 20)


rj8image = ImageTk.PhotoImage(Image.open('rj8.png'))
rshock8btn = tkinter.Button(master, image=rj8image, command=rs8, borderwidth=0, highlightthickness=0)
rshock8btn.place(x=347, y=306)


def rs9():
    ps4_buttons.press_and_release_right_joystick(40, 90)


rj9image = ImageTk.PhotoImage(Image.open('rj9.png'))
rshock9btn = tkinter.Button(master, image=rj9image, command=rs9, borderwidth=0, highlightthickness=0)
rshock9btn.place(x=336, y=297)


def rs10():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.press_and_release_right_joystick(-32767, 0)


rj10image = ImageTk.PhotoImage(Image.open('rj10.png'))
rshock10btn = tkinter.Button(master, image=rj10image, command=rs10, borderwidth=0, highlightthickness=0)
rshock10btn.place(x=334, y=282)


def rs11():
    ps4_buttons.press_and_release_right_joystick(40, 180)


rj11image = ImageTk.PhotoImage(Image.open('rj11.png'))
rshock11btn = tkinter.Button(master, image=rj11image, command=rs11, borderwidth=0, highlightthickness=0)
rshock11btn.place(x=335, y=268)


def rs12():
    ps4_buttons.press_and_release_right_joystick(100, 250)


rj12image = ImageTk.PhotoImage(Image.open('rj12.png'))
rshock12btn = tkinter.Button(master, image=rj12image, command=rs12, borderwidth=0, highlightthickness=0)
rshock12btn.place(x=343, y=257)


# def qsbutt():
#     print("felan nadarim")


def reset_default_action():
    ps4_buttons.reset_to_default()
    for win in wins:
        if win.title == 'Xbox':
            xbox_win = win
            xbox_win.maximize()


# quicksellimage = ImageTk.PhotoImage(Image.open('quicksell.png'))
# quecksellbtn = tkinter.Button(master, image=quicksellimage, command=qsbutt, borderwidth=0, highlightthickness=0)
# quecksellbtn.place(x=195, y=160)

reset_default_image = ImageTk.PhotoImage(Image.open('reset_btn.png'))
reset_default_btn = tkinter.Button(master, image=reset_default_image, command=reset_default_action, borderwidth=0,
                                   highlightthickness=0)
reset_default_btn.place(x=30, y=300)


def quick_sell():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.press_left_joystick(-10000, 32767)
    time.sleep(.5)
    ps4_buttons.cross(press_time=1)
    time.sleep(.5)
    ps4_buttons.release_left_joystick()


quick_sell_image = ImageTk.PhotoImage(Image.open('quick_sell.png'))
quick_sell_btn = tkinter.Button(
    master, image=quick_sell_image, command=quick_sell, borderwidth=0, highlightthickness=0)
quick_sell_btn.place(x=200, y=130)


def apply_recommended_consumable():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.press_left_joystick(32767, -10000)
    time.sleep(.5)
    ps4_buttons.cross(press_time=1)
    time.sleep(.5)
    ps4_buttons.release_left_joystick()


apply_rec_consume_image = ImageTk.PhotoImage(Image.open('apply_rec_consume.png'))
apply_rec_consume_btn = tkinter.Button(
    master, image=apply_rec_consume_image, command=apply_recommended_consumable, borderwidth=0, highlightthickness=0)
apply_rec_consume_btn.place(x=200, y=150)


def add_or_switch():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.ps()
    time.sleep(2)
    for iie in range(7):
        ps4_buttons.right()
        time.sleep(.5)
    ps4_buttons.down()
    time.sleep(.5)
    ps4_buttons.cross()


add_or_switch_image = ImageTk.PhotoImage(Image.open('add_or_switch.png'))
add_or_switch_btn = tkinter.Button(
    master, image=add_or_switch_image, command=add_or_switch, borderwidth=0, highlightthickness=0)
add_or_switch_btn.place(x=200, y=170)


def restart_console():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.ps(press_time=1)
    time.sleep(1)
    for iie in range(4):
        ps4_buttons.right()
        time.sleep(.5)
    ps4_buttons.cross()
    time.sleep(1)
    ps4_buttons.right()

# restart_console_image = ImageTk.PhotoImage(Image.open('restart_console.png'))
# restart_console_btn = tkinter.Button(
#     master, image=restart_console_image, command=restart_console, borderwidth=0, highlightthickness=0)
# restart_console_btn.place(x=200, y=190)

def quit_game():
    xbox_win.activate()
    time.sleep(.01)
    ps4_buttons.ps()
    time.sleep(4)
    ps4_buttons.cross()
    time.sleep(10)
    ps4_buttons.option()
    time.sleep(4)
    ps4_buttons.r2()
    time.sleep(4)
    ps4_buttons.cross()

quit_game_image = ImageTk.PhotoImage(Image.open('quit_game.png'))
quit_game_btn = tkinter.Button(
    master, image=quit_game_image, command=quit_game, borderwidth=0, highlightthickness=0)
quit_game_btn.place(x=200, y=190)

def remove_accounts_menu():
    xbox_win.activate()
    time.sleep(.1)
    ps4_buttons.ps()
    time.sleep(1)
    ps4_buttons.cross()
    time.sleep(3)
    for nnn in range(10):
        ps4_buttons.up()
        time.sleep(.5)
    for nnn in range(5):
        time.sleep(.5)
        ps4_buttons.right()
    ps4_buttons.cross(sleep_after=2)
    ps4_buttons.down(sleep_after=1)
    ps4_buttons.cross(sleep_after=2)
    for mnnt in range(5):
        ps4_buttons.down()
        ps4_buttons.right()
        time.sleep(.5)
    ps4_buttons.cross(sleep_after=5)

remove_accounts_menu_image = ImageTk.PhotoImage(Image.open('remove_accounts_menu.png'))
remove_accounts_menu_btn = tkinter.Button(
    master, image=remove_accounts_menu_image, command=remove_accounts_menu, borderwidth=0, highlightthickness=0)
remove_accounts_menu_btn.place(x=200, y=210)


master.mainloop()

# need tkinter lib
# python -m PyInstaller --onefile --add-binary="ViGEmClient.dll;." .\gamepad\gampad_xbox.py
