//
//  YHAIPictureMessageCell.swift
//  galaxy
//
//  Created by Dufet on 2024/12/3.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit
import SDWebImage

class YHAIPictureMessageCell: UITableViewCell {

    static let cellReuseIdentifier = "YHAIPictureMessageCell"
    var updateBlock: (()->())?
    var imgInfo = YHAIImageInfo() {
        didSet {
            
            if imgInfo.imageType == YHAIImageType.url.rawValue {
                
                // 尝试从缓存中获取图片
                if let cachedImage = SDImageCache.shared.imageFromCache(forKey: imgInfo.imageUrl) {
                    // 如果缓存中有图片，则直接使用
                    imgView.image = cachedImage
                    let size = self.getShowSize(image: cachedImage)
                    self.imgView.snp.updateConstraints { make in
                        make.width.equalTo(size.width)
                        make.height.equalTo(size.height)
                    }

                } else {
                    imgView.sd_setImage(with: URL(string: imgInfo.imageUrl), placeholderImage: UIImage(named: "global_default_image"), options: [], completed: { (image, error, cacheType, url) in
                        self.updateBlock?()
                    })
                }
                
            } else if imgInfo.imageType == YHAIImageType.local.rawValue {
                if let img = UIImage(named: imgInfo.localImageName) {
                    imgView.image = img
                    self.imgView.snp.updateConstraints { make in
                        make.width.equalTo(img.size.width)
                        make.height.equalTo(img.size.height)
                    }
                } else {
                    imgView.image = UIImage(named: "global_default_image")
                    self.imgView.snp.updateConstraints { make in
                        make.width.equalTo(200)
                        make.height.equalTo(200)
                    }
                }
            }
            
            self.setNeedsLayout()
            self.layoutIfNeeded()
        }
    }
    
    func getShowSize(image: UIImage?) -> CGSize {
        
        let imgW = 220.0
        var imgH = 220.0
        var ratio = 1.0
        
        guard let img = image else {
            return CGSizeMake(imgW, imgH)
        }
        if img.size.width > 0, img.size.height > 0 {
            ratio = img.size.width/img.size.height
        }
        imgH = imgW/ratio
    
//        if imgH > 476 {
//            imgH = 476
//            imgW = imgH*ratio
//            
//        } else {
//            imgW = 220
//            imgH = imgW/ratio
//        }
        
        return CGSizeMake(imgW, imgH)
    }

    lazy var imgView: UIImageView = {
        let v = UIImageView(image: UIImage(named: "global_default_image"))
        return v
    }()
    
    lazy var whiteContentView: UIView = {
        let v = UIView()
        v.layer.cornerRadius = 12.0
        v.clipsToBounds = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(didMessageClicked))
        v.addGestureRecognizer(tap)
        return v
    }()
    
    lazy var shadowView: YHAIChatShadowView = {
        let v = YHAIChatShadowView()
        return v
    }()
    
    lazy var rightAngleView: UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.isHidden = true
        return v
    }()
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    func setupUI() {
        
        selectionStyle = .none
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        contentView.addSubview(shadowView)
        contentView.addSubview(rightAngleView)
        contentView.addSubview(whiteContentView)
        whiteContentView.addSubview(imgView)
        
        whiteContentView.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(20)
            make.bottom.equalTo(0)
        }
        
        imgView.snp.makeConstraints { make in
            make.width.equalTo(220)
            make.height.equalTo(220)
            make.edges.equalToSuperview()
        }
        
        shadowView.snp.makeConstraints { make in
            make.edges.equalTo(whiteContentView)
        }
        
        rightAngleView.snp.makeConstraints { make in
            make.top.left.equalTo(whiteContentView)
            make.width.height.equalTo(15)
        }
    }
    
    @objc func didMessageClicked() {
        self.endEditing(true)
        YHPictureReviewManager.shared.showNetWorkPicturs(curIndex: 0, arrPicturs: [imgInfo.imageUrl])

    }
}
