//
//  PQMessageVideoInfoView.swift
//  PQSpeed
//
//  Created by SanW on 2020/11/12.
//  Copyright © 2020 BytesFlow. All rights reserved.
//

import UIKit
import BFCommonKit

open class PQBaseVideoInfoView: UIView {
    lazy public var imageView: UIImageView = {
        let imageView = UIImageView(image:UIImage.moduleImage(named: "msg_default", moduleName: "BFFramework",isAssets: false))
        imageView.addCorner(corner: 4)
        imageView.contentMode = .scaleAspectFill
        return imageView
    }()

    lazy public var videoTagView: UIImageView = {
        let videoTagView = UIImageView(image:UIImage.moduleImage(named: "msg_video_tag", moduleName: "BFFramework",isAssets: false))
        return videoTagView
    }()

    lazy public var titleLab: UILabel = {
        let titleLab = UILabel()
        titleLab.textColor = UIColor.hexColor(hexadecimal: "#CCCCCC")
        titleLab.numberOfLines = 3
        titleLab.font = UIFont(name: "PingFangSC", size: 13)
        return titleLab
    }()

    override public init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(imageView)
        addSubview(titleLab)
        imageView.addSubview(videoTagView)
        backgroundColor = UIColor.hexColor(hexadecimal: "#171718")
    }

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

   open var videoData: PQVideoListModel? {
        didSet {
            addData()
            addLayout()
        }
    }

   open func addData() {
        // 这里会crash
        let coverImg = (videoData?.videoCoverSnapshotPath != nil && (videoData?.videoCoverSnapshotPath?.count ?? 0) > 0) ? videoData?.videoCoverSnapshotPath ?? "" : (videoData?.coverImg?["coverImgPath"] as? String ?? "")
    imageView.setNetImage(url: coverImg, placeholder: UIImage.moduleImage(named: "msg_default", moduleName: "BFFramework",isAssets: false)!)
        titleLab.text = videoData?.title
    }

    open func addLayout() {
        let margin: CGFloat = 12
        let imageH: CGFloat = 57
        let imageW: CGFloat = imageH * (100.0 / 57.0)
        let tagW: CGFloat = 21
        let tagH: CGFloat = 23
        imageView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(margin)
            make.width.equalTo(imageW)
            make.height.equalTo(imageH)
            make.centerY.equalToSuperview()
        }
        videoTagView.snp.makeConstraints { make in
            make.right.bottom.equalToSuperview().offset(-3)
            make.width.equalTo(tagW)
            make.height.equalTo(tagH)
        }
        titleLab.snp.makeConstraints { make in
            make.top.equalTo(imageView)
            make.left.equalTo(imageView.snp.right).offset(margin)
            make.right.equalToSuperview().offset(-margin)
        }
    }
}
