//
//  MVMineHeadInfoView.swift
//  MusicVideoPlus
//
//  Created by SanW on 2021/6/23.
//


import UIKit

class MVMineHeadInfoView: UIView {
    private var leftMargin: CGFloat = 0
    lazy var iconImageView: UIImageView = {
        let iconImageView = UIImageView()
        iconImageView.contentMode = .scaleAspectFill
        iconImageView.clipsToBounds = true
        iconImageView.backgroundColor = UIColor.white
        return iconImageView
    }()

    lazy var nickNameLab: UILabel = {
        let nickNameLab = UILabel()
        nickNameLab.textColor = .black
        nickNameLab.numberOfLines = 1
        nickNameLab.font = UIFont.systemFont(ofSize: 24, weight: .bold)
        return nickNameLab
    }()

    lazy var productsLabel: UILabel = {
        let productsLabel = UILabel()
        productsLabel.textColor = UIColor(white: 0, alpha: 0.5)
        productsLabel.numberOfLines = 1
        productsLabel.font = UIFont.systemFont(ofSize: 14, weight: .semibold)
        return productsLabel
    }()

    init(frame: CGRect, margin: CGFloat = 16) {
        super.init(frame: frame)
        leftMargin = margin
        addSubview(iconImageView)
        addSubview(nickNameLab)
        addSubview(productsLabel)
    }

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

    override func layoutSubviews() {
        super.layoutSubviews()
        addData()
        addLayout()
    }

    func addData() {
        iconImageView.setNetImage(url: BFLoginUserInfo.shared.avatarUrl, placeholder: UIImage(named: "wode")!)
        nickNameLab.text = BFLoginUserInfo.shared.nickName
        updateProducts()
    }

    func updateProducts() -> Void {
        productsLabel.text = "\(Int(BFLoginUserInfo.shared.videos)?.changeUnit() ?? "0") 作品"
    }
    
    func addLayout() {
        let iconH: CGFloat = cDefaultMargin * 7
        let margin: CGFloat = 12
        iconImageView.addCorner(corner: iconH / 2)
        iconImageView.snp.makeConstraints { make in
            make.width.height.equalTo(iconH)
            make.top.equalToSuperview().offset(cDefaultMargin / 2)
            make.left.equalToSuperview().offset(leftMargin)
        }
        nickNameLab.snp.makeConstraints { make in
            make.left.equalTo(iconImageView.snp.right).offset(margin)
            make.right.equalToSuperview().offset(-leftMargin)
            make.centerY.equalTo(iconImageView).offset(-margin)
        }
        productsLabel.snp.makeConstraints { make in
            make.left.right.equalTo(nickNameLab)
            make.centerY.equalTo(iconImageView).offset(margin)
        }
    }
}
