class TestClient(unittest.TestCase):

    def __init__(self, *args, **kwargs):
        super(TestClient, self).__init__(*args, **kwargs)

        recording_id = 871
        frame_uuid = "44d25183-cc3e-4c8b-8661-8c286e71b2dc"
        host_url = "http://webplayer.horus.nu/"

        self.grid = Grid()
        self.request_builder = ImageRequestBuilder(recording_id, frame_uuid)
        self.stitcher = ImageProvider()
        self.client = Client(host_url)

    def request_stitched_sections(self, sections, scale, path=None):
        requests = self.client.fetch_all(
            self.request_builder.build(Mode.panoramic, scale, section) for section in sections)

        result = self.stitcher.combine(requests, scale.size, scale.size)
        if path:
            with open(path, 'wb') as f:
                f.write(result.image.getvalue())
            result.image.close()
        return result

    def test_client_request_pano(self):
        request = self.client.fetch(self.request_builder.build(Mode.panoramic, Scales.Px_1024))
        with open('./tests/data/pano.jpg', "wb") as file:
            file.write(request.result())

    def test_client_request(self):
        sections = self.grid.filter(h_min=-44, h_max=44, w_min=-170, w_max=-1)
        self.request_stitched_sections(
            sections, Scales.Px_1024, './tests/data/stitched.jpg')

    def test_client_request_pano_stitched(self):
        result = self.request_stitched_sections(
            self.grid, Scales.Px_1024, './tests/data/pano_stitched.jpg')
        self.assertEqual(result.to_pixel_coordinates((0, 0)).x, 0)
        self.assertEqual(result.fov, Rect(
            x=-180.0, y=-90.0, width=360.0, height=180.0))


    def test_client_request_single_section_stitched(self):
        result = self.request_stitched_sections(
            (self.grid[3],), Scales.Px_1024, './tests/data/single_section_stitched.jpg')
        self.assertEqual(result.to_pixel_coordinates((170.0, 0)).x, 796)
        self.assertEqual(result.fov, Rect(
            x=-45.0, y=45.0, width=45.0, height=45.0))

    def test_client_request_left_wrapped(self):
        sections = self.grid.filter(h_min=-46, h_max=1, w_min=-270, w_max=-46)
        result = self.request_stitched_sections(
            sections, Scales.Px_1024, './tests/data/stitched_lw.jpg')
        self.assertEqual(result.to_pixel_coordinates((0, 0)).x, 0)

    def test_client_request_right_wrapped(self):
        sections = self.grid.filter(h_min=0, w_min=70, w_max=246)
        result = self.request_stitched_sections(
            sections, Scales.Px_1024, './tests/data/stitched_rw.jpg')
        self.assertEqual(result.to_pixel_coordinates((70.0, 0)).x, 1592)
        self.assertEqual(result.fov, Rect(
            x=-180.0, y=0.0, width=225.0, height=90.0))

    def test_client_request_left_right_wrapped(self):
        sections = self.grid.filter(w_min=-240, w_max=246)
        result = self.request_stitched_sections(
            sections, Scales.Px_1024, './tests/data/stitched_lrw.jpg')
        self.assertEqual(result.to_pixel_coordinates((-240.0, 0)).x, 2730)
        self.assertEqual(result.fov, Rect(
            x=-180.0, y=-90.0, width=180.0, height=180.0))
        assert frame.recordingid in recording_id
    angle = compute_angle(frame, location)
    attempts = args.attempts
    while True:
        if (clipping_interval[0] <= angle <= clipping_interval[1]):
            requestBuilder = ImageRequestBuilder(frame.recordingid, frame.uuid)

            filename = None

            if mode == Mode.panoramic.name:
                fov = Box.create(center=(angle, 0),
                                 width=horizontal_fov,
                                 height=vertical_fov)
                sections = grid.filter(fov=fov)
                requests = client.fetch_all(
                    requestBuilder.build(mode, Scales.Px_1024, section)
                    for section in sections)
                result = image_provider.combine(requests, Scales.Px_1024.size,
                                                Scales.Px_1024.size)
                if temp_path:
                    filename = temp_path + \
                        "stitched_{}_{}.jpg".format(
                            frame.recordingid,  frame.index)
                results.append({
                    "frame":
                    frame,
                    "angle":
                    angle,
                    "pixel_coordinate":
                    result.to_pixel_coordinates((angle, 0)),
                    "stitch":
scale = Scales.Px_2048

# Get the image

request_builder = ImageRequestBuilder(frame.recordingid, frame.uuid)

#(min, max) yaw in deg
directions = {
    "front": (-45, 45),
    "back": (135, 225),
    "left": (-135, -45),
    "right": (45, 135),
}

grid = Grid()
for direction in directions:
    min_yaw = directions[direction][0]
    max_yaw = directions[direction][1]
    sections = grid.filter(h_min=-44, h_max=44, w_min=min_yaw, w_max=max_yaw)
    requests = client.fetch_all(
        request_builder.build(Mode.panoramic, scale, section)
        for section in sections)
    result = image_provider.combine(requests, scale.size, scale.size)

    # Save the file
    filename = ".\\panoramic_{}_{}.jpg".format(frame.index, direction)

    with open(filename, 'wb') as f:
        f.write(result.image.getvalue())
        result.image.close()