plugins_name = "主图生成与上传"
from UI.auto_deal_pics_ui.show_process import Ui_Form as show_process_Ui_Form
from UI.auto_deal_pics_ui.auto_deal_pics_ui import Ui_Form as MainUi
from UI.auto_deal_pics_ui.matching_photos import Ui_Form as matching_photos_Ui_Form






from functools import partial
from module.view_control.MineQWidget import MineQWidget, DialogShow


from module.view_control.generate_main_image.grenerate_main_image_test import GeneratePic
from module.view_control.matching_photos.data import DataModeMatchPhoto
from collections import defaultdict



import settings
from module.view_control.generate_goods_no_detail_pic import detail_func
import json
from module.base_mode.base import *
from import_qt_mode import *
import threading
import concurrent.futures
from module.view_control.generate_goods_no_detail_pic.data import DataModeGenerateDetail, DataModeUploadPic
from module.view_control.generate_goods_no_detail_pic.detail_func import create_folder
import time
from PIL import Image
from io import BytesIO
import os, re
from func_timeout import FunctionTimedOut

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.plugins_base_func import *
else:
    from plugins_mode.plugins_base_func import *

_Type = ['.png', '.PNG', '.jpg', '.JPG', '.gif', '.GIF', ".jpge", ".JPGE"]


# 主程序入口
class AutoDealPics1(MineQWidget):

    def __init__(self, windows=None):
        super().__init__()
        self.windows = windows
        self.ui = MainUi()
        self.ui.setupUi(self)
        self.setWindowTitle("图片处理（插件）")
        self.resize(618, 651)
        # 线程锁
        self.lock = Lock()
        self.init()
        self.show()

    def init(self):
        self.show_process = ShowProcess(self)
        self.ui.main_stackedWidget.addWidget(self.show_process)
        self.show_process.back_sign.connect(self.to_back)
        self.show_process.stop_sign.connect(self.to_stop)

        # 匹配图片页面
        self.show_matching_pic = MatchingPhotos(self)
        self.ui.main_stackedWidget.addWidget(self.show_matching_pic)

        # 抠图与生成详情页
        self.show_generate_detail_pic = GenerateDetailPic(self, top_windows=self.windows)
        self.ui.main_stackedWidget.addWidget(self.show_generate_detail_pic)

        self.show_generate_detail_pic.text_show.connect(self.show_progress_detail)
        self.show_generate_detail_pic.progress_show_sign.connect(self.switch_to_process_ui)
        self.show_generate_detail_pic.progress_end_sign.connect(self.end_process)

        # 开始抠图生成主图处理
        self.show_matching_pic.run_cutout_sign.connect(self.switch_to_detail_ui)
        self.ui.main_stackedWidget.setCurrentIndex(1)

    def to_back(self, *args):
        self.switch_to_detail_ui()

    def to_stop(self):
        self.show_generate_detail_pic.to_stop()

    def switch_to_process_ui(self, flag):
        if flag:
            self.show_process.clearn()
            self.ui.main_stackedWidget.setCurrentIndex(0)
        else:
            self.ui.main_stackedWidget.setCurrentIndex(2)

    def end_process(self, *args):
        self.show_process.is_end()

    def show_progress_detail(self, text):
        self.show_process.show_progress_detail(text)

    # 页面切换到详情页生成的窗口
    def switch_to_detail_ui(self, data=None):
        if data:
            folder_path = data["folder_path"]
            self.show_generate_detail_pic.change_img_dir_by_text(folder_path)
        self.ui.main_stackedWidget.setCurrentIndex(2)


class ShowProcess(QWidget):
    back_sign = Signal()
    stop_sign = Signal()

    def __init__(self, parent=None, windows=None):
        super().__init__(parent)
        self.windows = windows
        self.ui = show_process_Ui_Form()
        self.ui.setupUi(self)
        self.ui.back.clicked.connect(self.back_sign.emit)
        self.ui.stop.clicked.connect(self.stop_sign.emit)
        self.ui.progressBar.hide()
        self.show()

    def show_progress_detail(self, text):
        self.ui.textBrowser_4.append(text)

    def is_end(self):
        self.ui.back.show()
        self.ui.stop.hide()

    def clearn(self):
        self.ui.progressBar.setValue(0)
        self.ui.textBrowser_4.clear()
        self.ui.back.hide()
        self.ui.stop.show()












