# Python imports
from datetime import datetime
from datetime import datetime, date, timedelta
import json, copy, ast

# Django imports
from django.http import JsonResponse
from django.shortcuts import render
from django.template.loader import render_to_string

# LeakLess Monitor imports
from Applications.Measure.Plot.Annotations.views import annotations_get_icon_list
from Applications.Measure.models import LldevicePdlagMeasure, LldevicePdlagMeasureArchive, Lldeviceannotations, LldevicePdlagStatus
from Applications.Measure.views import *
from Applications.Users.views import user_get_device
from Applications.Devices.views import devices_return_channel_labels, devices_return_identifier, devices_return_by_id

def plot_generate(request):
    if request.user.is_authenticated():
        device_id           = request.GET.get('device_id')
        device = devices_return_by_id(device_id)

        if device is None:
            return HttpResponse('Invalid device')

        annotation_icon_list=annotations_get_icon_list()
        user_device_type=user_get_device(request)

        data={}
        if device.loc_type == 'PDL-AG':
            datasets = plot_generate_pdlag(request)
        elif device.loc_type == 'MAG8000':
            datasets = plot_generate_mag8000(request)
        else:
            datasets = plot_generate_pdlag(request)

        data['datasets']    = datasets
        title_string_top    = "Identifikator: "+device.rem_identifier
        title_string_bottom = "ID: "+device_id
        data['page_render'] = render_to_string('plot_measurement.html', {   'user_object'          : request.user,
                                                                            'user_device_type'     : user_device_type,
                                                                            'datasets'             : datasets,
                                                                            'title_string_top'     : title_string_top,
                                                                            'title_string_bottom'  : title_string_bottom,
                                                                            'annotation_icon_list' : annotation_icon_list,
                                                                            'lang'                 : user_get_language(request)})
        return JsonResponse(data)
    else:
        return HttpResponse('Invalid user')


def plot_generate_pdlag(request):
    device_id           = request.GET.get('device_id')
    plot_start_date     = datetime.strptime(request.GET.get('startDateInput'), "%Y-%m-%d %H:%M:%S")
    plot_end_date       = datetime.strptime(request.GET.get('endDateInput'), "%Y-%m-%d %H:%M:%S")

    checkbox_pressure   = request.GET.get('pressure')
    checkbox_flow       = request.GET.get('flow')
    checkbox_total_flow = request.GET.get('total_flow')
    checkbox_digital    = request.GET.get('digital')

    device = devices_return_by_id(device_id)
    pressure_unit       = device.get_pressure_recalculate_unit()
    pressure_factor     = device.get_pressure_factor()
    flow_unit           = device.get_flow_recalculate_unit()
    flow_factor         = device.get_flow_factor()
    total_flow_unit     = device.get_total_flow_recalculate_unit()
    total_flow_factor   = device.get_total_flow_factor()

    measurement_list = measure_get_measure_data(device_id, plot_start_date, plot_end_date)

    annotation_list = Lldeviceannotations.objects.filter(   timestamp__gte=plot_start_date,
                                                            timestamp__lte=plot_end_date,
                                                            lldevicelist=device_id).order_by('timestamp')

    channel_labels = devices_return_channel_labels(device_id)

    pressure_annotation_array = []
    flow_annotation_array = []
    total_flow_annotation_array = []
    digital_annotation_array = []
    pressure_measure_array = []
    flow_measure_array = []
    total_flow_measure_array = []
    digital_measure_array = []

    for annotation in annotation_list:
        time = annotation.timestamp
        annotation_data = [time, annotation.channel, annotation.shorttext, annotation.text, annotation.icon]
        if "Pressure" in annotation.channel:
            pressure_annotation_array.append(annotation_data)
        elif "Flow" in annotation.channel:
            flow_annotation_array.append(annotation_data)
        elif "Total flow" in annotation.channel:
            total_flow_annotation_array.append(annotation_data)
        elif "Digital" in annotation.channel:
            digital_annotation_array.append(annotation_data)

    for measure in measurement_list:
        time = str(measure.timestamp).replace("-", "/")
        if checkbox_pressure == "1":
            pressure_measure_array.append([time,
                                           plot_check_is_none(measure.ch1_pressure) * pressure_factor,
                                           plot_check_is_none(measure.ch2_pressure) * pressure_factor])
        if checkbox_flow == "1":
            flow_measure_array.append([time,
                                       plot_check_is_none(measure.ch1_flow) * flow_factor,
                                       plot_check_is_none(measure.ch2_flow) * flow_factor])
        if checkbox_total_flow == "1":
            total_flow_measure_array.append([time,
                                             plot_check_is_none(measure.ch1_total_flow) * total_flow_factor,
                                             plot_check_is_none(measure.ch2_total_flow) * total_flow_factor])
        if checkbox_digital == "1":
            digital_measure_array.append([time,
                                          plot_check_is_none(measure.digital_ch1),
                                          plot_check_is_none(measure.digital_ch2)])

    datasets = []
    if checkbox_pressure == "1":
        pressure_data = {
            'name'              : "Pressure",
            'ime'               : "Tlak",
            'unit'              : "[" + pressure_unit + "]",
            'measure_data'      : pressure_measure_array,
            'annotation_data'   : pressure_annotation_array,
            'label_data'        : ['Pressure CH1', 'Pressure CH2'],
            'label_data_extra'  : [channel_labels['ch1_pressure_label'], channel_labels['ch2_pressure_label']],
            'equation_data'     : [None, None],
            'show_total_panel'  : False
        }
        datasets.append(pressure_data)
    if checkbox_flow == "1":
        flow_data = {
            'name'              : "Flow",
            'ime'               : "Protok",
            'unit'              : "[" + flow_unit + "]",
            'measure_data'      : flow_measure_array,
            'annotation_data'   : flow_annotation_array,
            'label_data'        : ['Flow CH1', 'Flow CH2'],
            'label_data_extra'  : [channel_labels['ch1_flow_label'], channel_labels['ch2_flow_label']],
            'equation_data'     : [None, None],
            'show_total_panel'  : False
        }
        datasets.append(flow_data)
    if checkbox_total_flow == "1":
        total_flow_data = {
            'name'              : "Total Flow",
            'ime'               : "Ukupni Protok",
            'unit'              : "[" + total_flow_unit + "]",
            'measure_data'      : total_flow_measure_array,
            'annotation_data'   : total_flow_annotation_array,
            'label_data'        : ['Total flow CH1', 'Total flow CH2'],
            'label_data_extra'  : [channel_labels['ch1_flow_label'], channel_labels['ch2_flow_label']],
            'equation_data'     : [None, None],
            'show_total_panel'  : True
        }
        datasets.append(total_flow_data)
    if checkbox_digital == "1":
        digital_data = {
            'name'              : "Digital input",
            'ime'               : "Digitalni ulaz",
            'label_ch1'         : channel_labels['ch1_digital_label'],
            'label_ch2'         : channel_labels['ch2_digital_label'],
            'unit'              : "[0/1]",
            'measure_data'      : digital_measure_array,
            'annotation_data'   : digital_annotation_array,
            'label_data'        : ['Digital CH1', 'Digital CH2'],
            'label_data_extra'  : [channel_labels['ch1_digital_label'], channel_labels['ch2_digital_label']],
            'equation_data'     : [None, None],
            'show_total_panel'  : False
        }
        datasets.append(digital_data)

    return datasets

