from flask import Flask, render_template, request, redirect, url_for, session
import csv
import os
import pandas as pd
import tkinter as tk
from tkinter import messagebox
from pyvis.network import Network
import json
import re
zikeiretu = pd.read_csv("csv/fusion_data_forVARLiNGAM.csv")
app = Flask(__name__)
app.secret_key = 'your_secret_key'  # セッションの安全性のためのキーを設定
print(zikeiretu[["Day"]])
@app.route("/page/<node_label>")
def page(node_label):
    # Remove (t) or (t-1) from node_label
    node_label_cleaned = re.sub(r'\(t(?:-1)?\)', '', node_label)
    # Redirect to the cleaned URL
    return redirect(url_for("page", node_label=node_label_cleaned))

@app.route('/')
def home():
   def scraping():
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    import time
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.chrome.service import Service as ChromeService
    from webdriver_manager.chrome import ChromeDriverManager
    from pathlib import Path
    import pandas as pd
    import os
    start_year = 2000
    options = webdriver.ChromeOptions()
    #options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    dldir_path = Path('csv')  # csv という名前のフォルダとする
    dldir_path.mkdir(exist_ok=True)  # なければ作成
    download_dir = str(dldir_path.resolve())  # 絶対パスを取得
    print("download_dir: " + download_dir)

    options.add_experimental_option('prefs', {    # Chrome のオプションに
    'download.default_directory': download_dir  # 絶対パスで指定  
    })
    service = Service(executable_path='C:/Users/yukiy/OneDrive/デスクトップ/研究室引継ぎ/蒲田さん/flask/flask/webdriver/chromedriver.exe')
    driver = webdriver.Chrome(service=service, options=options)
    driver.implicitly_wait(10)
    # for i in range(1,3): 
    #  print(i)
    #  driver.get("https://www.stat-search.boj.or.jp/")
    #  elem = driver.find_element(By.XPATH, '//*[@id="toukei_4"]/a')
    #  elem.click()
    #  elem = driver.find_element(By.XPATH, '//*[@id="toukei_4"]/ul/li['+str(i)+']/a')
    #  elem.click()
    #  elem_tenkai = driver.find_element(By.XPATH, '//*[@id="menuSearchTabpanel"]/div[2]/div[1]/div[2]/input')
    #  elem_tenkai.click()
    #  elem_check = driver.find_element(By.XPATH, '//*[@id="menuSearchDataCodeList"]/tbody/tr[1]/td/label')
    #  elem_check.click()
    #  elem = driver.find_element(By.XPATH, '//*[@id="menuSearchTabpanel"]/div[2]/div[2]/div[4]/a')
    #  elem.click()
    #  elem_start_year = driver.find_element(By.XPATH, '//*[@id="fromYear"]')
    #  start_year = elem_start_year.send_keys(start_year)
    #  elem = driver.find_element(By.XPATH, '//*[@id="resultArea"]/div[4]/ul/li[1]/a')
    #  elem.click()
    #  handle_array = driver.window_handles
    #  driver.switch_to.window(handle_array[i])
    #  elem = driver.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/table/tbody/tr[2]/td[5]/a')
    #  elem.click()
    #  handle_array = driver.window_handles
    #  driver.switch_to.window(handle_array[i+1])
    #  elem = driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div/table/tbody/tr/td/a')
    #  elem.click()
    url=driver.get("https://indexes.nikkei.co.jp/nkave/index/profile?idx=nk500av")
    click_elem = driver.find_element(By.XPATH, '/html/body/div[1]/div/main/section[3]/div/nav/ul/li[2]/a').get_attribute('href')
    driver.get(click_elem)
    time.sleep(3)
    os.remove('csv/nwe.csv')
    old_csv = "csv/nikkei_500_stock_average_daily_jp.csv"
    new_csv = "csv/nwe.csv"
    os.rename(old_csv,new_csv)
    # click_elem = driver.find_element(By.XPATH, '/html/body/div[1]/div/main/section[3]/div/nav/ul/li[2]/a').get_attribute('href')
    # click_elem
    # driver.get(click_elem)
    # for i in range(30):                       /html/body/div[1]/div/main/section[3]/div/nav/ul/li[2]/a
    #     click_elem.click()
    # elem = driver.find_element(By.XPATH, '//*[@id="fourvalue_timeline"]/tbody')
    # print(elem.text)
    file_path = "csv/nwe.csv"

    try:
        df = pd.read_csv(file_path, encoding='cp932')  # エンコーディングを適切なものに変更
        df["データ日付"] = pd.to_datetime(df["データ日付"], errors='coerce', format='%Y/%m/%d')
    except UnicodeDecodeError as e:
        print(f"CSVファイルの読み込みエラー: {e}")
    except ValueError as ve:
        print(f"日付の変換エラー: {ve}")

    df["データ日付"] = pd.to_datetime(df["データ日付"], format='%Y/%m/%d')
    df = df.set_index('データ日付')

    #不必要なカラムを削除。今回の指数の値は全て終値で示します。
    df = df.drop(['始値', '高値', '安値'], axis=1)
    df = df.sort_index(ascending=True)

    #名前の変更
    df.rename(columns={'終値':'Nikkei500','業種別（水産）終値':'Fishery', '業種別（鉱業）終値':'Mining', '業種別（建設）終値':'Construction', '業種別（食品）終値':'Foods',\
    '業種別（繊維）終値':'Fiber','業種別（パルプ・紙）終値':'Pulp & paper', '業種別（化学）終値':'Chemicals', '業種別（医薬品）終値':'Pharmaceuticals',\
    '業種別（石油）終値':'Petroleum','業種別（ゴム）終値':'Rubber', '業種別（窯業）終値':'Glass & Ceramics','業種別（鉄鋼）終値':'Steel','業種別（非鉄・金属）終値':'Nonferrous metals',\
    '業種別（機械）終値':'Machinery','業種別（電気機器）終値':'Electric machinery','業種別（造船）終値':'Shipbuilding','業種別（自動車）終値':'Automotive',\
    '業種別（輸送用機器）終値':'Transportation instruments','業種別（精密機器）終値':'Precision instruments', '業種別（その他製造）終値':'Other manufacturing','業種別（商社）終値':'Trading companies',\
    '業種別（小売業）終値':'Retail','業種別（銀行）終値':'Banking','業種別（その他金融）終値':'Other financial services','業種別（証券）終値':'Securities',\
    '業種別（保険）終値':'Insurance','業種別（不動産）終値':'Real estate','業種別（鉄道・バス）終値':'Railway/bus','業種別（陸運）終値':'Land transport',\
    '業種別（海運）終値':'Marine transport', '業種別（空運）終値':'Air transport', '業種別（倉庫）終値':'Warehousing', '業種別（通信）終値':'Communications',\
    '業種別（電力）終値':'Electric power', '業種別（ガス）終値':'Gas', '業種別（サービス）終値':'Services'}, inplace=True)
    pd.set_option('display.max_rows', None)
    pd.set_option('display.max_columns', None)
    # dfの最後の行を削除
    df = df.drop(df.index[-1])
   scraping()
   def kyoki_word_network():
    got_net = Network(height="1000px", width="95%", bgcolor="#FFFFFF", font_color="black", notebook=True, directed=True)
    print(zikeiretu)
    got_data = pd.read_csv("ultra_kekka.csv")[:2000]
    sources = got_data['from']
    targets = got_data['to']
    weights = got_data['effect']

    unique_nodes = set(sources) | set(targets)

    nodes = [{"id": node, "label": node, "title": node} for node in unique_nodes]

    edges = [{"from": src, "to": dst, "color": 'red' if w < 0 else 'blue', "label": str(round(w, 4)), "arrows": "to"} for src, dst, w in zip(sources, targets, weights)]

    nodes_json = json.dumps(nodes, ensure_ascii=False)
    edges_json = json.dumps(edges, ensure_ascii=False)

    html_code = """
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Your Network</title>
        <style>
            #mynetwork {
                height: 1000px;
                width: 95%;
                border: 1px solid lightgray;
            }
        </style>
    </head>
    <body>
        <div id="mynetwork"></div>
        <script type="text/javascript" src="https://unpkg.com/vis-network/standalone/umd/vis-network.min.js"></script>
        <script>
            var container = document.getElementById("mynetwork");
            var nodes = new vis.DataSet(""" + nodes_json + """);
            var edges = new vis.DataSet(""" + edges_json + """);
            var data = {nodes: nodes, edges: edges};
            var options = {};
            var network = new vis.Network(container, data, options);

            // ノードのダブルクリック用のイベントリスナーを追加
            network.on("doubleClick", function (params) {
                if (params.nodes.length > 0) {
                    var nodeId = params.nodes[0];
                    var nodeLabel = nodes.get(nodeId).label;

                    // (t) または (t-1) を外す
                    nodeLabel = nodeLabel.replace(/(\(t\)|\(t-1\))/g, "");

                    // ページ遷移や他のダブルクリックされたノードに基づくアクションを実行
                    var baseUrl = "page/" + nodeLabel;
                    window.location.href = baseUrl;  // ページ遷移のURLを変更してください
                }
            });
        </script>
    </body>
    </html>
    """

    with open("your_network.html", "w", encoding="utf-8") as html_file:
        html_file.write(html_code)
        got_net.show_buttons(filter_=['physics'])
        return got_net

   kyoki_word_network()
   return render_template("3D.html", df=zikeiretu)

    
