# -*- coding: utf-8 -*-
"""
Created on Wed Jul 11 09:28:46 2018

@author: BI4
"""

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

def plotMCT(MCT):
    
    plt.figure(figsize=(24,12))
    _map = Basemap(projection='merc',
                  resolution='l',
                  llcrnrlon=-93, llcrnrlat=7,
                  urcrnrlon=-75, urcrnrlat=19)
    
    _map.drawcoastlines()
    _map.drawcountries(linewidth=1)
    _map.drawmapboundary(fill_color='#99ffff')
    _map.fillcontinents(color='#cc9966',lake_color='#99ffff')
    
    
    _lons = [-90.25,-88.9167,-86.5,-85,-84,-81]
    _lats = [15.5,13.7,15,13,10,8.2]
    _inter = {'GUAESA':[0,1,'{0} MW'],
              'GUAHON':[0,2,'{0} MW'],
              'ESAHON':[1,2,'{0} MW'],
              'HONNIC':[2,3,'{0} MW'],
              'NICCRC':[3,4,'{0} MW'],
              'CRCPAN':[4,5,'{0} MW']}
    
    _x,_y = _map(_lons,_lats)
    
    for i in _inter:
        a = [_x[_inter[i][0]],_x[_inter[i][1]]]
        b = [_y[_inter[i][0]],_y[_inter[i][1]]]
        
        if MCT[i][1] >= MCT[i][2] or MCT[i][1] <= MCT[i][0]:
            color = 'orangered'
        elif MCT[i][1] >= 0.8*MCT[i][2] or MCT[i][1] <= 0.8*MCT[i][0]:
            color = 'yellow'
        else:
            color = 'green'
        
        plt.plot(a,b,'-',color=color,linewidth=3, label=_inter[i][2])
    
        plt.text(sum(a)/2,sum(b)/2,'{:.2f} MW'.format(MCT[i][1]*100), 
                 fontdict={'fontsize': 12, 'backgroundcolor':'#cc9966', 'fontweight':'bold'},
                 horizontalalignment='center',
                 verticalalignment='center',)
    
    _map.plot(_x,_y,'o', color='navy',markersize=18)
    
    red_patch = mpatches.Patch(color='red', label='Interconexión al 100% de capacidad')
    yellow_patch = mpatches.Patch(color='yellow', label='Interconexión a mas del 80% de capacidad')
    green_patch = mpatches.Patch(color='green',label='Interconexión a menos del 80% de capacidad')
    plt.legend(handles=[red_patch,yellow_patch,green_patch], loc=3, fontsize=16)
    
    plt.suptitle('Flujos entre Áreas de Control', fontsize =18, fontweight='bold')
    plt.show()