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

import UIKit
import BFCommonKit

public class PQSectionHeadView: UIView {
    // 清除按钮回调
    public var btnClickHandle: ((_ sender: UIButton) -> Void)?
    // 是否隐藏清除按钮
    public var isHiddenClearBtn: Bool = true {
        didSet {
            clearBtn.isHidden = isHiddenClearBtn
        }
    }

    public var sectionTitle: String? {
        didSet {
            addData()
            addLayout()
        }
    }

    lazy public var lineView: UIView = {
        let lineView = UIView()
        lineView.backgroundColor = UIColor.hexColor(hexadecimal: "#EE0051")
        lineView.addCorner(corner: 1.5)
        return lineView
    }()

    lazy public var titleLab: UILabel = {
        let titleLab = UILabel()
        titleLab.textColor = UIColor.white
        titleLab.font = UIFont(name: "PingFangSC", size: 16)
        return titleLab
    }()

    lazy public  var clearBtn: UIButton = {
        let clearBtn = UIButton(type: .custom)
        clearBtn.setTitle("全部已读 ", for: .normal)
        clearBtn.setImage(UIImage.moduleImage(named: "msg_clear_noreaded", moduleName: "BFFramework",isAssets: false), for: .normal)
        clearBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#666666"), for: .normal)
        clearBtn.titleLabel?.font = UIFont(name: "PingFangSC", size: 13)
        clearBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
        clearBtn.isHidden = true
        return clearBtn
    }()

    override public  init(frame: CGRect) {
        super.init(frame: frame)
        addSubviews()
    }

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

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

    public  func addSubviews() {
        addSubview(lineView)
        addSubview(titleLab)
        addSubview(clearBtn)
    }

    public  func addData() {
        titleLab.text = sectionTitle
    }

    public  func addLayout() {
        let lineW: CGFloat = 3
        let lineH: CGFloat = cDefaultMargin * 2

        lineView.snp.makeConstraints { make in
            make.width.equalTo(lineW)
            make.height.equalTo(lineH)
            make.centerY.equalToSuperview()
        }
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(lineView.snp.right).offset(lineW * 4)
            make.centerY.equalToSuperview()
        }
        clearBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-cDefaultMargin)
        }
        clearBtn.imagePosition(at: PQButtonImageEdgeInsetsStyle.right, space: 0)
    }

    @objc public  func btnClick(sender: UIButton) {
        if btnClickHandle != nil {
            btnClickHandle!(sender)
        }
    }
}
