# encoding: utf-8
from __future__ import unicode_literals

import datetime
import os

import pandas as pd
from flask import Flask

from html_utils import TemplateRender

app = Flask(__name__, static_url_path='', static_folder='results', template_folder='.')


@app.route('/echarts.min.js')
def static_js():
    return app.send_static_file('echarts.min.js')


@app.route('/template')
def root():
    datetime_ohlc_df = pd.DataFrame(data=[[11.1, 14.1, 9.1, 10.1], [12.1, 15.1, 8.1, 13.1], [9.1, 16.1, 9.1, 16.1]],
                                    index=pd.DatetimeIndex([
                                        datetime.datetime.now().replace(day=10),
                                        datetime.datetime.now().replace(day=11),
                                        datetime.datetime.now().replace(day=12)
                                    ], name='datetime'),
                                    columns=['open', 'high', 'low', 'close'])

    order_history_df = pd.DataFrame(data=[[11, 21, 22], [22, 12, 21]],
                                    index=pd.DatetimeIndex([datetime.datetime.now().replace(day=10),
                                                            datetime.datetime.now().replace(day=11)]),
                                    columns=['amount', 'price', 'value'])
    # 标题，k线图，买入卖出点，是必填选项
    template_render = TemplateRender('下证指数', datetime_ohlc_df, order_history_df, '%y/%m/%d')

    indicator_df1 = pd.DataFrame(data=[12, 13, 14],
                                 index=pd.DatetimeIndex([
                                     datetime.datetime.now().replace(day=10),
                                     datetime.datetime.now().replace(day=11),
                                     datetime.datetime.now().replace(day=12)
                                 ], name='datetime'), columns=['value'])
    indicator_df2 = pd.DataFrame(data=[[17, 19], [18, 16], [19, 20]],
                                 index=pd.DatetimeIndex([
                                     datetime.datetime.now().replace(day=10),
                                     datetime.datetime.now().replace(day=11),
                                     datetime.datetime.now().replace(day=12)
                                 ], name='datetime'), columns=['value', 'count'])
    template_render.add_indicator('指标1', indicator_df1)  # 增加自己想要的指标
    template_render.add_indicator('指标2', indicator_df2)  # 可以支持不同顺序，不同数量

    filename = '%s.html' % datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")

    file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'results', filename)

    template_render.save(file_path)
    return template_render.render()  # save()，返回保存的文件路径


if __name__ == '__main__':
    # TemplateRender demo
    # http://127.0.0.1:5000/template
    app.run()
