from shape import Shape2D
from pyglet.gl import *
from pyglet.graphics import TextureGroup
class Button(object):
    def __init__(self,x,y,width,height,text,textColor,quadColor):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.textColor = textColor
        self.quadColor = quadColor
        self.function = None
        self.text = text
        self.label = pyglet.text.Label(text = text, font_name='Arial', font_size = 3 * height / 8,
            x= x + width//2, y=y + height//2, anchor_x='center', anchor_y='center',
            color=textColor)
        self.quad = pyglet.graphics.vertex_list(4,
                ('v2i', Shape2D.quad_vertices(x,y,width,height)),
                ('c3B',quadColor*4))

    def draw(self):
        self.quad.draw(pyglet.gl.GL_QUADS)
        self.label.draw()

    def on_click(self,x,y):
        if self._checkMouse(x,y):
            func,args = self.function
            func(*args)

    def on_mouse(self,x,y,textColor,quadColor):
        if self._checkMouse(x,y):
            self.label.color = textColor
            self.quad.colors = quadColor*4
        else:
            self.label.color = self.textColor
            self.quad.colors = self.quadColor*4

    def on_resize(self,x,y,width,height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.label.x = x + width//2
        self.label.y = y + height//2
        self.label.font_size = 3 * height / 8

        self.quad.vertices = Shape2D.quad_vertices(x,y,width,height)

    def changeFunc(self,func,*args):
        self.function = (func, args)

    def _checkMouse(self,x,y):
        return x > self.x and x < self.x+self.width and y > self.y and y < self.y+self.height

    def changeText(self,text):
        self.label.text = text

class OnOffButton():
    def __init__(self, x, y, width, height, LeftToRightFunc, RightToLeftFunc,  textColor = (255,0,0,255),
                    quadColor = (0,0,0), slideQuadColor = (64,64,64),state = False, leftText = "OFF", rightText = "ON"):
        ##@param state Boolean, False represent left and True represents Right
        # @param onFunc the tuple of (func, *args)
        # @param offFunc the tuple of (func, *args)
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.state = state
        self.LeftToRightFunc = LeftToRightFunc
        self.RightToLeftFunc = RightToLeftFunc
        self.leftText = pyglet.text.Label(text = leftText, font_name='Arial', font_size = 3 * height // 7,
            x = x - width//2 -10, y=y, anchor_x='right', anchor_y='center',
            color = textColor)
        self.rightText = pyglet.text.Label(text = rightText, font_name='Arial', font_size = 3 * height // 7,
            x= x + 10 + width//2, y = y, anchor_x='left', anchor_y='center',
            color = textColor)
        self.quad = pyglet.graphics.vertex_list(4,
                ('v2i', Shape2D.quad_vertices(x - width //2 ,y - height //2,width,height)),
                ('c3B',quadColor*4))
        self.slideQuad = pyglet.graphics.vertex_list(4,
                ('v2i', Shape2D.quad_vertices(x - 3 * width//8,y - 5 * height //8,width//4, 5 * height// 4)),
                ('c3B',slideQuadColor*4))

    def draw(self):
        self.leftText.draw()
        self.rightText.draw()
        self.quad.draw(pyglet.gl.GL_QUADS)
        self.slideQuad.draw(pyglet.gl.GL_QUADS)

    def on_resize(self,x,y,width,height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        #leftText
        self.leftText.x = x - width//2 -10
        self.leftText.y = y
        self.leftText.font_size = 3 * height // 7
        #rightText
        self.rightText.x = x + width//2 +10
        self.rightText.y = y
        self.rightText.font_size = 3 * height // 7
        #quads
        self.quad.vertices = Shape2D.quad_vertices(x - width //2 ,y - height //2,width,height)
        #slideQuad
        self.changeState(self.state)
        
    def on_click(self,x,y):
        if self._checkMouseOn(x,y):
            if self.state:
                func,args = self.RightToLeftFunc
            else:
                func,args = self.LeftToRightFunc
            func(*args)
            self.changeState(not self.state)
            return True
        return False

    def changeState(self, state):
        if state:
            #change to True(Right)
            self.slideQuad.vertices = Shape2D.quad_vertices(self.x + self.width//8, self.y - 5 * self.height //8, self.width//4, 5 * self.height// 4)
        else:
            #change to Left(True)
            self.slideQuad.vertices = Shape2D.quad_vertices(self.x - 3 * self.width//8, self.y - 5 * self.height //8, self.width//4, 5 * self.height// 4)
        self.state = state

    def _checkMouseOn(self,x,y):
        return self.x - self.width // 2  < x and x < self.x + self.width // 2 and self.y - self.height //2 < y  and self.y < self.y + self.height //2