//
//  IMPhoneCallContentView.swift
//  LegendTeam
//
//  Created by dong on 2022/4/20.
//

import UIKit

class IMPhoneCallConfig: IMContentBaseConfig {
    override func contentSize(model: SessionBaseModel) -> CGSize {
        guard let message = model.v2msg else { return .zero }

        let contentView = IMPhoneCallContentView(frame: .zero)
        contentView.contentLabel.text = contentView.getContentTextWith(message: message)
        var size = contentView.contentLabel.sizeThatFits(CGSize(width: SessionBaseModel.maxBubbleContentWidth, height: CGFloat.greatestFiniteMagnitude))
        if size.height < 28 {
            size = CGSize(width: size.width, height: 28)
        }
        size = CGSize(width: size.width + 44, height: size.height)
        return size
    }

//    override func contentInsets(model: SessionBaseModel) -> UIEdgeInsets {
//        return UIEdgeInsets(top: 8.5, left: 20, bottom: 8.5, right: 20)
//    }
    override func cellInsets(model: SessionBaseModel) -> UIEdgeInsets {
        return UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16)
    }

    override func contentViewClass(model: SessionBaseModel) -> IMContentBaseView.Type {
        return IMPhoneCallContentView.self
    }
    
    override func onlyShowContent(model: SessionBaseModel) -> Bool {
        return true
    }
}

class IMPhoneCallContentView: IMContentBaseView {
    var effectView: UIVisualEffectView!
    var contentLabel: UILabel!
    var phontIcon: UIImageView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        initialViews()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func initialViews() {
        effectView = {
            let v = UIVisualEffectView(effect: UIBlurEffect(style: .light))
            v.alpha = 1
            v.cornerRadius = 4
            insertSubview(v, at: 0)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            return v
        }()
        
        contentLabel = UILabel()
        self.containerView.addSubview(contentLabel)
        contentLabel.font = .t.tbs
        contentLabel.textColor = .white
        contentLabel.numberOfLines = 0
        contentLabel.textAlignment = .center
        contentLabel.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 28))
        }
        
        phontIcon = {
            let v = UIImageView()
            v.image = MWIconFont.image(fromIcon: .iconCallHangup, size: CGSize(width: 16, height: 16), color: .white)
            self.containerView.addSubview(v)
            v.snp.makeConstraints { make in
                //make.leading.equalTo(contentLabel.snp.trailing).offset(8)
                make.trailing.equalToSuperview().offset(-8)
                make.centerY.equalToSuperview()
            }
            return v
        }()
    }

    override func refreshModel(model: SessionBaseModel) {
        super.refreshModel(model: model)

        guard let nimMessage = model.v2msg else {
            return
        }

        contentLabel.text = getContentTextWith(message: nimMessage)
        
        layoutIfNeeded()
        UIView.setAnimationsEnabled(true)
    }

    fileprivate func getContentTextWith(message: V2NIMMessage) -> String {
        let info = IMRemoteUtil.dealRemoteInfo(message: message)
        return info.displayString ?? ""
    }

    override func cellTapAction() {
        let event = IMEventModel()
        event.eventType = .phonecallTap
        event.cellModel = model
        delegate?.onTapAction(event: event)
    }
}
