//
//  PQServerProtocalView.swift
//  MusicVideoPlus
//
//  Created by SanW on 2021/6/7.
//

import BFFramework
import UIKit

// MARK: - 应用服务协议

/// 应用服务协议
class PQServerProtocalView: UIView {
    public var remindBlock: ((_ sender: UIButton?, _ webUrl: String?) -> Void)?

    public lazy var contentView: UIView = {
        let contentView = UIView()
        contentView.backgroundColor = UIColor.white
        contentView.addCorner(corner: 4)
        return contentView
    }()

    public lazy var titleLab: UILabel = {
        let titleLab = UILabel()
        titleLab.font = UIFont.systemFont(ofSize: 18, weight: .medium)
        titleLab.textAlignment = .center
        titleLab.numberOfLines = 0
        titleLab.textColor = UIColor.black
        return titleLab
    }()

    public lazy var confirmBtn: UIButton = {
        let confirmBtn = UIButton(type: .custom)
        confirmBtn.setTitle("确定", for: .normal)
        confirmBtn.setTitleColor(UIColor.black, for: .normal)
        confirmBtn.tag = 2
        confirmBtn.addTarget(self, action: #selector(btnClck(sender:)), for: .touchUpInside)
        confirmBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        return confirmBtn
    }()

    lazy var protocolLab: TYAttributedLabel = {
        let protocolLab = TYAttributedLabel()
        protocolLab.highlightedLinkBackgroundColor = UIColor.white
        protocolLab.delegate = self
        protocolLab.numberOfLines = 0
        protocolLab.font = UIFont(name: "PingFang SC", size: 15)
        protocolLab.text = "感谢您选择闪音APP\n我们非常重视您的个人信息安全和隐私保护。依据最新法律要求，使用我们的产品前，请仔细阅读并同意"
        protocolLab.textColor = UIColor.black
        protocolLab.appendLink(withText: "《软件许可及服务协议》", linkFont: UIFont(name: "PingFang SC", size: 15), linkColor: UIColor.hexColor(hexadecimal: "#07C160"), underLineStyle: CTUnderlineStyle(), linkData: cUserProtocol)
        protocolLab.appendLink(withText: "和", linkFont: UIFont(name: "PingFang SC", size: 15), linkColor: UIColor.black, underLineStyle: CTUnderlineStyle(), linkData: nil)
        protocolLab.appendLink(withText: "《隐私政策》", linkFont: UIFont(name: "PingFang SC", size: 15), linkColor: UIColor.hexColor(hexadecimal: "#07C160"), underLineStyle: CTUnderlineStyle(), linkData: cPrivacy)
        protocolLab.appendLink(withText: "，以便我们向您提供更优质的服务！\n\n我们承诺将尽全力保护您的个人信息及合法权益，再次感谢您的信任！", linkFont: UIFont(name: "PingFang SC", size: 15), linkColor: UIColor.black, underLineStyle: CTUnderlineStyle(), linkData: nil)
        return protocolLab
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(contentView)
        backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.8)
        contentView.addSubview(titleLab)
        contentView.addSubview(confirmBtn)
        contentView.addSubview(protocolLab)
        titleLab.text = "服务协议与隐私政策"
        titleLab.textAlignment = .center
        confirmBtn.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
        confirmBtn.setTitle("同意", for: .normal)
        confirmBtn.setTitleColor(UIColor.white, for: .normal)
        confirmBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#07C160")
        confirmBtn.addCorner(corner: cDefaultMargin * 2.5)
    }

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

    func addLayout() {
        let normalH: CGFloat = cDefaultMargin * 2.5
        var protocolH: CGFloat = protocolLab.getSizeWithWidth(cScreenWidth - cDefaultMargin * 12).height
        if cScreenHeigth <= 568.0 {
            protocolH = protocolH + cDefaultMargin * 4
        }
        let contentH: CGFloat = normalH + cDefaultMargin * 2 + cDefaultMargin + protocolH + cDefaultMargin * 2 + cDefaultMargin * 5 + cDefaultMargin * 2

        contentView.snp.makeConstraints { make in
            make.left.equalTo(self).offset(cDefaultMargin * 4)
            make.right.equalTo(self).offset(-cDefaultMargin * 4)
            make.height.equalTo(contentH)
            make.center.equalTo(self)
        }
        contentView.addCorner(corner: 4)
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(contentView).offset(normalH)
            make.right.equalTo(contentView).offset(-normalH)
            make.height.equalTo(cDefaultMargin * 2)
            make.top.equalTo(contentView).offset(normalH)
        }
        protocolLab.snp.makeConstraints { make in
            make.top.equalTo(titleLab.snp.bottom).offset(cDefaultMargin)
            make.left.right.equalTo(titleLab)
            make.height.equalTo(protocolH)
        }
        confirmBtn.snp.makeConstraints { make in
            make.left.right.equalTo(titleLab)
            make.height.equalTo(cDefaultMargin * 5)
            make.bottom.equalToSuperview().offset(-cDefaultMargin * 2)
        }
    }

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

    @objc func btnClck(sender: UIButton) {
        saveUserDefaults(key: cShowProtocal, value: "1")
        removeFromSuperview()
        if remindBlock != nil {
            remindBlock!(sender, nil)
        }
    }
}

extension PQServerProtocalView: TYAttributedLabelDelegate {
    func attributedLabel(_: TYAttributedLabel!, textStorageClicked textStorage: TYTextStorageProtocol!, at _: CGPoint) {
        var link: String? = (textStorage as? TYLinkTextStorage)?.linkData as? String ?? ""
        if link == nil || (link?.count ?? 0) <= 0 {
            let range = (textStorage as? TYLinkTextStorage)?.range
            if range?.length == 11 {
                link = cUserProtocol
            } else if range?.length == 6 {
                link = cPrivacy
            }
        }
        if link != nil, (link?.count ?? 0) > 0, remindBlock != nil {
            remindBlock!(nil, link)
        }
    }
}


