import io
import os

import numpy as np

from jinja2 import Template
from bokeh.resources import CDN

from . import plotting
from . import db
from . import templates
from .utils import SdfError
from . import config as cfg


def www_all(results,update=False):
    """Generate www material."""

    print(" Web")

    # see whether index.html needs updating (unless update enforced)
    index_file = results[0].path+'/index.html'
    
    if os.path.exists(index_file):
        pkltime = []
        for r in results:
            pkltime.append( os.path.getmtime(r.pickle) )

        if os.path.getmtime(index_file) > np.max(pkltime):
            if not update:
                print("   no update needed")
                return
        print("   updating")
    else:
        print("   generating")

    index(results,file=index_file)


def index(results,tab_order=None,file='index.html'):
    """Make index.html - landing page with an SED and other info."""

    script,div = plotting.sed_components(results,tab_order=tab_order)

    info = db.sdb_info(results[0].id)
    if info is not None:
        sdbid,main_id,xids,ra,dec,dist = info
    else:
        sdbid,main_id,xids,ra,dec,dist = None,None,None,None,None

    template = Template(templates.sed)
    bokeh_js = CDN.render_js()
    bokeh_css = CDN.render_css()
    html = template.render(bokeh_js=bokeh_js,
                           bokeh_css=bokeh_css,
                           css=templates.css,
                           plot_script=script,
                           plot_div=div,
                           sdbid=sdbid,main_id=main_id,
                           ra=ra/15.0,dec=dec,dist=dist
                           )

    with io.open(file, mode='w', encoding='utf-8') as f:
        f.write(html)


@@ -850,23 +850,8 @@
 lts,
-tab_order=None,
 file
@@ -988,215 +988,9 @@
 ults
-,tab_order=tab_order)%0A%0A    info = db.sdb_info(results%5B0%5D.id)%0A    if info is not None:%0A        sdbid,main_id,xids,ra,dec,dist = info%0A    else:%0A        sdbid,main_id,xids,ra,dec,dist = None,None,None,None,None
+)
 %0A%0A  
@@ -1021,19 +1021,21 @@
 mplates.
-sed
+index
 )%0A    bo
@@ -1351,92 +1351,395 @@
     
-sdbid=sdbid,main_id=main_id,%0A                           ra=ra/15.0,dec=dec,dist=dist
+phot_file=os.path.basename(results%5B0%5D.rawphot),%0A                           main_id=results%5B0%5D.obs_keywords%5B'main_id'%5D,%0A                           spty=results%5B0%5D.obs_keywords%5B'sp_type'%5D,%0A                           ra=results%5B0%5D.obs_keywords%5B'raj2000'%5D,%0A                           dec=results%5B0%5D.obs_keywords%5B'dej2000'%5D,%0A                           plx=results%5B0%5D.obs_keywords%5B'plx_value'%5D
 %0A   

use info from phot file rather than db