###############################################################################################
#######################################2ページ目右下
###############################################################################################
###############################################################################################
##########################################スクレイピング
###############################################################################################
def scraping():
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    import time
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.chrome.service import Service as ChromeService
    from webdriver_manager.chrome import ChromeDriverManager
    from pathlib import Path
    import pandas as pd
    import os
    start_year = 2000
    options = webdriver.ChromeOptions()
    #options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    dldir_path = Path('csv')  # csv という名前のフォルダとする
    dldir_path.mkdir(exist_ok=True)  # なければ作成
    download_dir = str(dldir_path.resolve())  # 絶対パスを取得
    print("download_dir: " + download_dir)

    options.add_experimental_option('prefs', {    # Chrome のオプションに
    'download.default_directory': download_dir  # 絶対パスで指定  
    })
    service = Service(executable_path='C:/Users/Owner/OneDrive - 富山県立大学/研究室/卒業研究/chromedriver-win64/chromedriver-win64/chromedriver.exe')
    driver = webdriver.Chrome(service=service, options=options)
    driver.implicitly_wait(10)
    # for i in range(1,3): 
    #  print(i)
    #  driver.get("https://www.stat-search.boj.or.jp/")
    #  elem = driver.find_element(By.XPATH, '//*[@id="toukei_4"]/a')
    #  elem.click()
    #  elem = driver.find_element(By.XPATH, '//*[@id="toukei_4"]/ul/li['+str(i)+']/a')
    #  elem.click()
    #  elem_tenkai = driver.find_element(By.XPATH, '//*[@id="menuSearchTabpanel"]/div[2]/div[1]/div[2]/input')
    #  elem_tenkai.click()
    #  elem_check = driver.find_element(By.XPATH, '//*[@id="menuSearchDataCodeList"]/tbody/tr[1]/td/label')
    #  elem_check.click()
    #  elem = driver.find_element(By.XPATH, '//*[@id="menuSearchTabpanel"]/div[2]/div[2]/div[4]/a')
    #  elem.click()
    #  elem_start_year = driver.find_element(By.XPATH, '//*[@id="fromYear"]')
    #  start_year = elem_start_year.send_keys(start_year)
    #  elem = driver.find_element(By.XPATH, '//*[@id="resultArea"]/div[4]/ul/li[1]/a')
    #  elem.click()
    #  handle_array = driver.window_handles
    #  driver.switch_to.window(handle_array[i])
    #  elem = driver.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/table/tbody/tr[2]/td[5]/a')
    #  elem.click()
    #  handle_array = driver.window_handles
    #  driver.switch_to.window(handle_array[i+1])
    #  elem = driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div/table/tbody/tr/td/a')
    #  elem.click()
    url=driver.get("https://indexes.nikkei.co.jp/nkave/index/profile?idx=nk500av")
    click_elem = driver.find_element(By.XPATH, '/html/body/div[1]/div/main/section[3]/div/nav/ul/li[2]/a').get_attribute('href')
    driver.get(click_elem)
    time.sleep(3)
    os.remove('csv/nwe.csv')
    old_csv = "csv/nikkei_500_stock_average_daily_jp.csv"
    new_csv = "csv/nwe.csv"
    os.rename(old_csv,new_csv)
    # click_elem = driver.find_element(By.XPATH, '/html/body/div[1]/div/main/section[3]/div/nav/ul/li[2]/a').get_attribute('href')
    # click_elem
    # driver.get(click_elem)
    # for i in range(30):                       /html/body/div[1]/div/main/section[3]/div/nav/ul/li[2]/a
    #     click_elem.click()
    # elem = driver.find_element(By.XPATH, '//*[@id="fourvalue_timeline"]/tbody')
    # print(elem.text)
    file_path = "C:/Users/Owner/OneDrive - 富山県立大学/デスクトップ/scraping/csv/nwe.csv"

    try:
        df = pd.read_csv(file_path, encoding='cp932')  # エンコーディングを適切なものに変更
        df["データ日付"] = pd.to_datetime(df["データ日付"], errors='coerce', format='%Y/%m/%d')
    except UnicodeDecodeError as e:
        print(f"CSVファイルの読み込みエラー: {e}")
    except ValueError as ve:
        print(f"日付の変換エラー: {ve}")

    df["データ日付"] = pd.to_datetime(df["データ日付"], format='%Y/%m/%d')
    df = df.set_index('データ日付')

    #不必要なカラムを削除。今回の指数の値は全て終値で示します。
    df = df.drop(['始値', '高値', '安値'], axis=1)
    df = df.sort_index(ascending=True)

    #名前の変更
    df.rename(columns={'終値':'Nikkei500','業種別（水産）終値':'Fishery', '業種別（鉱業）終値':'Mining', '業種別（建設）終値':'Construction', '業種別（食品）終値':'Foods',\
    '業種別（繊維）終値':'Fiber','業種別（パルプ・紙）終値':'Pulp & paper', '業種別（化学）終値':'Chemicals', '業種別（医薬品）終値':'Pharmaceuticals',\
    '業種別（石油）終値':'Petroleum','業種別（ゴム）終値':'Rubber', '業種別（窯業）終値':'Glass & Ceramics','業種別（鉄鋼）終値':'Steel','業種別（非鉄・金属）終値':'Nonferrous metals',\
    '業種別（機械）終値':'Machinery','業種別（電気機器）終値':'Electric machinery','業種別（造船）終値':'Shipbuilding','業種別（自動車）終値':'Automotive',\
    '業種別（輸送用機器）終値':'Transportation instruments','業種別（精密機器）終値':'Precision instruments', '業種別（その他製造）終値':'Other manufacturing','業種別（商社）終値':'Trading companies',\
    '業種別（小売業）終値':'Retail','業種別（銀行）終値':'Banking','業種別（その他金融）終値':'Other financial services','業種別（証券）終値':'Securities',\
    '業種別（保険）終値':'Insurance','業種別（不動産）終値':'Real estate','業種別（鉄道・バス）終値':'Railway/bus','業種別（陸運）終値':'Land transport',\
    '業種別（海運）終値':'Marine transport', '業種別（空運）終値':'Air transport', '業種別（倉庫）終値':'Warehousing', '業種別（通信）終値':'Communications',\
    '業種別（電力）終値':'Electric power', '業種別（ガス）終値':'Gas', '業種別（サービス）終値':'Services'}, inplace=True)
    pd.set_option('display.max_rows', None)
    pd.set_option('display.max_columns', None)
    # dfの最後の行を削除
    df = df.drop(df.index[-1])


