# Written by retoor@molodetz.nl

# This code defines a WebView class that inherits from BaseView and includes a method for rendering a web template, requiring login access for its usage.

# The code imports the BaseView class from the `snek.system.view` module.

# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from aiohttp import web
from snek.system.view import BaseView

class WebView(BaseView):
    login_required = True

    async def get(self):
        if self.login_required and not self.session.get("logged_in"):
            return web.HTTPFound("/")
        channel = await self.services.channel.get(uid=self.request.match_info.get("channel"))
        if not channel:
            user = await self.services.user.get(uid=self.request.match_info.get("channel"))
            if user:
                channel = await self.services.channel.get_dm(self.session.get("uid"), user["uid"])
                if channel:
                    return web.HTTPFound("/channel/{}.html".format(channel["uid"]))
        if not channel:
            return web.HTTPNotFound()
        user = await self.services.user.get(uid=self.session.get("uid"))
        messages = [await self.app.services.channel_message.to_extended_dict(message) for message in await self.app.services.channel_message.offset(
            channel["uid"]
        )]
        channels = []
        async for subscribed_channel in self.app.services.channel_member.find(user_uid=self.session.get("uid"), deleted_at=None, is_banned=False):
            item = {}
            other_user = await self.app.services.channel_member.get_other_dm_user(subscribed_channel["channel_uid"], self.session.get("uid"))
            if other_user:
                item["name"] = other_user["nick"]
                item["uid"] = other_user["uid"]
            else:
                item["name"] = subscribed_channel["label"]
                item["uid"] = subscribed_channel["channel_uid"]
            channels.append(item)
        return await self.render_template("web.html", {"channel": channel,"user": user,"messages": messages , "channels": channels})