#!/usr/local/bin/python3
# -*- Coding: utf-8 -*-

from db import database

# 分析
def makepage_analyze():

    # DB利用
    db = database("jibun.db")

    #変数初期化
    boundaryValue = 3.0
    positiveColor = '#ffabce'
    negativeColor = '#d3eef9'
    item_list = []
    assessment_list = []

    bodycontents = ''

    # 分析表示
    bodycontents += '''
        <section class="panel">
            <table width="100%">
                <thead>
                    <tr>
                        <th>項目名</th><th>評価平均</th><th>評価個数</th>
                    </tr>
                </thead>
                <tbody>
    '''

    # 分析表示；テーブル
    for e in sorted(db.get_record_totalization(), key=lambda e: e[1], reverse=True):
        
        # 配列に格納
        item_list.append(e[0])
        assessment_list.append(e[1])

        # 色変更用変数の設定
        trBgColor = positiveColor if e[1] > boundaryValue else negativeColor
        # if e[1] < boundaryValue:
        #     trBgColor = negativeColor
            
        bodycontents += "<tr style='background-color:" + trBgColor + "';>"
        bodycontents += "<td nowrap>" + str(e[0]) + "</td>"        
        bodycontents += "<td nowrap align='right'>" + str(e[1]) + "</td>"
        bodycontents += "<td nowrap align='right'>" + str(e[2]) + "</td>"      
        bodycontents += "</tr>"

    bodycontents += '''
                </tbody>
            </table>
        </div>
    '''

    # 分析表示：グラフ
    bodycontents += '''
        <br>
        <div>
            <canvas id="myChart" width="100" height="100"></canvas>
        <div>
        <script>

            // 値を設定
            var datasets = [
                {
                    data: [
    '''

    #配列より評価値の取得
    for assessment in assessment_list:
       bodycontents +=  str(assessment) + ','
    
    bodycontents += '''      
                    ],
                    backgroundColor: [] // グラフ色用配列
                },
            ]

            // 各棒グラフの値が正か負かによって色分け
            for (var i = 0; i < datasets[0].data.length; i++) {
                if (datasets[0].data[i] > ''' + str(boundaryValue)  + '''){
                    datasets[0].backgroundColor[i] = " ''' + positiveColor  + ''' " // ポジティブ
                } else {
                    datasets[0].backgroundColor[i] = " ''' + negativeColor  + ''' " // ネガティブ
                }
            } 
            
            //円グラフ（ドーナッツ型）
            var ctx = document.getElementById("myChart");
            var myChart = new Chart(ctx, {
                //グラフの種類　pie or doughnut
                type: 'doughnut',
                data: {
                    //データ項目のラベル
                    labels: [
    '''
    #配列より項目名の取得
    for item in item_list:
        bodycontents +=  '"' + str(item) + '",'
    
    bodycontents += '''
                    ],
                    datasets: datasets
                },
                    options: {
                        responsive: true , hover: {mode: null}
                    },
                }
            );
        </script>
    '''
    return bodycontents