# -*- coding: utf-8 -*-
from ...tools.shapes import Shape

from dia.models import InsulinAdministration, InsulinType


class ShapeInsulin(Shape):
    """
    Sirve de envoltura al objeto InsulinAdministration
    añadiendo los shapes
    """
    def __init__(self, insulin):
        assert isinstance(insulin, InsulinAdministration), "insulin debe ser una instancia de InsulinAdministration"
        self._insulin = insulin
        self._shape = ShapeInsulin.SHAPE[self.type]

    @property
    def dose(self):
        return self._insulin.dose
    
    @property
    def datetime(self):
        return self._insulin.datetime
    
    @property
    def type(self):
        return self._insulin.type

    def dose_absorbed_at_range_datetime(self, start_dt, end_dt):
        start_minute = (start_dt - self.datetime).total_mins
        end_minute = (end_dt - self.datetime).total_mins
                        
        proportional_value = self.pointers(start_minute, end_minute).\
            value_inside_pointers(self.dose)

        return proportional_value
    
    
    """
    Revisados y actualizados aquí a fecha de 31/01/2017 8:58
    """
    SHAPE = {
        InsulinType.RAPID: [
            [    0,   0], [   10,  27], [   20,  55], [   30,  71],
            [   40,  86], [   50, 100], [   60,  91], [   70,  82],
            [   80,  73], [   90,  63], [  100,  54], [  110,  45],
            [  120,  39], [  130,  33], [  140,  27], [  150,  21],
            [  160,  15], [  170,  11], [  180,  10], [  190,   8],
            [  200,   6], [  210,   5], [  220,   3], [  230,   2],
            [  240,   0],
        ],
        InsulinType.SHORT: [
            [    0,   0], [   10,  10], [   20,  20], [   30,  30],
            [   40,  42], [   50,  54], [   60,  67], [   70,  72],
            [   80,  78], [   90,  83], [  100,  89], [  110,  94],
            [  120, 100], [  130,  98], [  140,  97], [  150,  95],
            [  160,  93], [  170,  92], [  180,  90], [  190,  85],
            [  200,  80], [  210,  75], [  220,  70], [  230,  65],
            [  240,  60], [  250,  58], [  260,  56], [  270,  53],
            [  280,  51], [  290,  49], [  300,  47], [  310,  44],
            [  320,  42], [  330,  40], [  340,  38], [  350,  36],
            [  360,  33], [  370,  31], [  380,  29], [  390,  27],
            [  400,  24], [  410,  22], [  420,  20], [  430,  18],
            [  440,  16], [  450,  13], [  460,  11], [  470,   9],
            [  480,   7], [  490,   4], [  500,   2], [  510,   0],
        ],
        InsulinType.INTERMEDIATE: [
            [    0,   0], [   10,   5], [   20,   9], [   30,  14],
            [   40,  19], [   50,  23], [   60,  27], [   70,  31],
            [   80,  35], [   90,  39], [  100,  43], [  110,  47],
            [  120,  50], [  130,  54], [  140,  57], [  150,  61],
            [  160,  64], [  170,  67], [  180,  70], [  190,  73],
            [  200,  76], [  210,  78], [  220,  81], [  230,  83],
            [  240,  85], [  250,  87], [  260,  89], [  270,  91],
            [  280,  92], [  290,  94], [  300,  95], [  310,  96],
            [  320,  97], [  330,  98], [  340,  99], [  350, 100],
            [  450,  99], [  470,  98], [  480,  97], [  500,  96],
            [  510,  94], [  520,  93], [  530,  92], [  540,  91],
            [  550,  89], [  560,  87], [  570,  86], [  580,  84],
            [  590,  82], [  600,  80], [  610,  78], [  620,  76],
            [  630,  74], [  640,  72], [  650,  70], [  660,  68],
            [  670,  66], [  680,  64], [  690,  62], [  700,  61],
            [  710,  59], [  720,  57], [  730,  55], [  740,  53],
            [  750,  52], [  760,  50], [  770,  48], [  780,  47],
            [  790,  45], [  800,  44], [  810,  42], [  820,  40],
            [  830,  39], [  840,  37], [  850,  36], [  860,  34],
            [  870,  33], [  880,  32], [  890,  30], [  900,  29],
            [  910,  28], [  920,  26], [  930,  25], [  940,  24],
            [  950,  23], [  960,  21], [  970,  20], [  980,  19],
            [  990,  18], [ 1000,  17], [ 1010,  16], [ 1020,  15],
            [ 1030,  14], [ 1040,  13], [ 1050,  12], [ 1060,  11],
            [ 1070,  10], [ 1080,   9], [ 1090,   8], [ 1100,   7],
            [ 1110,   6], [ 1130,   5], [ 1140,   4], [ 1150,   3],
            [ 1170,   2], [ 1180,   1], [ 1200,   0],
        ],
        InsulinType.SLOW: [
            [    0,   0], [   10,   7], [   20,  13], [   30,  20],
            [   40,  27], [   50,  33], [   60,  40], [   70,  47],
            [   80,  53], [   90,  60], [  100,  67], [  110,  73],
            [  120,  80], [  130,  82], [  140,  83], [  150,  85],
            [  160,  87], [  170,  88], [  180,  90], [  190,  91],
            [  200,  92], [  210,  93], [  230,  94], [  240,  95],
            [  250,  96], [  260,  97], [  270,  98], [  290,  99],
            [  300, 100], [ 1390,  83], [ 1400,  67], [ 1410,  50],
            [ 1420,  33], [ 1430,  17], [ 1440,   0],
        ],
        InsulinType.SLOW: [
            [    0,   0], [   10,   7], [   20,  13], [   30,  20],
            [   40,  27], [   50,  33], [   60,  40], [   70,  47],
            [   80,  53], [   90,  60], [  100,  67], [  110,  73],
            [  120,  80], [  130,  82], [  140,  83], [  150,  85],
            [  160,  87], [  170,  88], [  180,  90], [  190,  91],
            [  200,  92], [  210,  93], [  230,  94], [  240,  95],
            [  250,  96], [  260,  97], [  270,  98], [  290,  99],
            [  300, 100], [ 2770,  92], [ 2780,  83], [ 2790,  75],
            [ 2800,  67], [ 2810,  58], [ 2820,  50], [ 2830,  42],
            [ 2840,  33], [ 2850,  25], [ 2860,  17], [ 2870,   8],
            [ 2880,   0],
        ]
    }
    



