# Mattermost Bot.
#  Copyright (c) 2016-2020 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
#  published under MIT-License

from inspect import cleandoc
import csv
import os


from AbstractCommand import *
class DialogManagedCoronaLVAFeedback(AbstractCommand):
    TRIGGER = "corona-lva-feedback"
    CONFIG = {"display_name": "somebot-command", "auto_complete": True,
              "auto_complete_hint": "",
             }
    USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "spams all LVA-Channels and asks for feedback on Corona changes. [BOT_ADMIN]"


    def __init__(self, team_id, datadir):
        super().__init__(team_id)

        self.datadir = datadir
        self.channels = dict()
        
        with open(self.datadir+'channels.csv', newline='') as csvfile:
            self.channels = {row[0]:row for row in csv.reader(csvfile, delimiter=',', quotechar='"')}


    def on_POST(self, request, data):
        self._require_bot_admin(data) # will throw an exception if not. (Dont try-except: Its handled up the stack.)

        msg = cleandoc("""
            ## @channel ``We want your Corona-related feedback for this course!``
            The Corona-crisis has led to many announcements of changes to courses.
            We are interested to find out which courses still need to improve to achieve the excellence we are accustomed to so we can follow-up on the issues that still persist.
            
            We know of the following announcements: https://wiki.fsinf.at/wiki/Course_reactions_to_coronavirus/(W)Inf
            If a course is missing, or outdated, please update the page, it helps us represent you!

            <dynamisch>
            This course announced:
            + lectures: 
            + excersises: 
            <dynamisch>
            This course was cancelled.
            <dynamisch>
            We do not have any data on this course's announcements. :(
            </dynamisch>
            """)

        att = [{
            "text": "##### How well were the announced changes executed so far? Are there still issues with this course?",
            "actions": [{"name": "Give/edit your feedback", "integration": {"url": self.URL+"/interactive"}}]
            }]

        print(self.URL)
        request.cmd_respond_text_chan(msg, {"attachments":att})


    def on_POST_interactive(self, request, data):
        #import pprint
        #print("on_POST_interactive")
        #pprint.pprint(data)

        request.respond(200, {"ephemeral_text": "## ``The submission period ended. We are now evaluating the responses!`` :)"})

#        filename_feedback = self.datadir+data["team_id"]+"-"+data["channel_id"]+"-"+data["user_id"]+"-feedback.txt"
#        if os.path.isfile(filename_feedback):
#            with open(filename_feedback, "r") as f:
#                feedback = f.read()
#        else:
#            feedback = ""
#
#        dialog = {
#            "callback_id": data["team_id"]+"-"+data["channel_id"]+"-"+data["user_id"],
#            "title": "Corona related course changes",
#            "submit_label":"Submit",
#            "elements":[{
#                "display_name": "Your Comment/Feedback (Max. 3000 characters)",
#                "placeholder": "Type here. Max. 3000 characters.",
#                "name": "feedback",
#                "type": "textarea",
#                "help_text": "We will not forward your feedback to teaching staff. You can edit/blank it later at any time. Your user_id is stored.",
#                "optional": True,
#                "default": feedback
#            }]
#        }

#        request.respond(200, {})
#        self.bot.api.open_dialog(data["trigger_id"], self.URL+"/dialog", dialog)


#    def on_POST_dialog(self, request, data):
#        #import pprint
#        #print("on_POST_dialog")
#        #pprint.pprint(data)
#
#        filename_feedback = self.datadir+data["callback_id"]+"-feedback.txt"
#        feedback = data["submission"]["feedback"].strip()
#        if feedback == "":
#            os.remove(filename_feedback)
#            self.bot.debug_chan("``corona-lva-feedback`` result deleted for ``"+self.channels[data["channel_id"]][6]+"``")
#        else:
#            with open(filename_feedback, "w") as f:
#                f.write(feedback)
#            self.bot.debug_chan("``corona-lva-feedback`` result stored for ``"+self.channels[data["channel_id"]][6]+"``\n```\n"+data["submission"]["feedback"]+"\n```")
#
#        request.respond(200, {})
