//
//  LNOrderShareImageGenerator.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/27.
//

import Foundation
import UIKit
import SnapKit


class LNOrderShareImageGenerator {
    private let container = UIImageView()
    
    private let avatar = UIImageView()
    private let userNameLabel = UILabel()
    private let scoreView = LNFiveStarScoreView()
    private let qrCodeView = UIImageView()
    private let costLabel = UILabel()
    
    private let skillIc = UIImageView()
    private let skillNameLabel = UILabel()
    private let countLabel = UILabel()
    
    init() {
        setupViews()
    }
    
    func update(skill: LNGameMateSkillVO, count: Int, image: UIImage) {
        avatar.showAvatar(myUserInfo.avatar)
        skillIc.sd_setImage(with: URL(string: skill.icon))
        skillNameLabel.text = skill.name
        scoreView.score = myUserInfo.star
        qrCodeView.image = image
        
        countLabel.text = .init(key: "A00162", count, skill.unit)
        
        let cost = skill.price * Double(count)
        costLabel.text = cost.toDisplay
        
        container.layoutIfNeeded()
    }
    
    func generate() -> UIImage {
        let renderer = UIGraphicsImageRenderer(bounds: container.bounds)
        
        return renderer.image { _ in
            container.drawHierarchy(
                in: container.bounds,
                afterScreenUpdates: true
            )
        }
    }
}

extension LNOrderShareImageGenerator {
    private func setupViews() {
        let image: UIImage = .icOrderShareBg
        container.image = image
        container.frame = .init(x: 0, y: 0, width: image.size.width, height: image.size.height)
        
        avatar.backgroundColor = .fill
        avatar.layer.cornerRadius = 37.5
        avatar.layer.borderWidth = 2
        avatar.layer.borderColor = UIColor.fill.cgColor
        avatar.clipsToBounds = true
        avatar.showAvatar(myUserInfo.avatar)
        container.addSubview(avatar)
        avatar.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(30)
            make.width.height.equalTo(75)
        }
        
        userNameLabel.font = .heading_h2
        userNameLabel.textColor = .text_5
        userNameLabel.text = myUserInfo.nickname
        container.addSubview(userNameLabel)
        userNameLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(avatar.snp.bottom).offset(2)
        }
        
        scoreView.icSize = 10
        scoreView.spacing = 4
        container.addSubview(scoreView)
        scoreView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(userNameLabel.snp.bottom).offset(2)
        }
        
        container.addSubview(qrCodeView)
        qrCodeView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(scoreView.snp.bottom).offset(13)
            make.width.height.equalTo(145)
        }
        
        let priceView = UIView()
        container.addSubview(priceView)
        priceView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(qrCodeView.snp.bottom).offset(6)
        }
        
        let coin = UIImageView.coinImageView()
        priceView.addSubview(coin)
        coin.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview()
            make.width.height.equalTo(20)
        }
        
        costLabel.font = .heading_h2
        costLabel.textColor = .text_5
        priceView.addSubview(costLabel)
        costLabel.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.trailing.equalToSuperview()
            make.leading.equalTo(coin.snp.trailing).offset(2)
        }
        
        let tipsView = UIImageView()
        tipsView.image = .primary_7
        tipsView.layer.cornerRadius = 14.5
        tipsView.clipsToBounds = true
        container.addSubview(tipsView)
        tipsView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(priceView.snp.bottom).offset(9)
        }
        
        let tipsLabel = UILabel()
        tipsLabel.text = .init(key: "A00164")
        tipsLabel.font = .heading_h4
        tipsLabel.textColor = .text_1
        tipsView.addSubview(tipsLabel)
        tipsLabel.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview().inset(6)
            make.horizontalEdges.equalToSuperview().inset(21)
        }
        
        skillIc.layer.cornerRadius = 10
        skillIc.clipsToBounds = true
        container.addSubview(skillIc)
        skillIc.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(56)
            make.bottom.equalToSuperview().offset(-88)
            make.width.height.equalTo(20)
        }
        
        skillNameLabel.font = .heading_h5
        skillNameLabel.textColor = .text_5
        container.addSubview(skillNameLabel)
        skillNameLabel.snp.makeConstraints { make in
            make.centerY.equalTo(skillIc)
            make.leading.equalTo(skillIc.snp.trailing).offset(8)
        }
        
        countLabel.font = .body_m
        countLabel.textColor = .text_5
        container.addSubview(countLabel)
        countLabel.snp.makeConstraints { make in
            make.centerY.equalTo(skillIc)
            make.trailing.equalToSuperview().offset(-58)
        }
    }
}