def plot_generate_mag8000(request):
    device_id           = request.GET.get('device_id')
    plot_start_date     = datetime.strptime(request.GET.get('startDateInput'), "%Y-%m-%d %H:%M:%S")
    plot_end_date       = datetime.strptime(request.GET.get('endDateInput'), "%Y-%m-%d %H:%M:%S")

    checkbox_analog     = request.GET.get('analog')
    checkbox_flow       = request.GET.get('flow')
    checkbox_total_flow = request.GET.get('total_flow')

    analog_current_unit = 'mA'
    analog_voltage_unit = 'V'
    flow_unit           = ''
    total_flow_unit     = ''

    device = devices_return_by_id(device_id)
    if device.loc_pressurerecalculateunit is not None:
        analog_current_unit = device.loc_pressurerecalculateunit

    if device.loc_flowrecalculateunit is not None:
        analog_voltage_unit = device.loc_flowrecalculateunit

    analog_current_equation = None
    analog_voltage_equation = None

    if device.loc_recalculateequation1 is not None and device.loc_recalculateequation1 != '':
        analog_current_equation=device.loc_recalculateequation1

    if device.loc_recalculateequation2 is not None and device.loc_recalculateequation2 != '':
        analog_voltage_equation = device.loc_recalculateequation2

    measurement_list = measure_get_measure_data(device_id, plot_start_date, plot_end_date)

    annotation_list = Lldeviceannotations.objects.filter(   timestamp__gte=plot_start_date,
                                                            timestamp__lte=plot_end_date,
                                                            lldevicelist=device_id).order_by('timestamp')

    channel_labels = devices_return_channel_labels(device_id)

    analog_annotation_array = []
    flow_annotation_array = []
    total_flow_annotation_array = []
    analog_measure_array = []
    flow_measure_array = []
    total_flow_measure_array = []

    for annotation in annotation_list:
        time = annotation.timestamp
        annotation_data = [time, annotation.channel, annotation.shorttext, annotation.text, annotation.icon]
        if "Analog" in annotation.channel:
            analog_annotation_array.append(annotation_data)
        elif "Flow" in annotation.channel:
            flow_annotation_array.append(annotation_data)
        elif "Total flow" in annotation.channel:
            total_flow_annotation_array.append(annotation_data)


    for measure in measurement_list:
        time = str(measure.timestamp).replace("-", "/")
        if checkbox_analog == "1":
            analog_measure_array.append([time,
                                           plot_check_is_none(measure.analog_current),
                                           plot_check_is_none(measure.analog_voltage)])
        if checkbox_flow == "1":
            flow_measure_array.append([time,
                                       plot_check_is_none(measure.flow_value)])
        if checkbox_total_flow == "1":
            total_flow_measure_array.append([time,
                                             plot_check_is_none(measure.total_flow1),
                                             plot_check_is_none(measure.total_flow2),
                                             plot_check_is_none(measure.total_flow3),])



    if len(measurement_list)>0:
        measurement_line = measurement_list[0]
        flow_unit = measurement_line.flow_unit
        total_flow_unit = measurement_line.total_unit

    datasets = []
    if checkbox_analog == "1":
        analog_data = {
            'name'              : "Analog",
            'ime'               : "Analogni",
            'unit'              : "[" + analog_current_unit + "/" +analog_voltage_unit + "]",
            'measure_data'      : analog_measure_array,
            'annotation_data'   : analog_annotation_array,
            'label_data'        : ['Analog current', 'Analog voltage'],
            'label_data_extra'  : [channel_labels['ch1_pressure_label'], channel_labels['ch2_pressure_label']],
            'equation_data'     : [analog_current_equation, analog_voltage_equation],
            'show_total_panel'  : False
        }
        datasets.append(analog_data)
    if checkbox_flow == "1":
        flow_data = {
            'name'              : "Flow",
            'ime'               : "Protok",
            'unit'              : "[" + flow_unit + "]",
            'measure_data'      : flow_measure_array,
            'annotation_data'   : flow_annotation_array,
            'label_data'        : ['Flow'],
            'label_data_extra'  : [channel_labels['ch1_flow_label']],
            'equation_data'     : [None],
            'show_total_panel'  : False
        }
        datasets.append(flow_data)
    if checkbox_total_flow == "1":
        total_flow_data = {
            'name'              : "Total Flow",
            'ime'               : "Ukupni Protok",
            'unit'              : "[" + total_flow_unit + "]",
            'measure_data'      : total_flow_measure_array,
            'annotation_data'   : total_flow_annotation_array,
            'label_data'        : ['Total flow CH1', 'Total flow CH2', 'Total flow CH3'],
            'label_data_extra'  : [channel_labels['ch2_flow_label'], channel_labels['ch1_digital_label'], channel_labels['ch2_digital_label']],
            'equation_data'     : [None, None, None],
            'show_total_panel'  : True
        }
        datasets.append(total_flow_data)

    return datasets

