import base64
from dataclasses import dataclass
from typing import Optional

import cv2
import numpy as np
import requests

url = 'http://192.168.199.249:18080'


def send_request(image_path, image_type):
    with open(image_path, 'rb') as f:
        img_str: str = base64.encodebytes(f.read()).decode('utf-8')
        r = requests.post(f'{url}/ocr_system/idcard', json={'image': img_str, 'image_type': image_type})
        print(r.json())
        return r.json()


def send_request_cv2(image_path, image_type, rotate=None):
    img = cv2.imread(image_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    if rotate is not None:
        img = cv2.rotate(img, rotate)
    _, img = cv2.imencode('.jpg', img)
    img_str = base64.b64encode(img).decode('utf-8')
    r = requests.post(f'{url}/ocr_system/idcard', json={'image': img_str, 'image_type': image_type})
    print(r.json())
    return r.json()

@dataclass
class ResultItem:
    # status: str
    orientation: int
    name: str
    id: str
    ethnicity: str
    gender: str
    birthday: str
    address: str
    card_type: Optional[str]
    address_province: Optional[str]
    address_city: Optional[str]
    address_region: Optional[str]
    address_detail: Optional[str]
    expire_date: Optional[str]