//
//  INRecorderController.swift
//  Introduce
//
//  Created by 胡志强 on 2021/11/29.
//  首页

import BFMaterialKit
import BFRecordScreenKit
import BFUIKit
import Foundation
import UIKit
import TYAttributedLabel
import BFCommonKit
import BFAnalyzeKit

public let cUserProtocol = "https://weapppiccdn.yishihui.com/resources/agreements/shishuo/videoservice.html?type=20"
/** 隐私政策 */
public let cPrivacy = "https://weapppiccdn.yishihui.com/resources/agreements/shishuo/videoagreement.html?type=20"
 
class INRecorderController: BFBaseViewController {
    lazy var addVideoBtn: UIButton = {
        let btn = UIButton(type: .custom)
//        btn.setTitle("Add", for: .normal)
        btn.setImage(UIImage(named: "add"), for: .normal)
        btn.addTarget(self, action: #selector(addVideo), for: .touchUpInside)
        return btn
    }()

    lazy var bgView:UIImageView = {
        let bgView = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
        bgView.contentMode = .scaleAspectFill
        bgView.image = UIImage(named: "homebg")
        return bgView
    }()
    
    //欢迎界面
    lazy var welcomeView:UIView = {
        let view = UIView.init()
        view.backgroundColor = .white
        view.addCorner(corner:17)
        return view
        
    }()
    //欢迎界面背景
    lazy var welcomeBackView:UIView = {
        let view = UIView.init(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
        view.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.6)
        return view
        
    }()
    
    lazy var welcomeTitle:UILabel = {
       
        let l = UILabel()
        l.text = "欢迎使用"
        l.textColor = .black
        l.font = UIFont.boldSystemFont(ofSize: 22)
        l.textAlignment = .center
        view.addSubview(l)
        return l
    }()
    
    lazy var protocolLab: TYAttributedLabel = {
        let protocolLab = TYAttributedLabel()
        protocolLab.highlightedLinkBackgroundColor = UIColor.clear
        protocolLab.backgroundColor = UIColor.clear
        protocolLab.numberOfLines = 0
        protocolLab.delegate = self
        protocolLab.font = UIFont.systemFont(ofSize: 14)
        protocolLab.text = "在您使用视说服务前，请认真阅读"
        protocolLab.textColor = UIColor(red: 0.262, green: 0.262, blue: 0.262, alpha: 1)
        protocolLab.appendLink(withText: "《视说用户协议》、 ", linkFont: UIFont.systemFont(ofSize: 14), linkColor: UIColor.hexColor(hexadecimal: "#90a9d2"),  underLineStyle:CTUnderlineStyle(), linkData: cUserProtocol)

        protocolLab.appendLink(withText: "  《用户隐私政策》", linkFont: UIFont.systemFont(ofSize: 14), linkColor: UIColor.hexColor(hexadecimal: "#90a9d2"), underLineStyle:CTUnderlineStyle(), linkData: cPrivacy)
        
        protocolLab.appendLink(withText: "\n\n", linkFont: UIFont.systemFont(ofSize: 14), linkColor: UIColor.hexColor(hexadecimal: "#999999"), underLineStyle: CTUnderlineStyle(), linkData: nil)
        
        protocolLab.appendLink(withText: "点击同意表示您已阅读并且认可所有条款。", linkFont: UIFont.systemFont(ofSize: 14), linkColor: UIColor(red: 0.262, green: 0.262, blue: 0.262, alpha: 1), underLineStyle: CTUnderlineStyle(), linkData: nil)
        protocolLab.textAlignment = CTTextAlignment.left
        return protocolLab
    }()
    
    lazy var doneBtn:UIButton = {
        let doneBtn = UIButton()
        doneBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#389AFF")
        doneBtn.setTitle("同意", for: .normal)
        doneBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        doneBtn.addTarget(self, action: #selector(doneAction), for: .touchUpInside)
        doneBtn.addCorner(corner: 4)
        return doneBtn
    }()
    
    lazy var cancleBtn:UIButton = {
        let cancleBtn = UIButton()
        cancleBtn.backgroundColor = UIColor.clear
        cancleBtn.setTitle("不同意", for: .normal)
        cancleBtn.setTitleColor( UIColor(red: 0.262, green: 0.262, blue: 0.262, alpha: 1), for: .normal)
        cancleBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        cancleBtn.addTarget(self, action: #selector(cancleAction), for: .touchUpInside)
        
        cancleBtn.addCorner(corner: 4)
        return cancleBtn
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        hiddenNavigation()
        // 曝光上报
        BFEventTrackAdaptor.baseReportUpload(businessType: .bt_pageView, objectType: nil, pageSource: .sp_homePage, commonParams: commonParams())
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .black
        view.addSubview(bgView)
        
        view.addSubview(addVideoBtn)
        addVideoBtn.snp.makeConstraints { make in
            make.width.height.equalTo(170)
            make.center.equalToSuperview()
        }

        let l = UILabel()
        l.text = "选择视频/图片\n开始讲解"
        l.textColor = .white
        l.font = UIFont.systemFont(ofSize: 16)
        l.textAlignment = .center
        view.addSubview(l)
        l.numberOfLines = 2
        l.snp.makeConstraints { make in
            make.top.equalTo(addVideoBtn.snp.bottom)
            make.centerX.equalToSuperview()
            make.width.equalTo(200)
            make.height.equalTo(48)
        }
    
        //欢迎界面
        if(getUserDefaults(key: "welcomeViewisshow") == nil){
            view.addSubview(welcomeBackView)
            welcomeBackView.addSubview(welcomeView)
            welcomeView.addSubview(protocolLab)
            welcomeView.addSubview(welcomeTitle)
            welcomeView.addSubview(doneBtn)
            welcomeView.addSubview(cancleBtn)
            
            welcomeView.snp.makeConstraints{ make in
                make.center.equalToSuperview()
                make.width.equalTo(280)
                make.height.equalTo(350)
            }
            
            welcomeTitle.snp.makeConstraints{ make in
                make.centerX.equalToSuperview()
                make.width.equalTo(120)
                make.top.equalToSuperview().offset(30)
                make.height.equalTo(31)
            }
            
            protocolLab.snp.makeConstraints{ make in
                make.centerX.equalToSuperview()
                make.width.equalTo(230)
                make.top.equalTo(welcomeTitle.snp.bottom).offset(16)
                make.height.equalTo(140)
            }
     
            doneBtn.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.width.equalTo(230)
                make.height.equalTo(47)
                make.top.equalTo(protocolLab.snp.bottom).offset(30)
            }
            
            cancleBtn.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.width.equalTo(230)
                make.height.equalTo(20)
                make.top.equalTo(doneBtn.snp.bottom).offset(17)
            }
        }
        
        // add by ak 取 nsl token
        BFRecordScreenViewModel.getNlsAccessToken { [weak self] token, appkey in
            BFLog(message: "提前取一次nls appkey is \(appkey), token is \(token)")
       
        }
   
    }
    
    @objc func doneAction() {
        welcomeBackView.isHidden = true
        
        saveUserDefaults(key: "welcomeViewisshow", value: "true")
       
    }
    
    
    @objc func cancleAction() {
        welcomeBackView.isHidden = true
 
        exit(-1)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @objc func addVideo() {
        if (getUserDefaults(key: "welcomeViewisshow") == nil) {
            welcomeBackView.isHidden = false
        }else{
            let vc = INPhotoVideosController()
            vc.hidesBottomBarWhenPushed = true
            navigationController?.pushViewController(vc, animated: true)
            // 按钮点击
            BFEventTrackAdaptor.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_addVideo, pageSource: .sp_homePage, commonParams: commonParams())
        }
    }
}


//点击超链接回调
extension INRecorderController: TYAttributedLabelDelegate {
    func attributedLabel(_: TYAttributedLabel!, textStorageClicked textStorage: TYTextStorageProtocol!, at _: CGPoint) {
        let link: String? = (textStorage as? TYLinkTextStorage)?.linkData as? String ?? ""
        if link != nil, (link?.count ?? 0) > 0 {
            let detail = BFBaseWebViewController()
            detail.baseUrl = link
            self.navigationController?.pushViewController(detail, animated: true)
        }
    }
}