// MARK: - 绑定手机号提示

/// 绑定手机号提示
public class PQBandingPhoneRemindView: UIView {
    public  var bandingPhoneRemindHandle: ((_ sender: UIButton) -> Void)?
    public var remindBlock: ((_ sender: UIButton?, _ webUrl: String?) -> Void)?

    lazy var contentView: UIView = {
        let contentView = UIView()
        contentView.backgroundColor = UIColor.white
        contentView.addCorner(corner: 4)
        return contentView
    }()

    lazy  public   var protocolLab: TYAttributedLabel = {
        let protocolLab = TYAttributedLabel()
        protocolLab.highlightedLinkBackgroundColor = UIColor.white
        protocolLab.delegate = self
        protocolLab.numberOfLines = 0
        protocolLab.font = UIFont.systemFont(ofSize: 18)
        protocolLab.text = "绑定手机号，防止视频丢失"
        protocolLab.textColor = UIColor.black
//        protocolLab.appendLink(withText: "《互联网跟帖评论服务管理规定》", linkFont: UIFont.systemFont(ofSize: 18), linkColor: UIColor.hexColor(hexadecimal: "#07C160"), underLineStyle: CTUnderlineStyle(), linkData: cNetManagePrivacy)
//        protocolLab.appendLink(withText: "，上传视频需绑定手机号", linkFont: UIFont.systemFont(ofSize: 18), linkColor: UIColor.black, underLineStyle: CTUnderlineStyle(), linkData: nil)
        return protocolLab
    }()

    lazy  public  var cancelBtn: UIButton = {
        let cancelBtn = UIButton(type: .custom)
        cancelBtn.setTitle("暂不绑定", for: .normal)
        cancelBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#999999"), for: .normal)
        cancelBtn.tag = 1
        cancelBtn.addTarget(self, action: #selector(btnClck(sender:)), for: .touchUpInside)
        cancelBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        return cancelBtn
    }()

    lazy  public   var confirmBtn: UIButton = {
        let confirmBtn = UIButton(type: .custom)
        confirmBtn.setTitle("去绑定", for: .normal)
        confirmBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#07C160"), for: .normal)
        confirmBtn.tag = 2
        confirmBtn.addTarget(self, action: #selector(btnClck(sender:)), for: .touchUpInside)
        confirmBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        return confirmBtn
    }()

    override public  init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(contentView)
        contentView.addSubview(protocolLab)
        contentView.addSubview(cancelBtn)
        contentView.addSubview(confirmBtn)
        backgroundColor = UIColor(hue: 0, saturation: 0, brightness: 0, alpha: 0.6)
        let ges = UITapGestureRecognizer(target: self, action: #selector(removeView))
        addGestureRecognizer(ges)
    }

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

    override  public  func layoutSubviews() {
        super.layoutSubviews()
        let normalH: CGFloat = cDefaultMargin * 2.5
        let protocolH: CGFloat = protocolLab.getSizeWithWidth(cScreenWidth - normalH * 6).height
        let contentH = cDefaultMargin * 2 + protocolH + cDefaultMargin * 3 + normalH + 15
        contentView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.height.equalTo(contentH)
            make.left.equalToSuperview().offset(normalH * 2)
            make.right.equalToSuperview().offset(-normalH * 2)
        }
        protocolLab.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(cDefaultMargin * 2)
            make.height.equalTo(protocolH)
            make.left.equalToSuperview().offset(normalH)
            make.right.equalToSuperview().offset(-normalH)
        }
        confirmBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-normalH)
            make.height.equalTo(normalH)
            make.bottom.equalToSuperview().offset(-15)
        }
        cancelBtn.snp.makeConstraints { make in
            make.right.equalTo(confirmBtn.snp_left).offset(-normalH)
            make.centerY.height.equalTo(confirmBtn)
        }
    }

    @objc  public  func removeView() {
        removeFromSuperview()
    }

    @objc public   func btnClck(sender: UIButton) {
        removeFromSuperview()
        if bandingPhoneRemindHandle != nil {
            bandingPhoneRemindHandle!(sender)
        }
    }
    
    /// 账号被封禁提示
    /// - Returns: <#description#>
   class func accountBlockRemindView(remindTitle: String) {
        let remindData = PQBaseModel()
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 3.0
        paragraphStyle.alignment = .center
        remindData.attributedTitle = NSMutableAttributedString(string: "您的账号已被封禁，\(remindTitle)\n有疑问可拨打客服电话：\n0731-85676658 18519777009", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.hexColor(hexadecimal: "#666666"), NSAttributedString.Key.paragraphStyle: paragraphStyle])
        let remindView = PQUploadRemindView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
        UIApplication.shared.keyWindow?.addSubview(remindView)
        remindView.remindData = remindData
    }
}


extension PQBandingPhoneRemindView: TYAttributedLabelDelegate {
    public   func attributedLabel(_: TYAttributedLabel!, textStorageClicked textStorage: TYTextStorageProtocol!, at _: CGPoint) {
        var link: String? = (textStorage as? TYLinkTextStorage)?.linkData as? String ?? ""
        if link == nil || (link?.count ?? 0) <= 0 {
            let range = (textStorage as? TYLinkTextStorage)?.range
            if range?.length == 11 {
                link = cUserProtocol
            } else if range?.length == 6 {
                link = cPrivacy
            }
        }
        if link != nil, (link?.count ?? 0) > 0, remindBlock != nil {
            remindBlock!(nil, link)
        }
    }
}



