from models import *
import datetime
from services.SegmentService import SegmentService
from services.deal_cutout import DealCutout, DealCloths,DealModelForm
from services.other.module_online_data import GetOnlineData


@app.get("/")
async def index():
    """入口文件"""
    return {
        "message": "请求成功",
        "data": f"Hello, World!",
    }


@app.post("/api/check_select_images", description="检查目录或文件,生成可执行的文件结构")
async def checkSelect(params: CheckSelectImages):
    service = SegmentService()
    file_path = params.path
    path_type = params.path_type
    image_list = params.image_list
    need_cutout_images = []
    if path_type == 0:
        need_cutout_images = service.initImages(
            image_list=image_list, need_cutout_images=need_cutout_images
        )
    else:
        need_cutout_images = service.check_need_cutout_images(
            file_path, need_cutout_images
        )
    # print("need_cutout_images",need_cutout_images)
    #
    if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
        raise UnicornException("您所选文件夹下没有jpg/png图片，或对应图片已扣图")
    return success(need_cutout_images)

@app.post("/api/segment_images", description="执行抠图操作")
async def segmentImages(params: SegmentImages):
    token = params.token
    if token is None:
        raise UnicornException("token不能为空")
    image_type = params.image_type
    segment_type = params.segment_type
    output_type = params.output_type
    need_cutout_images = params.need_cutout_images
    result = None
    if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
        raise UnicornException("您所选文件夹下没有jpg/png图片，或对应图片已扣图")
    if image_type == 1 and segment_type == 1:
        raise UnicornException("服装暂不支持精细化抠图")
    elif image_type == 1 and segment_type == 0:
        deal_cutout_cloth = DealCloths(token)
        # 服装抠图
        # need_cutout_images = service.check_need_cutout_images(file_path)
        deal_cutout_cloth.need_cutout_images = need_cutout_images
        deal_cutout_cloth.output_type = output_type
        result, save_root_path = deal_cutout_cloth.startDispose()
    elif image_type == 0 and segment_type == 1:
        deal_cutout_mode = DealCutout(token)
        # 通用-精细化抠图
        deal_cutout_mode.need_cutout_images = need_cutout_images
        deal_cutout_mode.output_type = output_type
        result, save_root_path = deal_cutout_mode.startDispose()
    elif image_type == 0 and segment_type == 0:
        # 通用-普通抠图
        deal_cutout_mode = DealCutout(token)
        deal_cutout_mode.need_cutout_images = need_cutout_images
        deal_cutout_mode.output_type = output_type
        result, save_root_path = deal_cutout_mode.normalMode()
    return success({"result": result, "save_root_path": save_root_path})

@app.post("/api/model_form_segment", description="人台抠图")
def model_form_segment(params:ModelFormModel):
    need_cutout_images = params.need_cutout_images
    if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
        raise UnicornException("您所选文件夹下没有jpg/png图片，或对应图片已扣图")
    if len([x for x in need_cutout_images if x["need_cutout"]]) > 30:
        raise UnicornException("最多允许上传30张图片")
    modelFormClazz = DealModelForm(token=params.token,params=params)
    modelFormClazz.need_cutout_images = need_cutout_images
    result ,save_root_path,generate_ids=modelFormClazz.startDispose()
    return success({"result": result, "save_root_path": save_root_path,"generate_ids":generate_ids})
@app.post("/api/search_bacth_progress", description="人台抠图")
def search_bacth_progress(params:SearchProgress):
    modelFormClazz = DealModelForm(token=params.token,params=params)
    is_finished,successCount,failCount,root_path = modelFormClazz.search_progress()
    return success({"is_finished": is_finished,"success_count": successCount,"fail_count": failCount,"save_root_path":root_path})
@app.post("/api/request_hlm", description="请求查询可用余额")
async def requestHlm(params: RequestHlm):
    token = params.token
    if token is None:
        raise UnicornException("token不能为空")
    onlineData = GetOnlineData(token=token)
    result = onlineData.get_cutout_image_times()
    if result == False:
        raise UnicornException("请求失败")
    return success({"result": result})
