"""
步骤：
1、整理需要处理的款号图-输出款号图文件夹
2、整理所有相关的图片作为素材图
3、按要求进行拼接
"""

from ast import mod
import os
import settings

from PIL import ImageFont
import copy

# from module.view_control.generate_goods_no_detail_pic.detail_generate_base import DetailBase
# from module.view_control.generate_goods_no_detail_pic.pic_deal import PictureProcessing
is_test_plugins = False
try:
    is_test_plugins = settings.is_test_plugins
except:
    is_test_plugins = False

if is_test_plugins:
    from custom_plugins.plugins_mode.detail_generate_base import DetailBase
    from custom_plugins.plugins_mode.pic_deal import PictureProcessing
else:
    from plugins_mode.detail_generate_base import DetailBase
    from plugins_mode.pic_deal import PictureProcessing

plugins_name = "详情模板"
company_name_list = ["信诺"]
template_name = "xinnuo-3"
# "俯视", "侧视", "后跟", "鞋底", "内里" 组合，组合2

class DetailPicGet(DetailBase):
    need_view = ["俯视", "侧视", "后跟", "鞋底", "内里","组合","组合2","组合3","组合4","组合5"]
    root = r"{}\resources\detail_temp\xinnuo\3".format(os.getcwd())

    def __init__(
        self,
        goods_no,
        goods_no_value: dict,
        out_put_dir,
        windows=None,
        test=False,
        excel_data=None,
        assigned_page_list=None,
    ):
        super().__init__(
            goods_no,
            goods_no_value,
            out_put_dir,
            windows=windows,
            excel_data=excel_data,
            assigned_page_list=assigned_page_list,
        )
        self.template_name = template_name

        self.root = r"{}\resources\detail_temp\xinnuo\3".format(os.getcwd())
        print("run xinnuo-3 ")
        self.base_bg_color = (255, 255, 255)
        self.base_bg_color_2 = (244, 242, 243)
        self.deal_pic_func_list = [
            self.deal_pic_1,
            self.deal_pic_2,
            self.deal_pic_3,
            self.deal_pic_4,
            self.deal_pic_5,
            self.deal_pic_6,
        ]

        if test:
            # pp = self.generate_font_list_to_pic()
            # pp.im.save(r"C:\Users\gymmc\Desktop\细节图示例/字号.png")
            # for k, v in self.goods_no_value.items():
            #     print(k, v)
            self.run_test()
        else:
            self.run_all()

    def run_test(self):
        detailed_images = []
        detailed_images.append(self.deal_pic_1())
        detailed_images.append(self.deal_pic_2())
        detailed_images.append(self.deal_pic_3())
        detailed_images.append(self.deal_pic_4())
        detailed_images.append(self.deal_pic_5())
        detailed_images.append(self.deal_pic_6())
        img = self.add_pic(detailed_images)
        img.save(r"{}/{}.jpg".format(self.out_put_dir, self.goods_no, format="JPEG"))
        img.show()

    def deal_pic_1(self):
        pp_bg = PictureProcessing(r"{}\1.jpg".format(self.root),1600)

        goods_art_no_list = list(self.data.keys())
        pp_jpg_1, pp_png_1 = self.image_one_pic(
            return_orign=True,
            goods_art_no=goods_art_no_list[0],
            name="组合",
        )
        if pp_png_1 != None and pp_jpg_1 != None:
            pp_jpg_1 = pp_jpg_1.resize(value=pp_bg.width/1.1)
            pp_png_1 = pp_png_1.resize(value=pp_bg.width /1.1)

            if self.check_shoe_is_right_by_pixel(im=pp_png_1.im)==True:
                pp_jpg_1 = pp_jpg_1.transpose()
                pp_png_1 = pp_png_1.transpose()

            # pp_png_1_bg = PictureProcessing("RGBA", (pp_jpg_1.width*1.4, pp_jpg_1.width*1.4), (255,255,255,0))
            # pp_png_1_bg = pp_png_1_bg.paste_img(
            #     top_img=pp_png_1,base="nc", value=(0, 0)
            # )
            # pp_png_1_bg = pp_png_1_bg.rotate(0)
            #
            # pp_bg = pp_bg.paste_img(
            #     top_img=pp_png_1_bg,base="cc", value=(25, 340)
            # )

            pp_bg = pp_bg.to_overlay_pic_advance(
                mode="pixel",
                top_img=pp_jpg_1,
                base="cc",
                value=(25, 50),
                top_png_img=pp_png_1,
            )

        return pp_bg
    def deal_pic_2(self):
        pp_bg = PictureProcessing(r"{}\2.jpg".format(self.root), 1600)
        goods_art_no_list = list(self.data.keys())
        pp_jpg_1, pp_png_1 = self.image_one_pic(
            return_orign=True,
            goods_art_no=goods_art_no_list[0],
            name="俯视",
        )
        pp_jpg_1 = pp_jpg_1.resize(value=pp_bg.width*0.45)
        pp_png_1 = pp_png_1.resize(value=pp_bg.width*0.45)
        # pp_bg = pp_bg.paste_img(top_img=shoe_bg, value=(0, 0), base="nw")
        pp_bg = pp_bg.to_overlay_pic_advance(
            mode="pixel",
            top_img=pp_jpg_1,
            base="cc",
            value=(0, 70),
            top_png_img=pp_png_1,
        )
        font = ImageFont.truetype(
            r"resources\ttf\puhui\Medium.ttf", 24
        )  # 设计理念
        font_hei = ImageFont.truetype(r"resources\ttf\simhei.ttf", 24)  # 设计理念
        xiemian  = self.get_text_value("鞋面")
        xiedian = self.get_text_value("鞋垫")
        neili = self.get_text_value("内里")
        xiedi = self.get_text_value("鞋底")

        zhangkuan = self.get_text_value("掌宽")
        gengao = self.get_text_value("跟高")

        pp_bg.get_text_image_advanced(
            value=(264, 680),
            font=font,
            text=xiemian,
            align="left",
            # anchor="ma",
            spacing=5,
            fill=(52, 52, 52),
            return_mode="image",
            margins=(0, 0, 0, 0),
            max_len_one_line=6
        )
        pp_bg.get_text_image_advanced(
            value=(611, 680),
            font=font,
            text=neili,
            align="left",
            # anchor="ma",
            spacing=5,
            fill=(52, 52, 52),
            return_mode="image",
            margins=(0, 0, 0, 0),
            max_len_one_line=6
        )
        pp_bg.get_text_image_advanced(
            value=(611, 758),
            font=font,
            text=xiedi,
            align="left",
            # anchor="ma",
            spacing=5,
            fill=(52, 52, 52),
            return_mode="image",
            margins=(0, 0, 0, 0),
            max_len_one_line=6
        )
        pp_bg.get_text_image_advanced(
            value=(264, 758),
            font=font,
            text=xiedian,
            align="left",
            # anchor="ma",
            spacing=5,
            fill=(52, 52, 52),
            return_mode="image",
            margins=(0, 0, 0, 0),
            max_len_one_line=6
        )

        pp_bg.get_text_image_advanced(
            value=(212, 409),
            font=font_hei,
            text=f"掌宽\n{zhangkuan}",
            align="left",
            # anchor="ma",
            spacing=5,
            fill=(52, 52, 52),
            return_mode="image",
            margins=(0, 0, 0, 0),
        )

        pp_bg.get_text_image_advanced(
            value=(588, 469),
            font=font_hei,
            text=f"跟高\n{gengao}",
            align="left",
            # anchor="ma",
            spacing=5,
            fill=(52, 52, 52),
            return_mode="image",
            margins=(0, 0, 0, 0),
        )
        return pp_bg
    def deal_pic_3(self):
        detailed_images = []
        pp_bg = PictureProcessing(r"{}\3.jpg".format(self.root))
        goods_art_no_list = list(self.data.keys())
        all_color_name = []
        bg_color = (247, 247, 247)
        text_color = (52, 52, 52)
        font = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 25)
        goods_art_images = []
        total_height = 0
        for index, goods_art_no in enumerate(goods_art_no_list):
            pp_jpg,pp_png = self.image_one_pic(
                goods_art_no=goods_art_no,
                name="侧视",
                return_orign=True,
            )
            if pp_jpg is None:
                continue
            pp_jpg = pp_jpg.resize(value=pp_bg.width / 2.5)
            pp_png = pp_png.resize(value=pp_bg.width / 2.5)
            color_name = self.goods_no_value["货号资料"][index]["颜色名称"]
            all_color_name.append(color_name)
            text_bg = PictureProcessing("RGBA", (pp_jpg.width, 50), (255,255,255,0))
            text_bg = text_bg.get_text_image_advanced(
                value=(0, 0),
                font=font,
                text="{}".format(color_name),
                # text="{}".format(color_name),
                align="center",
                # anchor="mm",
                spacing=5,
                fill=text_color,
                return_mode="image",
                margins=(0, 0, 0, 0),
            )
            _bg = PictureProcessing(
                "RGB",
                (pp_bg.width / 2, pp_jpg.height+200),
                bg_color,
            )
            if (index+1) == 1:
                total_height += _bg.height
            if (index + 1) % 2 != 0 and (index + 1) != 1:
                total_height += _bg.height
            _bg = _bg.to_overlay_pic_advance(top_img=pp_jpg,top_png_img=pp_png, base="center")
            _bg = _bg.paste_img(
                top_img=text_bg, value=(pp_jpg.width / 2+10, pp_jpg.height+100)
            )
            goods_art_images.append(_bg)
        rows = 2
        shoes_bg = PictureProcessing(
        ).horizontal_distribution(
            pp_list=goods_art_images,
            bg_width=pp_bg.width,
            margins=(0, 0, 0, 0),
            line_spacing=0,
            number_per_row=rows,
        )
        main_bg = PictureProcessing(
            "RGB",
            (pp_bg.width, total_height),
            bg_color,
        )
        main_bg.paste_img(top_img=shoes_bg)
        detailed_images.append(main_bg)
        return PictureProcessing(im=self.add_pic(detailed_images))

        return pp_bg
    def deal_pic_4(self):
        pp_bg = PictureProcessing(r"{}\4.jpg".format(self.root))
        return pp_bg
    def deal_pic_5(self):
        detailed_images = []
        bg_image = PictureProcessing(r"{}\5.jpg".format(self.root))
        goods_art_no_list = list(self.data.keys())

        pp_jpg1, pp_png1 = self.image_one_pic(goods_art_no=goods_art_no_list[0], name="俯视", return_orign=True)
        ppg_width=bg_image.width*0.62
        ppg_height=bg_image.width*0.62*(329/481)
        pph=ppg_height*2
        ppw=(pp_png1.width/pp_png1.height)*pph
        pp_png1=pp_png1.resize(value=bg_image.width*1)
        pp_jpg1=pp_jpg1.resize(value=bg_image.width*1)
        pp_png1 = pp_png1.rotate(15)
        new_pp1_bg=PictureProcessing("RGB",(ppg_width,ppg_height),(255,255,255))

        new_pp1_bg=new_pp1_bg.paste_img(
            mode="pixel",top_img=pp_png1,base="sw",value=(0, 0)
        )

        bg_image=bg_image.paste_img(
            mode="pixel",top_img=new_pp1_bg,base="en",value=(20, 205)
        )

        pp_jpg1, pp_png1 = self.image_one_pic(goods_art_no=goods_art_no_list[0], name="组合5", return_orign=True)
        if pp_jpg1 != None or pp_png1 != None:
            ppg_width=bg_image.width*0.5
            ppg_height=bg_image.width*0.5*(329/428)
            pph=ppg_height*2
            # ppw=(pp_png1.width/pp_png1.height)*pph
            pp_png1=pp_png1.resize(value=bg_image.width)
            pp_jpg1=pp_jpg1.resize(value=bg_image.width)




            new_pp1_bg=PictureProcessing("RGB",(ppg_width,ppg_height),(200,200,200))

            new_pp1_bg=new_pp1_bg.paste_img(
                mode="pixel",top_img=pp_png1,base="cc",value=(0, 0)
            )

            bg_image=bg_image.paste_img(
                mode="pixel",top_img=new_pp1_bg,base="nw",value=(20, 205+366)
            )

        pp_png1, pp_jpg1 = self.image_one_pic(goods_art_no=goods_art_no_list[0], name="内里", return_orign=True)
        if pp_png1 != None and pp_jpg1 != None:
            ppg_width=bg_image.width*0.62
            ppg_height=bg_image.width*0.62*(329/481)
            pph=ppg_height*2
            # ppw=(pp_png1.width/pp_png1.height)*pph
            pp_png1=pp_png1.resize(value=bg_image.width*1)
            pp_jpg1=pp_jpg1.resize(value=bg_image.width*1)
            # pp_png1 = pp_png1.rotate(30)

            # if self.check_shoe_is_right_by_pixel(im=pp_png1.im)==True:
            pp_jpg1 = pp_jpg1.transpose()
            pp_png1 = pp_png1.transpose()


            temp_pp1_bg=PictureProcessing("RGB",(ppg_width*2,ppg_width*2),(255,255,255))
            temp_pp1_bg = temp_pp1_bg.paste_img(
                mode="pixel",top_img=pp_jpg1,base="wc",value=(0, 0)
            )
            temp_pp1_bg = temp_pp1_bg.rotate(60)



            new_pp1_bg=PictureProcessing("RGB",(ppg_width,ppg_height),(255,255,255))

            new_pp1_bg=new_pp1_bg.paste_img(
                mode="pixel",top_img=temp_pp1_bg,base="en",value=(-300, -300)
            )

            bg_image=bg_image.paste_img(
                mode="pixel",top_img=new_pp1_bg,base="en",value=(20, 205+366*2)
            )

        pp_jpg1, pp_png1 = self.image_one_pic(goods_art_no=goods_art_no_list[0], name="鞋底", return_orign=True)
        if pp_jpg1 != None and pp_png1 != None:
            ppg_width=bg_image.width*0.62
            ppg_height=bg_image.width*0.62*(329/481)
            # pph=ppg_height*2
            # ppw=(pp_png1.width/pp_png1.height)*pph
            pp_png1=pp_png1.resize(value=bg_image.width*1)
            pp_jpg1=pp_jpg1.resize(value=bg_image.width*1)


            temp_pp1_bg=PictureProcessing("RGB",(ppg_width*2,ppg_width*2),(255,255,255))
            temp_pp1_bg = temp_pp1_bg.paste_img(
                mode="pixel",top_img=pp_jpg1,base="wc",value=(0, 0)
            )
            temp_pp1_bg = temp_pp1_bg.rotate(-45)
            # pp_png1 = pp_png1.rotate(-45)
            new_pp1_bg=PictureProcessing("RGB",(ppg_width,ppg_height),(255,255,255))
            new_pp1_bg=new_pp1_bg.paste_img(
                mode="pixel",top_img=temp_pp1_bg,base="wc",value=(-120, 160)
            )

            # new_pp1_bg = new_pp1_bg.to_overlay_pic_advance(
            #     top_img=pp_jpg1, top_png_img=pp_png1, base="wc", value=(0, 0)
            # )

            bg_image=bg_image.paste_img(
                mode="pixel",top_img=new_pp1_bg,base="nw",value=(20, 205+366*3)
            )

        font_title = ImageFont.truetype(
            r"resources\ttf\puhui\Regular.ttf", 31
        )
        font_desc = ImageFont.truetype(
            r"resources\ttf\puhui\Regular.ttf", 20
        )
        text_list=[{'name':'细节1标题','align':'left','max_len':16,'default':'牛皮鞋面','value':(69, 281-10)},
                   {'name':'细节1内容','align':'left','max_len':16,'default':'质感真皮鞋面 柔韧有型','value':(69, 332-10)},
                   {'name':'细节2标题','align':'right','max_len':16,'default':'可调节式魔术贴','value':(500, 649-10)},
                   {'name':'细节2内容','align':'right','max_len':16,'default':'穿脱随心方便舒适','value':(562, 700-10)},
                   {'name':'细节3标题','align':'left','max_len':16,'default':'舒软鞋垫','value':(69, 1016-10)},
                   {'name':'细节3内容','align':'left','max_len':16,'default':'沙发感鞋垫 契合足型','value':(69, 1066-10)},
                   {'name':'细节4标题','align':'right','max_len':16,'default':'柔韧鞋底','value':(595, 1384-10)},
                   {'name':'细节4内容','align':'right','max_len':16,'default':'舒适耐穿 畅快出行','value':(555, 1434-10)}
                   ]

        for item in text_list:
            name = self.get_text_value(item['name'])
            if name== None:
                name = item['default']

            if '标题' in item['name']:
                bg_image.get_text_image_advanced(
                    value=item['value'],
                    font=font_title,
                    text=name,
                    align=item['align'],
                    # anchor="ma",
                    spacing=5,
                    fill=(0, 0, 0),
                    return_mode="image",
                    margins=(0, 0, 0, 0),
                    max_len_one_line=item['max_len']
                )
            else:
                bg_image.get_text_image_advanced(
                    value=item['value'],
                    font=font_desc,
                    text=name,
                    align=item['align'],
                    # anchor="ma",
                    spacing=5,
                    fill=(187, 187, 187),
                    return_mode="image",
                    margins=(0, 0, 0, 0),
                    max_len_one_line=item['max_len']
                )

        detailed_images.append(bg_image)
        return PictureProcessing(im=self.add_pic(detailed_images))

    def deal_pic_6(self):
        detailed_images = []
        image_path = r"{}\6.jpg".format(self.root)
        pp_bg = PictureProcessing(image_path,1600)
        goods_art_no_list = list(self.data.keys())

        font = ImageFont.truetype(
            r"resources\ttf\puhui\Regular.ttf", 18
        )

        # pp_bg_new = copy.deepcopy(pp_bg)
        bg_height=179
        pp_jpg_1, pp_png_1 = self.image_one_pic(
            return_orign=True,
            goods_art_no=goods_art_no_list[0],
            name='组合',
        )
        if pp_jpg_1 != None and pp_png_1 != None:
            # if self.check_shoe_is_right_by_pixel(im=pp_png_1.im):
            #     pp_jpg_1 = pp_jpg_1.transpose()
            #     pp_png_1 = pp_png_1.transpose()
            pp_jpg_1 = pp_jpg_1.resize(value=pp_bg.width / 1.7)
            pp_png_1 = pp_png_1.resize(value=pp_bg.width / 1.7)

            pp_bg = pp_bg.to_overlay_pic_advance(
                top_img=pp_jpg_1, top_png_img=pp_png_1, base="nc", value=(0, 270)
            )
            bg_height=683

            pp_bg.get_text_image_advanced(
                value=(310,610),
                font=font,
                text="正面45°/ FRONT45°",
                align="center",
                # anchor="ma",
                spacing=5,
                return_mode="image",
                margins=(0, 0, 0, 0),
                max_len_one_line=20
            )


        pp_jpg_1, pp_png_1 = self.image_one_pic(
            return_orign=True,
            goods_art_no=goods_art_no_list[0],
            name='组合2',
        )
        if pp_jpg_1 != None and pp_png_1 != None:
            # if self.check_shoe_is_right_by_pixel(im=pp_png_1.im):
            #     pp_jpg_1 = pp_jpg_1.transpose()
            #     pp_png_1 = pp_png_1.transpose()
            pp_jpg_1 = pp_jpg_1.resize(value=pp_bg.width / 1.7)
            pp_png_1 = pp_png_1.resize(value=pp_bg.width / 1.7)

            pp_bg = pp_bg.to_overlay_pic_advance(
                top_img=pp_jpg_1, top_png_img=pp_png_1, base="nc", value=(0, 720)
            )
            bg_height=1086
            pp_bg.get_text_image_advanced(
                value=(315,1020),
                font=font,
                text="侧面/ SIDE",
                align="center",
                # anchor="ma",
                spacing=5,
                return_mode="image",
                margins=(0, 0, 0, 0),
                max_len_one_line=20
            )

        pp_jpg_1, pp_png_1 = self.image_one_pic(
            return_orign=True,
            goods_art_no=goods_art_no_list[0],
            name='组合3',
        )
        if pp_jpg_1 != None and pp_png_1 != None:
            # if self.check_shoe_is_right_by_pixel(im=pp_png_1.im):
            #     pp_jpg_1 = pp_jpg_1.transpose()
            #     pp_png_1 = pp_png_1.transpose()
            pp_jpg_1 = pp_jpg_1.resize(value=pp_bg.width / 1.7)
            pp_png_1 = pp_png_1.resize(value=pp_bg.width / 1.7)

            pp_jpg_2 = pp_jpg_1.transpose()
            pp_png_2 = pp_png_1.transpose()

            pp_bg = pp_bg.to_overlay_pic_advance(
                top_img=pp_jpg_1, top_png_img=pp_png_1, base="nc", value=(0, 1150)
            )

            # pp_bg = pp_bg.to_overlay_pic_advance(
            #     top_img=pp_jpg_2, top_png_img=pp_png_2, base="nc", value=(80, 1150)
            # )

            bg_height=1600
            pp_bg.get_text_image_advanced(
                value=(350,1530),
                font=font,
                text="后跟/ BACK",
                align="center",
                # anchor="ma",
                spacing=5,
                return_mode="image",
                margins=(0, 0, 0, 0),
                max_len_one_line=20
            )

        pp_jpg_1, pp_png_1 = self.image_one_pic(
            return_orign=True,
            goods_art_no=goods_art_no_list[0],
            name='组合4',
        )
        if pp_jpg_1 != None and pp_png_1 != None:
            # if self.check_shoe_is_right_by_pixel(im=pp_png_1.im):
            #     pp_jpg_1 = pp_jpg_1.transpose()
            #     pp_png_1 = pp_png_1.transpose()
            pp_jpg_1 = pp_jpg_1.resize(value=pp_bg.width / 1.7)
            pp_png_1 = pp_png_1.resize(value=pp_bg.width / 1.7)

            pp_bg = pp_bg.to_overlay_pic_advance(
                top_img=pp_jpg_1, top_png_img=pp_png_1, base="nc", value=(0, 1750)
            )
            bg_height=2395
            pp_bg.get_text_image_advanced(
                value=(353,2270),
                font=font,
                text="组合/ COM",
                align="center",
                # anchor="ma",
                spacing=5,
                return_mode="image",
                margins=(0, 0, 0, 0),
                max_len_one_line=20
            )


        temp_pp1_bg=PictureProcessing("RGB",(pp_bg.width,bg_height),(255,255,255))


        temp_pp1_bg=temp_pp1_bg.paste_img(
            mode="pixel",top_img=pp_bg,base="nw",value=(0, 0)
        )

        detailed_images.append(temp_pp1_bg)

        return self.pp_pic_subsection(
            PictureProcessing(im=self.add_pic(detailed_images))
        )


if __name__ == "__main__":

    d = DetailPicGet(
        goods_no="test",
        goods_no_value={},
        out_put_dir="./output/xinnuo-3",
        test=True,
    )
    d.run_test()
