import os
from struct import iter_unpack

import pytest

from extract import extract_file, grouper
from font import FontFile#, draw_string
from main import MainExe
from piv import PivFile
#from sprite import SpriteSheetFile


def read_file(file_name, file_type):
    file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                             file_name)

    with open(file_path, 'rb') as f:
        file_data = f.read()
    return file_type(file_data)


@pytest.fixture
def mindscape():
    return read_file('MINDSCAP', PivFile)


class TestPivFile(object):
    def test_extract_piv_file(self, mindscape):
        extracted = extract_file(mindscape.file_length, mindscape.pixel_data)
        with open('mindscap_extract_1.bin', 'rb') as f:
            test_data = f.read()

        assert extracted == test_data

    def test_extract_pixels(self, mindscape):
        '''The original code would extract the code into video memory into the
        four memory blocks in unchained mode, this tests recreates those blocks
        '''
        block_a = []
        block_b = []
        block_c = []
        block_d = []

        extracted = mindscape.extract_pixels()

        for a, b, c, d in grouper(extracted, 4):
            block_a.append(a)
            block_b.append(b)
            block_c.append(c)
            block_d.append(d)

        blocks = block_a + block_b + block_c + block_d
        with open('mindscap_video_mem.bin', 'rb') as f:
            test_data = f.read()

        assert bytes(blocks) == test_data

    def test_extract_palette(self, mindscape):
        extracted = mindscape.extract_palette()

        with open('mindscap_palette_extract_1.bin', 'rb') as f:
            test_data = f.read()

        assert extracted == [i[0] for i in iter_unpack('<H', test_data)]


@pytest.fixture
def loading_screen():
    return read_file('CH.PIV', PivFile)


@pytest.fixture
def bold_font():
    return read_file('BOLD.F', FontFile)


@pytest.fixture
def main_exe():
    return MainExe(file_path=os.path.join(
        os.path.dirname(os.path.realpath(__file__)), 'MAIN.EXE'))


# class TestLoadingScreen(object):
#     def test_loading_screen(self, loading_screen, bold_font, main_exe):
#         bold_font.extract_subimage(loading_screen, 0x49, 0x5, 0x14)
#         bold_font.extract_subimage(loading_screen, 0x4a, 0x16, 0xb5)
#         bold_font.extract_subimage(loading_screen, 0x4b, 0x6e, 0xbe)
#         bold_font.extract_subimage(loading_screen, 0x45, 0x6e, 0xbe)

#         for string, metadata in main_exe.strings.items():
#             draw_string(loading_screen, bold_font, string, metadata[2],
#                         main_exe)

#         with open('loading_screen.bin', 'rb') as f:
#             test_data = f.read()

#         assert bytes(loading_screen.pixels) == test_data


# def test_sprite():
#     test = read_file('BOLD.F', SpriteSheetFile)
#     import pdb; pdb.set_trace()


@@ -50,16 +50,30 @@
 pytest%0A%0A
+import assets%0A
 from ext
@@ -233,16 +233,18 @@
 tFile%0A%0A%0A
+# 
 def read
@@ -268,24 +268,26 @@
 file_type):%0A
+# 
     file_pat
@@ -348,16 +348,17 @@
 le__)),%0A
+#
         
@@ -374,24 +374,25 @@
             
+ 
 file_name)%0A%0A
@@ -391,16 +391,18 @@
 _name)%0A%0A
+# 
     with
@@ -430,24 +430,25 @@
 ) as f:%0A
+#
         
 file_dat
@@ -435,24 +435,25 @@
 f:%0A#        
+ 
 file_data = 
@@ -461,16 +461,18 @@
 .read()%0A
+# 
     retu
@@ -501,85 +501,8 @@
 )%0A%0A%0A
-@pytest.fixture%0Adef mindscape():%0A    return read_file('MINDSCAP', PivFile)%0A%0A%0A
 clas
@@ -554,26 +554,73 @@
 iv_file(self
-, 
+):%0A        mindscape = assets.files.backgrounds%5B'
 mindscape):%0A
@@ -608,34 +608,34 @@
 ounds%5B'mindscape
-):
+'%5D
 %0A        extract
@@ -854,35 +854,24 @@
 _pixels(self
-, mindscape
 ):%0A        '
@@ -1129,32 +1129,58 @@
     extracted = 
+assets.files.backgrounds%5B'
 mindscape.extrac
@@ -1164,32 +1164,34 @@
 ounds%5B'mindscape
+'%5D
 .extract_pixels(
@@ -1584,27 +1584,16 @@
 tte(self
-, mindscape
 ):%0A     
@@ -1607,16 +1607,42 @@
 acted = 
+assets.files.backgrounds%5B'
 mindscap
@@ -1642,16 +1642,18 @@
 indscape
+'%5D
 .extract
@@ -1837,16 +1837,18 @@
 ata)%5D%0A%0A%0A
+# 
 @pytest.
@@ -1851,24 +1851,26 @@
 est.fixture%0A
+# 
 def loading_
@@ -1875,28 +1875,30 @@
 g_screen():%0A
+#
     
+ 
 return read_
@@ -1915,32 +1915,34 @@
 IV', PivFile)%0A%0A%0A
+# 
 @pytest.fixture%0A
@@ -1937,24 +1937,26 @@
 est.fixture%0A
+# 
 def bold_fon
@@ -1956,24 +1956,26 @@
 old_font():%0A
+# 
     return r
@@ -2005,16 +2005,18 @@
 File)%0A%0A%0A
+# 
 @pytest.
@@ -2023,16 +2023,18 @@
 fixture%0A
+# 
 def main
@@ -2041,16 +2041,18 @@
 _exe():%0A
+# 
     retu
@@ -2086,16 +2086,18 @@
 h.join(%0A
+# 
         

update tests to use assets from settings