###############################################################################################
##########################################グラフ表示
###############################################################################################

from flask import Flask, render_template


def show_graph():
    from pyvis.network import Network
    
    # ネットワークグラフを作成
    g = Network()

    # ノードを30個追加
    for i in range(1, 31):
        node_id = i
        node_label = f"Node {i}"
        g.add_node(node_id, label=node_label)

    # エッジを追加（ノード間の接続、ここではランダムに接続している例）
    for i in range(30):
        from_node = i % 30 + 1
        to_node = (i + 1) % 30 + 1
        g.add_edge(from_node, to_node)

    # グラフを表示するHTMLファイルを作成
    html_file_path = "templates/network_graph.html"  # Flaskのtemplatesディレクトリに配置

    g.show(html_file_path, notebook=False)

    # HTMLテンプレートをレンダリングして表示
    with open(html_file_path, "r") as file:
        html_content = file.read()

    # JavaScriptのコードを生成
    js_code = '''
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vis/7.0.3/vis-network.min.js"></script>
    <script type="text/javascript">
        var container = document.getElementById("mynetwork");
        var options = {};
        var data = {
            nodes: new vis.DataSet([
    '''

    for i in range(1, 31):
        js_code += f'{{id: {i}, label: "Node {i}"}},'

    js_code += '''
            ]),
            edges: new vis.DataSet([
    '''

    for i in range(30):
        js_code += f'{{from: {i % 30 + 1}, to: {(i + 1) % 30 + 1}}},'

    js_code += '''
            ])
        };

        var network = new vis.Network(container, data, options);

        network.on("click", function (params) {
            if (params.nodes.length > 0) {
                var nodeId = params.nodes[0];
                switch (nodeId) {
    '''

    for i in range(1, 31):
        js_code += f'''
                    case {i}:
                        window.location.href = "/page/{i}";
                        break;
    '''

    js_code += '''
                    default:
                        break;
                }
            }
        });
    </script>
    '''

    # JavaScriptのコードをHTMLに挿入して更新
    html_content = html_content.replace('</body>', js_code + '</body>')

    with open(html_file_path, "w") as file:
        file.write(html_content)

    return render_template('network_graph.html')
