import dash_core_components as dcc

import json

from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html

import sd_material_ui as smui

from app import app

from datastore import datastore

ds = datastore()

# X-ray knots summary
summary_data = ds.get_summary_data()
knots_summary_values = [
    summary_data['total_knots_with_offsets'],
    summary_data['total_knots_without_offsets'],
]

# add the number of knots without any radio counterparts
knots_summary_values.append(
    summary_data['total_resolved_xray_knots']
    - knots_summary_values[1]
    - knots_summary_values[0]
)
knots_summary_data = {
    'values': knots_summary_values,
    'labels': [
        'Knots with offsets',
        'Knots without offsets',
        'Knots without radio counterparts'
    ],
    'marker': {
        'colors': ['rgb(31, 119, 180)', 'rgb(255, 127, 14)', 'rgb(44, 160, 44)']
    },
    'type': 'pie',
    'hoverinfo': 'label+percent',
    'hole': 0.4
}

# HS summary
hs_summary_values = [
    summary_data['total_HS_with_offsets'],
    summary_data['total_HS_without_offsets']
]

# add the ambiguous ones
hs_summary_values.append(
    summary_data['total_resolved_xray_HS']
    - summary_data['total_HS_with_offsets']
    - summary_data['total_HS_without_offsets']
)

hs_summary_data = {
    'values': hs_summary_values,
    'labels': [
        'HS with offsets',
        'HS without offsets',
        'HS with ambiguous radio features'
    ],
    'marker': {
        'colors': ['rgb(31, 119, 180)', 'rgb(255, 127, 14)', 'rgb(44, 160, 44)']
    },
    'type': 'pie',
    'hoverinfo': 'label+percent',
    'hole': 0.4
}

layout = html.Div(
    [
        html.Div(
            [
                smui.Paper(
                    [
                        dcc.Graph(
                            id='knots-summary-pie',
                            figure={
                                'data': [knots_summary_data],
                                'layout':{
                                    'title': 'X-ray Knots',
                                    'legend': {
                                        'orientation': 'h'
                                    },
                                    # 'title': 'X-rays',
                                    "annotations":
                                    [
                                        {
                                            "font": {
                                                "size": 20
                                            },
                                            "showarrow": False,
                                            "text": "Knots",
                                            "x": 0.5,
                                            "y": 0.5
                                        }
                                    ]
                                }
                            }
                        )
                    ]
                ),
                smui.Paper(
                    [
                        dcc.Graph(
                            id='hotspots-summary-pie',
                            figure={
                                'data': [hs_summary_data],
                                'layout':{
                                    'title': 'X-ray Hotspots',
                                    'legend': {
                                        'orientation': 'h'
                                    },
                                    "annotations":
                                    [
                                        {
                                            "font": {
                                                "size": 20
                                            },
                                            "showarrow": False,
                                            "text": "HS",
                                            "x": 0.5,
                                            "y": 0.5
                                        }
                                    ]
                                }
                            }
                        )
                    ]
                ),
                smui.Paper(
                    [
                        dcc.Graph(
                            id='offset-type-pie',
                            figure={
                                'data': [
                                    {
                                        'values': [
                                            summary_data['xray_first_offsets'],
                                            summary_data['radio_first_offsets'],
                                            summary_data['total_knots_without_offsets']
                                            +
                                            summary_data['total_HS_without_offsets']
                                        ],
                                        'labels':[
                                            'X-ray upstream offset',
                                            'X-ray downstream offset',
                                            'No offset'
                                        ],
                                        'type': 'pie',
                                        'hoverinfo': 'label+percent',
                                        'hole': 0.5
                                    }
                                ],
                                'layout':{
                                    'title':'All features',
                                    'legend': {
                                        'orientation': 'h'
                                    },
                                    "annotations":
                                    [
                                        {
                                            "font": {
                                                "size": 20
                                            },
                                            "showarrow": False,
                                            "text": "knots+HS",
                                            "x": 0.5,
                                            "y": 0.5
                                        }
                                    ]
                                }
                            }
                        )
                    ]
                )
            ],
            className='flex-box',
            style={'clear':'both'}
        ),
        #html.Div(
        #    [
                smui.Paper(
                    [
                        'There are {0}/{1} sources where the X-ray emission from the jet terminates abruptly whenever there is a bend in the radio jet.'
                        .format(
                            summary_data['total_xray_jet_terminates_at_a_bend'],
                            summary_data['total_jets']
                        )


                    ],style={'height':'50px','line-height':'50px','text-align':'center'}
                )
            #], className='flex-box'
        #)
    ]
)
