from rembg.bg import remove
import numpy as np
import io
from PIL import Image , ImageOps , ImageFile , ImageColor
ImageFile.LOAD_TRUNCATED_IMAGES = True
from utils.files_lib import get_resources_data_path , get_full_path
from .m_data_helper import BGHelper



class BGModifierApi:
    def __init__(self):
        pass

    def bg_remove(self,input_path,output_path, magic:BGHelper ):
        f = np.fromfile(input_path)
        print("np.from done")
        result = remove(f)
        print("remove(f) done")
        img = Image.open(io.BytesIO(result)).convert("RGBA")
        # img = Image.open(io.BytesIO(result)).convert("RGB")
        print("output image done")
        img.save(output_path)
        if magic.bg_type is not None:
            print("coming")
            self.create_bg_image(input_path=output_path,output_path=output_path,magic=magic)
        print("save done")

    def create_bg_image(self,input_path,output_path, magic:BGHelper):
        org_img_path = input_path
        type = magic.bg_type
        bg_src = "#FFFFFF"

        try:
            with Image.open(org_img_path) as im:
                # im.show()
                fg_w, fg_h = im.size
                if type == "bg_img":
                    print(*(magic.bg_src.split("/")))
                    #working bg_src = get_full_path("filter_resources",*(magic.bg_src.split("/")))
                    bg_src = get_full_path(*(magic.bg_src.split("/")))
                    with Image.open(bg_src) as bg_img:
                        bg_w, bg_h = bg_img.size

                        # offset = self.find_xy(bg_w, bg_h, fg_w, fg_h)
                        # if bg_w < fg_w:
                            # bg_img = bg_img.resize((fg_w, fg_h))

                        # New code to resize bg_image with output image
                        offset = (0,0)
                        bg_img = bg_img.resize((fg_w, fg_h))

                        # bg_img.show()
                        bg_img.paste(im=im, box=offset, mask=im)
                        # bg_img.show()
                        bg_img.save(output_path)

                elif type == "color":
                    print("color:",magic.bg_src)
                    bg_src = magic.bg_src
                    try:
                        with Image.new("RGB", (fg_w, fg_h), ImageColor.getrgb(bg_src)) as bg_img:
                            offset = (0, 0)
                            # bg_img.show()
                            bg_img.paste(im=im, box=offset, mask=im)
                            # bg_img.show()
                            bg_img.save(output_path)
                    except Exception as err:
                        print("Error",err)
                        pass
        except OSError as err:
            print("cannot create thumbnail for", "infile", err)
        # Show image
        # img.show()
        return

    def find_xy(self, bg_w, bg_h, fg_w, fg_h):
        offset = (0, 0)
        if bg_w > fg_w:
            try:
                offset = ((bg_w - fg_w) // 2, (bg_h - fg_h) // 2)
            except:
                pass

        return offset

    def add_border_to_image(self,input_path,output_path,border_width, border_color_value)-> str:
        ImageOps.expand(Image.open(input_path), border=border_width, fill=border_color_value).save(output_path)
        return output_path

