#!/usr/bin/python
# The MIT License (MIT)
# Copyright (c) 2017 "Laxminarayan Kamath G A"<kamathln@gmail.com>

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.

from kivy.app import App
from kivy.uix.widget import Widget
from arrow import *
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from random import random
from kivy.clock import Clock

from kivy.lang import Builder

Builder.load_string('''
#:kivy 1.11.1
#:import kivy kivy

<Layout>:
    id: tt
    canvas:
        Color:
            rgb: 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size
        Color:
            rgb: 0, 0, 0
        Line: # Vertical line:
            points: [self.center_x, self.center_y-0.45*min(self.width,self.height), \
                self.center_x, self.center_y+0.45*min(self.width,self.height)] \
                if min(self.width,self.height)<mm(100) \
                else [self.center_x, self.center_y-mm(45), self.center_x, self.center_y+mm(45)]
            width: mm(0.25)
        Rectangle: # Horizontal line:
            pos: (self.center_x-0.45*min(self.width,self.height),self.center_y-mm(0.25)) \
                if min(self.width,self.height)<mm(100) \
                else (self.center_x-mm(45),self.center_y-mm(0.25))
            size: ((0.9*min(self.width,self.height)),mm(0.5)) \
                if min(self.width,self.height)<mm(100) else (mm(90),mm(0.5))

<Arrow>:
    pos: tt.pos

Layout1:  # I changed the name of your layout class, by convention, classes start with capital letters
    id:tt
    Arrow: # instance the arrow class
        pos: tt.pos

''')


class ArrowTest(App):
    def add_random_arrow(self,*args):
        newarrow = Arrow(
                         main_color=[0,0,0,1],
#                         outline_color=[random()/2.0,random()/2.0,random()/2.0,1],
                         o_x= 0.5*self.layout.width,
                         o_y= 0.5*self.layout.height,
                         #to_x=random()*self.layout.width,
                         #to_y=random()*self.layout.height,
                         angle=random()*360,
                         distance=150+random()*150,
                         fletching_radius=cm(0.1), # root-end
#                         distortions=[random() * -0.2, random() *0.3] if random()>0.5 else [],
                         head_angle=60
                        )
        self.layout.add_widget(newarrow)
        self.arrows.append(newarrow)
        #self.move_arrows()

#    def debug_dt(self,*args):
#        print ("DT avg ",self.dtsum/float(self.dtcounter)) 

    def build(self):
        self.arrows = []
        self.dtsum=0.0
        self.dtcounter=0.0
        self.layout = FloatLayout()
#        Clock.schedule_interval(self.move_arrows, 1/34.0)
        Clock.schedule_interval(self.add_random_arrow, 0.5)
        
        return self.layout

if __name__ == '__main__':
    ArrowTest().run()
