#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：trinity_client
@File    ：service_att_recognition.py
@Author  ：周成融
@Date    ：2023/7/26 12:01:05
@detail  ：
"""
import json
import logging
import time

import numpy as np
import redis
import tritonclient.grpc as grpcclient

from app.core.config import *
from app.schemas.generate_image import GenerateMultiViewModel
from app.service.generate_image.utils.mq import publish_status
from app.service.generate_image.utils.upload_sd_image import upload_png_sd
from app.service.utils.oss_client import oss_get_image

logger = logging.getLogger()


class GenerateMultiView:
    def __init__(self, request_data):
        self.grpc_client = grpcclient.InferenceServerClient(url=GMV_MODEL_URL)
        self.redis_client = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB, decode_responses=True)
        self.image = self.get_image(request_data.image_url)
        self.tasks_id = request_data.tasks_id
        self.user_id = self.tasks_id[self.tasks_id.rfind('-') + 1:]
        self.generate_data = {'tasks_id': self.tasks_id, 'status': 'PENDING', 'message': "pending", 'image_url': ''}
        self.redis_client.set(self.tasks_id, json.dumps(self.generate_data))
        self.redis_client.expire(self.tasks_id, 600)

    def get_image(self, image_url):
        try:
            image = oss_get_image(bucket=image_url.split('/')[0], object_name=image_url[image_url.find('/') + 1:], data_type="PIL")
            return image
        except Exception as e:
            logger.error(e)

    def callback(self, result, error):
        if error:
            self.generate_data['status'] = "FAILURE"
            self.generate_data['message'] = str(error)
            self.redis_client.set(self.tasks_id, json.dumps(self.generate_data))
        else:
            # pil图像转成numpy数组
            images = result.as_numpy("generated_image")
            image_url = upload_png_sd(images[6], user_id=self.user_id, category="multi_view", file_name=f"{self.tasks_id}.png")
            self.generate_data['status'] = "SUCCESS"
            self.generate_data['message'] = "success"
            self.generate_data['image_url'] = str(image_url)
            self.redis_client.set(self.tasks_id, json.dumps(self.generate_data))

    def read_tasks_status(self):
        status_data = self.redis_client.get(self.tasks_id)
        return json.loads(status_data), status_data

    def get_result(self):
        try:
            images = [np.array(self.image).astype(np.uint8)] * 1

            image_obj = np.array(images, dtype=np.uint8)

            input_image = grpcclient.InferInput("input_image", image_obj.shape, "UINT8")

            input_image.set_data_from_numpy(image_obj)

            inputs = [input_image]
            ctx = self.grpc_client.async_infer(model_name=GMV_MODEL_NAME, inputs=inputs, callback=self.callback)

            time_out = 600
            generate_data = None
            while time_out > 0:
                generate_data, _ = self.read_tasks_status()
                if generate_data['status'] in ["REVOKED", "FAILURE"]:
                    ctx.cancel()
                    break
                elif generate_data['status'] == "SUCCESS":
                    break
                time_out -= 1
                time.sleep(0.1)
            return generate_data
        except Exception as e:
            self.generate_data['status'] = "FAILURE"
            self.generate_data['message'] = str(e)
            self.redis_client.set(self.tasks_id, json.dumps(self.generate_data))
            raise Exception(str(e))
        finally:
            dict_generate_data, str_generate_data = self.read_tasks_status()
            if not DEBUG:
                publish_status(str_generate_data, GMV_RABBITMQ_QUEUES)


def infer_cancel(tasks_id):
    redis_client = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB, decode_responses=True)
    data = {'tasks_id': tasks_id, 'status': 'REVOKED', 'message': "revoked", 'data': 'revoked'}
    generate_data = json.dumps(data)
    redis_client.set(tasks_id, generate_data)
    return data


if __name__ == '__main__':
    rd = GenerateMultiViewModel(
        tasks_id="123-89",
        image_url="aida-sys-image/images/female/outwear/0628000123.jpg",
    )
    server = GenerateMultiView(rd)
    print(server.get_result())
