from aiohttp import web

from snek.system.view import BaseFormView


class ContainersIndexView(BaseFormView):

    login_required = True

    async def get(self):

        user_uid = self.session.get("uid")

        containers = []
        async for container in self.services.container.find(user_uid=user_uid):
            containers.append(container.record)

        user = await self.services.user.get(uid=self.session.get("uid"))

        return await self.render_template(
            "settings/containers/index.html", {"containers": containers, "user": user}
        )


class ContainersCreateView(BaseFormView):

    login_required = True

    async def get(self):

        return await self.render_template("settings/containers/create.html")

    async def post(self):
        data = await self.request.post()
        await self.services.container.create(
            user_uid=self.session.get("uid"),
            name=data["name"],
            status=data["status"],
            resources=data.get("resources", ""),
            path=data.get("path", ""),
            readonly=bool(data.get("readonly", False)),
        )
        return web.HTTPFound("/settings/containers/index.html")


class ContainersUpdateView(BaseFormView):

    login_required = True

    async def get(self):

        container = await self.services.container.get(
            user_uid=self.session.get("uid"), uid=self.request.match_info["uid"]
        )
        if not container:
            return web.HTTPNotFound()
        return await self.render_template(
            "settings/containers/update.html", {"container": container.record}
        )

    async def post(self):
        data = await self.request.post()
        container = await self.services.container.get(
            user_uid=self.session.get("uid"), uid=self.request.match_info["uid"]
        )
        container["status"] = data["status"]
        container["resources"] = data.get("resources", "")
        container["path"] = data.get("path", "")
        container["readonly"] = bool(data.get("readonly", False))
        await self.services.container.save(container)
        return web.HTTPFound("/settings/containers/index.html")


class ContainersDeleteView(BaseFormView):

    login_required = True

    async def get(self):

        container = await self.services.container.get(
            user_uid=self.session.get("uid"), uid=self.request.match_info["uid"]
        )
        if not container:
            return web.HTTPNotFound()

        return await self.render_template(
            "settings/containers/delete.html", {"container": container.record}
        )

    async def post(self):
        user_uid = self.session.get("uid")
        uid = self.request.match_info["uid"]
        container = await self.services.container.get(user_uid=user_uid, uid=uid)
        if not container:
            return web.HTTPNotFound()
        await self.services.container.delete(user_uid=user_uid, uid=uid)
        return web.HTTPFound("/settings/containers/index.html")
