import plotly.plotly as py
import plotly.figure_factory as FF
from plotly.graph_objs import graph_objs
import plotly.graph_objs as go

import numpy as np
from scipy.spatial import Delaunay

import csv
import mesh

def render_pretty_map_portion(x_origin=-33,y_origin=149.5):
    # Merge datasets
    d,t = mesh.read_tiered_data(['data/Output_Top_Illawarra.csv','data/Output_IllawarraCM_SR.csv','data/Output_KATSF.csv'])

    #x_origin = -33
    #y_origin = 149.5

    x_extent = 1
    y_extent = 0.8

    #x,y,z = mesh.grid(d,t,(-33,-32),(149.5,150.3), num_xsteps=64,num_ysteps=64)
    x,y,z = mesh.grid(d,t,(x_origin,x_origin+x_extent),(y_origin,y_origin+y_extent), num_xsteps=64,num_ysteps=64)
    x,y = np.meshgrid(x,y)
    x = x.flatten()
    y = y.flatten()
    z = z.flatten()

    layout = go.Layout(
                        scene = dict(
                        xaxis = dict(showbackground=False, zeroline=False, showline=False, 
                                     showgrid=False, visible=False),
                        yaxis = dict(showbackground=False, zeroline=False, showline=False, 
                                     showgrid=False, visible=False),
                        zaxis = dict(showbackground=False, zeroline=False, showline=False, 
                                     showgrid=False)
                        )
                      )


    points2D = np.vstack([x,y]).T
    tri = Delaunay(points2D)
    simplices = tri.simplices

    fig1 = FF.create_trisurf(x=x, y=y, z=z, simplices=simplices, title="Illawara_short", 
                             aspectratio=dict(x=1, y=1, z=1))
    #print(dir(fig1))
    #print(fig1.data[1])
    fig1.data[0]['opacity']=0.8
    fig1.data[1]['mode']="none"
    #fig1.data[1]['mode']="points"
    fig1.layout=layout
    return py.iplot(fig1, filename="")
    

"""
Explanation: Demonstration of Map-M8 selective block loading
The industry standard for terrain formats are excel spreadsheets, CSV files and DXF files (A file format used for computer aided drawing). Insert joke about how are a house and rocks have been different for a while
There is a lot of tooling built around the current file types - but they're no good for quick visualisation
on differing scales or of very large datasets. Sharing data means sending large files, and merging data is a nightmare, especially when they're overlapping.
In this demo, we combine three datasets of varying resolution into a single dataset, and then query it for a specific area. For any given area, the area of best resolution is used.
Setting up for 3D visualisation
End of explanation
"""


#render_pretty_map_portion(-33, 149.5)
render_pretty_map_portion()

"""
Explanation: Query of specific areas
Area covered: Lat: -33.0 to -32.0, Long: 149.5 to 150.3
End of explanation
"""


render_pretty_map_portion(-34, 150)

"""
Explanation: Shifting views
Area covered: Lat: -34 to -33, Long: 150 to 150.8
End of explanation
"""


render_pretty_map_portion(-34.5, 150)

"""
Explanation: Area covered: Lat: -34.5 to -33.5, Long: 150 to 150.8
End of explanation
"""


