from totalimpactwebapp.card import Card
from totalimpactwebapp import configs
from totalimpactwebapp.card_generate import *
import os


import datetime


def make(user):

    products = user.product_objects
    user_dict_about = user.dict_about()

    cards = []
    cards += ProductNewMetricCardGenerator.make(user, products)
    cards += ProfileNewMetricCardGenerator.make(user, products)

    for card in cards:
        card.set_product_from_list(products)
        card.metrics_info = configs.metrics()[card.metric_name]
        card.user = user_dict_about

    cards = filter_cards(cards)
    cards = sort_cards(cards)[0:10]

    response = {
        "user": user_dict_about,
        "cards": cards,
        "css": get_css(),
        "given_name": user.given_name
    }
    return response


def get_css():
    path = os.path.join(
        os.path.dirname(__file__),
        'static/less.emails/css/new-metrics.css'
    )
    file = open(path, "r")
    return file.read()


def sort_cards(cards):
    sorted_cards = sorted(cards, key=lambda card: card.sort_by(), reverse=True)
    return sorted_cards


def filter_cards(cards):
    ret = []
    for card in cards:
        if int(card.diff_value) <= 0:
            pass
        elif "pubmed" in card.metric_name:
            pass
        else:
            ret.append(card)

    return ret




















@@ -1153,32 +1153,49 @@
  card in cards:%0A
+        try:%0A    
         if int(c
@@ -1220,37 +1220,45 @@
  0:%0A            
+ 
+   
 pass%0A
+    
         elif %22pu
@@ -1288,32 +1288,36 @@
 me:%0A            
+    
 pass%0A        els
@@ -1313,16 +1313,20 @@
         
+ 
+   
 else:%0A  
@@ -1335,16 +1335,20 @@
         
+  
+  
 ret.appe
@@ -1357,16 +1357,118 @@
 (card)%0A%0A
+        # no integerable diff_value        %0A        except (ValueError, TypeError):%0A            pass%0A%0A
     retu

handle null diff value