# -*- coding: utf-8 -*-
"""
/***************************************************************************
Name                 :  Mascaret
Description          : Mascaret tools for QGIS
Date                 : Juin 2020

email                :
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
"""
import datetime
import os

from qgis.PyQt.QtWidgets import QDialog, QFileDialog
from qgis.PyQt.uic import loadUi

from ..ui.custom_control import ClassWarningBox
from .model.ClassGetResults import ClassGetResults


class ClassImportRes(QDialog):
    def __init__(self, mgis, obj_mod=None):
        QDialog.__init__(self)
        self.mgis = mgis
        self.mdb = self.mgis.mdb
        self.obj_mod = obj_mod
        self.date = None
        self.comments = ""
        self.path_model = os.path.join(os.path.dirname(__file__), "..", "mascaret")
        self.run = ""
        self.scen = ""
        self.basename = "mascaret"
        self.complet = False
        self.box = ClassWarningBox()
        self.cls_res = ClassGetResults(self.mdb, dbg=self.mgis.DEBUG)

        self.ui = loadUi(os.path.join(os.path.dirname(__file__), "..", "ui/ui_import_res.ui"), self)
        self.txt_path.setText(".")

        self.buttonBox.accepted.connect(self.accept_dialog)
        self.buttonBox.rejected.connect(self.reject)

        self.checkBox.setChecked(False)
        self.mDateTimeEdit.setEnabled(False)
        self.checkBox.stateChanged.connect(self.act_date)
        # self.mDateTimeEdit.dateChanged.connect(self.change_date)
        self.txt_path.setText(self.path_model)
        self.txt_base_name.setText(self.basename)
        self.bt_path.clicked.connect(self.path_search)
        self.txt_path.textChanged["QString"].connect(self.path_change)
        self.ed_scen.textChanged["QString"].connect(self.change_scenario)
        self.ed_run.textChanged["QString"].connect(self.change_run)
        self.txt_base_name.textChanged["QString"].connect(self.change_basename)

    def change_basename(self):
        basen = str(self.ed_run.text())
        self.basename = basen.replace("'", " ").replace('"', " ").strip()
        self.txt_path.setText(self.basename)

    def change_run(self):
        run = str(self.ed_run.text())
        self.run = run.replace("'", " ").replace('"', " ").strip()
        self.ed_run.setText(self.run)

    def change_scenario(self):
        scen = str(self.ed_scen.text())
        self.scen = scen.replace("'", " ").replace('"', " ").strip()
        self.ed_scen.setText(self.scen)

    def act_date(self):
        act_val = bool(self.checkBox.isChecked())
        if act_val:
            self.mDateTimeEdit.setEnabled(True)
        else:
            self.mDateTimeEdit.setEnabled(False)

    def accept_dialog(self):
        """validation dialog function"""
        if bool(self.checkBox.isChecked()):
            date_time_str = self.mDateTimeEdit.dateTime().toString("yyyy-MM-dd HH:mm:ss.zz")
            self.date = datetime.datetime.strptime(date_time_str, "%Y-%m-%d %H:%M:%S.%f")
        else:
            self.date = None
        self.comments = str(self.textEdit_com.toPlainText())

        if self.scen.strip() == "" or self.run.strip() == "":
            msg = "Indicate the run and scenario names."
            self.box.info(msg, "Error")
            return

        if self.obj_mod.check_scenar(self.scen, self.run):
            if self.import_result():
                QDialog.accept(self)

    def path_search(self):
        """search path windows"""

        path = QFileDialog.getExistingDirectory(self, "Choose a folder", self.path_model)
        if path:
            self.path_model = path
            self.txt_path.setText(path)

    def path_change(self, text):
        """change path"""
        if os.path.isdir(text):
            self.path_model = text
            self.txt_path.setText(text)

    def import_result(self):

        has_links, has_basins, has_tracer = self.cls_res._check_result_files(self.path_model)
        if not self.mdb:
            msg = "No database object."
            self.box.info(msg, "Error")
            return False

        id_run = self.insert_id_run(self.mdb, self.run, self.scen)

        self.cls_res.set_results_database(
            self.path_model,
            id_run,
            self.date,
            self.basename,
            comments=self.comments,
            tracer=has_tracer,
            casier=has_basins,
        )

        if self.cls_res.mess.get_critic_status():
            txt = self.cls_res.mess.message()
            self.box.info(txt, "Error")
            return False

        return True

    def insert_id_run(self, mdb, run_, scen):
        """
        creation run line in runs table
        :param run_: run name
        :param scen: scenario name
        :return (id_run): run id
        """
        maintenant = datetime.datetime.now()
        tab = {run_: {"scenario": scen, "date": "{:%Y-%m-%d %H:%M}".format(maintenant)}}
        listimport = ["run", "scenario", "date"]
        mdb.insert("runs", tab, listimport)
        # info = mdb.select(
        #     "runs",
        #     where="run = %s AND scenario = %s",
        #     list_var=["id"],
        #     params=[run_, scen],
        # )
        # print(info)