def plot_get_status_page(request):
    if request.user.is_authenticated():
        if request.method == 'GET':
            device_id           = request.GET.get('device_id')
            device_identifier   = devices_return_identifier(device_id)

            user_device_type = user_get_device(request)
            title_string_top    = "Identifikator: "+device_identifier
            title_string_bottom = "ID: "+device_id
            return render_to_response('plot_status.html', { 'user_object'           : request.user,
                                                            'user_device_type'      : user_device_type,
                                                            'title_string_top'      : title_string_top,
                                                            'title_string_bottom'   : title_string_bottom,
                                                            'device_id'             : device_id,
                                                            'lang'                  : user_get_language(request)})
        else:
            return redirect('/leakless-monitor')

    else:
        return redirect('/leakless-monitor')

def plot_get_status_data(request):
    if request.user.is_authenticated():
        response = {}
        if request.method == 'POST':
            return_data   = json.loads(request.body)
            device_id     = return_data['device_id']
            start_time    = datetime.strptime(return_data['start_time'], "%Y-%m-%d %H:%M:%S")
            end_time      = datetime.strptime(return_data['end_time'], "%Y-%m-%d %H:%M:%S")

            device = devices_return_by_id(device_id)

            if device.loc_type == 'PDL-AG':
                status_list = LldevicePdlagStatus.objects.filter(
                    timestamp__gte=start_time, timestamp__lte=end_time,
                    lldevicelist=device_id).order_by('timestamp')

                data_array = []

                for status in status_list:
                    time = str(status.timestamp).replace("-", "/")
                    data_array.append([time,
                                       status.signal,
                                       status.battery])
                response['text'] = 'Ok'
                response['data'] = data_array

            elif device.loc_type == 'MAG8000':
                status_list = LldeviceMAG8000Status.objects.filter(
                    timestamp__gte=start_time, timestamp__lte=end_time,
                    lldevicelist=device_id).order_by('timestamp')

                data_array = []

                for status in status_list:
                    time = str(status.timestamp).replace("-", "/")
                    data_array.append([time,
                                       0,
                                       status.battery])

                response['text'] = 'Ok'
                response['data'] = data_array
            else:

                response['text'] = 'Unsupported device type'
                response['data'] = None



        else:
            response['text'] = 'Unsported method'
            response['data'] = None

        return JsonResponse(response)

    else:
        return redirect(reverse('login_page'))

def plot_check_is_none(value):
    if value is None:
        value=0
    return value


