# -*- coding: utf-8 -*-
"""
Created on Fri Nov 23 14:39:35 2018

@author: Martin
"""
from math import sqrt, atan2


def test():
 
    inputFileName=input('nom du fichier contenant les listes de points :')
    inputFile=open(inputFileName,'r')
    
    ligneBriseeList=readFileofPoints(inputFile)
    inputFile.close()
    

    listeMur=[]
    for ligneBrisee in ligneBriseeList :  
        
        listeMur=listeMur+listPoints2listWalls(ligneBrisee)
        
    
    exportlofWalls(listeMur)
    
def listPoints2listWalls(listofPoints):
    numberofPoints=len(listofPoints)
#    numberofWalls=numberofPoints-1
    
    listofWalls=[]
    firstPoint=listofPoints[0]
    
    for k in range (1 , numberofPoints):
        secondPoint=listofPoints[k]
        mid=milieuSegment(firstPoint,secondPoint)
        length=longueurSegment(firstPoint,secondPoint)
        angle=angleSegment(firstPoint,secondPoint)
        
        listofWalls.append( [mid,length,angle])
        firstPoint=secondPoint
        
    return listofWalls

def exportlofWalls(listofWalls):
    worldwide=150
    worldheight=200
    
    filename = input('nom de fichier')
    myfile=open(filename+'.playground','w')
    
    # writing header
    myfile.write('<!DOCTYPE aseba-playground>\n<aseba-playground> \n\t<author name="Martin Canals" /> \n\t <thanks name="Tungsteno Design" />\n')
    myfile.write('\t<description lang="en">A ground board for Thymio Challenge Pack.</description>\n')
    myfile.write('\t<description lang="fr">Un environnement pour la voiture autonome.</description>\n')
    myfile.write('\n')
    #defining colors
    myfile.write('\t<color name="wall" r="1.0" g="1.0" b="1.0" /> \n')
    myfile.write('\t<color name="white" r="1.0" g="1.0" b="1.0" /> \n')
    myfile.write('\n')
    #defining world size
    myfile.write('\t<world w="'+ str(worldwide)+'" h="'+str(worldheight)+'" color="white" />\n')
    myfile.write('\n')
    
    #writing walls
    for wall in  listofWalls:
        writeWall(myfile,wall)
    # position initiale et robot
    myfile.write('\t<robot type="thymio2" x="70" y="15" port="33333" name="Thymio" /> \n')
    myfile.write('\n')
    
    #writing command for IP bridge
    myfile.write('\t<process command=":asebahttp --autorestart tcp:localhost;33333" /> \n')
    myfile.write('\n')
    
    myfile.write('</aseba-playground>')
    myfile.close()
    
def writeWall(myfile, wall) :
    
    xpos,ypos=wall[0]
    size=wall[1]
    angle=wall[2]
    strX=str(xpos)
    strY=str(ypos)
    strangle=str(angle)
    strsize=str(size)
    
    command='\t<wall x="'+strX+'" y= "'+ strY+'" l1="'+strsize+'" l2="5" h="10" mass="-1" color="wall" angle="'+strangle+'" />\n'
    myfile.write(command)

def readFileofPoints(myfile):
  
    listofLines=[]
    
    for line in myfile:
        line = line.rstrip('\n')
        if line=='begin' :
            lineofPoints=[]
        elif line=='end' :
            listofLines.append(lineofPoints)
        else:
            textcoord=line.split('\t')
            x=float(textcoord[0])
            y=float(textcoord[1])
            lineofPoints.append([x,y])
            
    return listofLines
            
            
    
        
      
def milieuSegment(P1,P2):
    return (P1[0]+P2[0])/2 ,  (P1[1]+P2[1])/2

def longueurSegment(P1,P2):
    return sqrt( (P1[1]-P2[1])**2+(P1[0]-P2[0])**2)

def angleSegment(P1,P2) :
    return atan2(P2[1]-P1[1], P2[0]-P1[0])



 