# -*- coding: utf-8 -*-

import random

ENABLE = 1
NEWS_ID = 31
NEWS_DEBUG = False

DEFAULT_WEIGHT = 10


def random_by_weight(p):
    choice_id = []
    for i in range(len(p)):
        choice_id += [i for _ in range(DEFAULT_WEIGHT + p[i]["news_weight"])]

    return p[random.choice(choice_id)]


def random_news():
    news_list = [
        {
            "news_title": "第八屆泰北團-夢想，「泰」不一樣",
            "news_image": "http://i.imgur.com/iNbbd4B.jpg",
            "news_url": "https://docs.google.com/forms/d/11Awcel_MfPeiEkl7zQ0MldvnAw59gXKLecbIODPOaMs/viewform?edit_requested=true",
            "news_content": "",
            "news_weight": 3
        },
        {
            "news_title": "滔滔思想運動會",
            "news_image": "http://i.imgur.com/DQBGjNS.jpg",
            "news_url": "https://facebook.com/KUASTAO",
            "news_content": "",
            "news_weight": 4
        },
        {
            "news_title": "體委幹部體驗營",
            "news_image": "http://i.imgur.com/aJyQlJp.jpg",
            "news_url": "https://www.facebook.com/Kuas%E9%AB%94%E5%A7%94-440439566106678/?fref=ts",
            "news_content": "",
            "news_weight": 4
        },
        {
            "news_title": "箏偽",
            "news_image": "http://i.imgur.com/ZHb6FrC.jpg",
            "news_url": "https://www.facebook.com/kuaszither/?fref=ts",
            "news_content": "",
            "news_weight": 6
        }
    ]

    if NEWS_DEBUG:
        return news_list[0]
    else:
        return random_by_weight(news_list)


def news_status():
    return [ENABLE, NEWS_ID]


def news():
    """
    News for kuas.

    return [enable, news_id, news_title, news_template, news_url]
        enable: bool
        news_id: int
        news_title: string
        news_tempalte: string
        news_url: string
    """

    # Get news from random news
    news = random_news()

    news_title = news["news_title"]
    news_template = (
            "<div style='text-align:center;'>"
            "<div><img style='display:block;margin-left:auto;margin-right:auto;max-width:80%;min-height:150px;height:auto;' src='"
            + news["news_image"] + "'></img>" + news["news_content"] + "</div>" +
            "</div>"

        )
    news_url = news["news_url"]

    return [ENABLE, NEWS_ID, news_title, news_template, news_url]

@@ -663,243 +663,8 @@
   %7B%0A
-            %22news_title%22: %22%E6%BB%94%E6%BB%94%E6%80%9D%E6%83%B3%E9%81%8B%E5%8B%95%E6%9C%83%22,%0A            %22news_image%22: %22http://i.imgur.com/DQBGjNS.jpg%22,%0A            %22news_url%22: %22https://facebook.com/KUASTAO%22,%0A            %22news_content%22: %22%22,%0A            %22news_weight%22: 4%0A        %7D,%0A        %7B%0A
     
@@ -969,10 +969,278 @@
 %22: %22
-%E7%AE%8F%E5%81%BD
+%E9%81%8A%E6%88%B2%E5%A4%96%E6%8E%9B %E5%8E%9F%E7%90%86%E5%AF%A6%E6%88%B0%22,%0A            %22news_image%22: %22http://i.imgur.com/WkI23R2.jpg%22,%0A            %22news_url%22: %22https://www.facebook.com/profile.php?id=735951703168873%22,%0A            %22news_content%22: %22%22,%0A            %22news_weight%22: 6%0A        %7D,%0A        %7B%0A            %22news_title%22: %22%E5%A5%BD%E6%97%A5%E5%AD%90%E8%82%B2%E6%A8%82%E7%87%9F
 %22,%0A 
@@ -1273,34 +1273,161 @@
 http
+s
 ://
-i.imgur.com/ZHb6FrC.jpg
+scontent-hkg3-1.xx.fbcdn.net/hphotos-xft1/v/t34.0-0/p206x206/12834566_977348362345349_121675822_n.jpg?oh=e04f6830fdfe5d3a77e05a8b3c32fefc&oe=56E663E6
 %22,%0A 
@@ -1450,35 +1450,33 @@
 _url%22: %22https://
-www
+m
 .facebook.com/ku
@@ -1481,23 +1481,12 @@
 kuas
-zither/?fref=ts
+YGR/
 %22,%0A 
@@ -1555,16 +1555,17 @@
       %7D%0A
+%0A
     %5D%0A%0A 

update news