###############################################################################################
#######################################インターネット作成VARLiNGAM
###############################################################################################
# app.route('sample',methods=['GET',"POST"])
# def sample():
#     return render_template("oresen.html",df)
###############################################################################################
#######################################共用
###############################################################################################
def generate_dynamic_page(page_number):
    def dynamic_page():
        g = Network()
        for i in range(1, 31):
            node_id = i
            node_label = f"Node {i}"
            g.add_node(node_id, label=node_label)
        for i in range(30):
            from_node = i % 30 + 1
            to_node = (i + 1) % 30 + 1
            g.add_edge(from_node, to_node)
        # グラフの表示をJavaScriptで処理するためのデータを取得
        nodes_data = g.nodes
        edges_data = g.edges

        return render_template('oresen.html', nodes=nodes_data, edges=edges_data)
    #ルートを追加
    app.add_url_rule(f'/page/{page_number}', view_func=dynamic_page,endpoint=f'dynamic_page_{page_number}')
for i in range(1,31):
 generate_dynamic_page(i)
@app.route('/logout')
def logout():
    # セッションからユーザー情報を削除し、ログアウト
    session.pop('username', None)
    session.pop('password', None)
    session.pop('name', None)
    return redirect(url_for('home'))

@app.route('/page/Gas')
def gas():
    # 日付とFiberの値を取得
    fiber_values = zikeiretu["Gas"].tolist()
    days = zikeiretu["Day"].tolist()
    return render_template('oresen.html', fiber_values=fiber_values, days=days, enumerate=enumerate)
@app.route('/page/Securities')
def securities():
    # 日付とFiberの値を取得
    fiber_values = zikeiretu["Securities"].tolist()
    days = zikeiretu["Day"].tolist()
    

    # グラフの表示
    return render_template('oresen.html', fiber_values=fiber_values, days=days, enumerate=enumerate)

if __name__ == '__main__':
    app.run(debug=True)