//
//  MVSettingController.swift
//  MusicVideoPlus
//
//  Created by ak on 2021/6/2.
//

import UIKit
import BFFramework
class MVSettingController: MVBaseController{

    //tips 1
    lazy var tipsA: UILabel = {
        let tipsA = UILabel()
        tipsA.font = UIFont.systemFont(ofSize: 26, weight: .medium)
        tipsA.textColor = UIColor.hexColor(hexadecimal: "#929292")
        let infoDictionary = Bundle.main.infoDictionary
         
        let majorVersion :String = infoDictionary! ["CFBundleShortVersionString"] as! String//主程序版本号
        tipsA.text = "APP版本号V\(majorVersion)"
        tipsA.lineBreakMode = .byWordWrapping
        tipsA.textAlignment = .center
        tipsA.numberOfLines = 0
        return tipsA
    }()

    //显示协议
    lazy var protocolLab: TYAttributedLabel = {
        let protocolLab = TYAttributedLabel()
        protocolLab.highlightedLinkBackgroundColor = UIColor.clear
        protocolLab.backgroundColor = UIColor.clear
        protocolLab.delegate = self
        protocolLab.numberOfLines = 0
        protocolLab.font = UIFont.systemFont(ofSize: 14)
        protocolLab.text = ""
        protocolLab.textColor = UIColor.hexColor(hexadecimal: "#999999")
        protocolLab.appendLink(withText: "《用户协议》", linkFont: UIFont.systemFont(ofSize: 14), linkColor: UIColor.hexColor(hexadecimal: "#6783AC"), underLineStyle: CTUnderlineStyle.init(), linkData: cUserProtocol)

        protocolLab.appendLink(withText: "《隐私协议》", linkFont: UIFont.systemFont(ofSize: 14), linkColor: UIColor.hexColor(hexadecimal: "#6783AC"), underLineStyle: CTUnderlineStyle.init(), linkData: cPrivacy)

        protocolLab.textAlignment = CTTextAlignment.center
        return protocolLab
    }()
    
    // 卡点视频 btn
    lazy var logoutBtn: UIButton = {
        let logoutBtn = UIButton(type: .custom)
        logoutBtn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
        logoutBtn.setImage(UIImage(named: "logoutBtn"), for: .normal)
        logoutBtn.adjustsImageWhenHighlighted = false
        return logoutBtn
    }()
    
    override func viewWillAppear(_: Bool) {
        super.viewWillAppear(true)

        showNavigation()
 
        leftButton(image: "back_black")
 
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor =  UIColor.hexColor(hexadecimal: "#F2F2F2")
        navHeadImageView?.backgroundColor = UIColor.hexColor(hexadecimal: "#F2F2F2")
        view.addSubview(tipsA)
        view.addSubview(protocolLab)
        view.addSubview(logoutBtn)
        addLayout()
        
    
        logoutBtn.isHidden = !BFLoginUserInfo.shared.isLogin()
        
    }
    
    @objc func btnClick() {
        BFLog(message: "logout ")
        let remindData:PQBaseModel = PQBaseModel()
        remindData.title = "退出？"
        
        let remindView = PQRemindView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
        UIApplication.shared.keyWindow?.addSubview(remindView)
        remindView.remindData = remindData
        remindView.remindBlock = { [weak self] sender, _ in
            if sender.tag == 2 {
                PQMineViewModel.quitLogin { [weak self] _, _ in
                    postNotification(name: cQuitSuccesssNotiKey)
                    self?.logoutBtn.isHidden = true
                    self?.navigationController?.popViewController(animated: true)
                }
            }
        }
    }
    
    
    func addLayout() {
        
        tipsA.snp.remakeConstraints { make in
            make.width.equalTo(146)
            make.height.equalTo(160)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(313)
        }
        
        protocolLab.snp.makeConstraints { make in
            make.bottom.equalTo(view).offset(-cDefaultMargin * 5)
            make.left.equalTo(view).offset(cDefaultMargin * 2)
            make.right.equalTo(view).offset(-cDefaultMargin * 2)
            make.height.equalTo(cScreenHeigth <= 568 ? cDefaultMargin * 4 : cDefaultMargin * 6)
        }
        
        logoutBtn.snp.makeConstraints { make in
            make.bottom.equalTo(protocolLab.snp_top).offset(-30)
            make.centerX.equalToSuperview()
            make.height.equalTo(50)
            make.width.equalTo(335)
        }
    }
}


extension MVSettingController: 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 == 0 {
                link = cUserProtocol
            } else if range?.length == 6 {
                link = cPrivacy
            }
        }
        let detail = PQBaseWebViewController()
        detail.baseUrl = link
        navigationController?.pushViewController(detail, animated: true)
    }
}
