"""Post-composer OK-button + right-edge clamp FTs.

The #id_post_line_text composer at the bottom of a Billpost gains an OK
.btn-confirm to its right. The composer's right boundary is clamped so the OK
button clears the bottom-right corner button in each orientation:
  • portrait  — the gear (.post-page > .gear-btn, right:0.5rem, 3rem wide),
  • landscape — the burger slot (#id_burger_btn lands at right:4.2rem, 3rem
    wide → its left edge is 7.2rem from the viewport's right edge; clearing it
    also clears the bud at right:0.5rem). post.html itself carries no burger
    (room.html will), so the landscape test asserts the OK edge clears that
    7.2rem slot rather than a live element.

FT bucket: bill.
"""
from selenium.webdriver.common.by import By

from .base import FunctionalTest
from .post_page import PostPage


class PostComposerOkButtonTest(FunctionalTest):
    def _open_post_page(self):
        # Adding a line from /billboard/ navigates to the fresh post's page,
        # where the editable #id_post_line_text composer renders.
        self.create_pre_authenticated_session("disco@test.io")
        self.browser.get(self.live_server_url + "/billboard/")
        PostPage(self).add_post_line("How does this look")
        return self.wait_for(lambda: self.browser.find_element(
            By.CSS_SELECTOR, ".post-line-form button.btn-confirm"))

    def _rect(self, el, prop):
        return self.browser.execute_script(
            f"return arguments[0].getBoundingClientRect().{prop};", el)

    def test_ok_button_clears_gear_in_portrait(self):
        self.browser.set_window_size(800, 1200)
        ok = self._open_post_page()
        gear = self.browser.find_element(By.CSS_SELECTOR, ".post-page > .gear-btn")
        self.assertLessEqual(
            self._rect(ok, "right"), self._rect(gear, "left"),
            "post-composer OK button overlaps the gear in portrait")

    def test_ok_button_clears_burger_slot_in_landscape(self):
        self.browser.set_window_size(1400, 800)
        ok = self._open_post_page()
        rem = self.browser.execute_script(
            "return parseFloat(getComputedStyle(document.documentElement).fontSize);")
        vw = self.browser.execute_script("return window.innerWidth;")
        ok_right = self._rect(ok, "right")
        # OK's right edge must sit at least 7.2rem in from the viewport's right
        # edge — clearing where #id_burger_btn lands in landscape (and the bud).
        self.assertGreaterEqual(
            vw - ok_right, 7.2 * rem - 2,
            "post-composer OK button doesn't clear the landscape burger slot")
