//
//  LXFPopUpToolView.swift
//  MobilePortal
//
//  Created by caiyang on 2020/5/13.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

@objcMembers class LXFPopUpToolView: NSObject {
    class func sharedInstance()->LXFPopUpToolView {
        struct SharedStatic {
            static var instance: LXFPopUpToolView = LXFPopUpToolView()
        }
        return SharedStatic.instance
    }
    var title:String?
    var content:String?
    var contentH = CGFloat()
    var backgroundViewW = CGFloat()
    
    lazy var backgroundView: UIView = {
        let backgroundView = UIView.init()
        backgroundView.backgroundColor = UIColor.white
        backgroundView.layer.cornerRadius = 10
        return backgroundView
    }()
    /// 横
    lazy var horizontalView: UIView = {
        let horizontalView = UIView.init()
        horizontalView.backgroundColor = UIColor.black
        horizontalView.alpha = 0.1
        return horizontalView
    }()
    /// 竖
    lazy var verticalView: UIView = {
        let verticalView = UIView.init()
        verticalView.backgroundColor = UIColor.black
        verticalView.alpha = 0.1
        return verticalView
    }()
    /// 标题
    lazy var titleLabel: UILabel = {
        let titleLabel = UILabel.init()
        titleLabel.textAlignment = .center
//        titleLabel.font = UIFont.systemFont(ofSize: 16)
        titleLabel.font = UIFont.init(name: "Helvetica-Bold", size: 16.67)
        titleLabel.numberOfLines = 0
//        titleLabel.backgroundColor = UIColor.red
//        titleLabel.font = UIFont.systemFont(ofSize: 13.33)
        titleLabel.textColor = UIColorFromRGB(rgbValue: 0x424C52)
        return titleLabel
    }()
    /// 标题
    lazy var contentLabel: UILabel = {
        let contentLabel = UILabel.init()
        contentLabel.textAlignment = .center
        contentLabel.font = UIFont.systemFont(ofSize: 12)
        contentLabel.numberOfLines = 0
        contentLabel.textColor = UIColorFromRGB(rgbValue: 0x23262A)
        return contentLabel
    }()
    /// 取消
    lazy var cancelBtn : UIButton = {
        let cancelBtn = UIButton.init(type: .custom)
        cancelBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        cancelBtn.titleLabel?.textAlignment = .center
        cancelBtn.setTitle("取消", for: .normal)
        cancelBtn.addTarget(self, action: #selector(cancelBtnAction), for: .touchUpInside)
        cancelBtn.setTitleColor(statusBarColor, for: .normal)
        return cancelBtn
    }()
    /// 确定
    lazy var confirmBtn : UIButton = {
        let confirmBtn = UIButton.init(type: .custom)
        confirmBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        confirmBtn.titleLabel?.textAlignment = .center
        confirmBtn.setTitle("确定", for: .normal)
        confirmBtn.addTarget(self, action: #selector(cancelBtnAction), for: .touchUpInside)
        confirmBtn.setTitleColor(blackTitleColor, for: .normal)
        return confirmBtn
    }()
    func show(title:String,content:String){
        self.content = content
        self.title = title
        contentH = 0
        backgroundViewW = 250
        getContentH()
        backgroundView.frame = CGRect(x:0, y: 0, width:backgroundViewW , height:contentH+100)
        addView()
        LXFPopUpTool.sharedInstance()?.popUp(with: backgroundView, withAnimation: false, withCanTapBgCancel: true)
    }
    func addView(){
        titleLabel.text = self.title
        titleLabel.frame = CGRect(x:0, y: 0, width:backgroundView.width , height:40 )
        backgroundView.addSubview(titleLabel)
        
        contentLabel.text = self.content
        contentLabel.frame = CGRect(x:10, y: titleLabel.originY+titleLabel.height+5, width:backgroundView.width-20 , height: contentH+10)
        backgroundView.addSubview(contentLabel)
        
        
        confirmBtn.frame = CGRect(x:0, y: contentLabel.originY+contentLabel.height+10, width:backgroundView.width/2 , height:40 )
        cancelBtn.frame = CGRect(x:confirmBtn.originX+confirmBtn.width, y: confirmBtn.originY, width:backgroundView.width/2 , height:40 )
        backgroundView.addSubview(confirmBtn)
        backgroundView.addSubview(cancelBtn)
        
        horizontalView.frame = CGRect(x:0, y: confirmBtn.originY-1, width:backgroundView.width , height:1 )
        backgroundView.addSubview(horizontalView)
        
        verticalView.frame = CGRect(x:confirmBtn.originX+confirmBtn.width, y: confirmBtn.originY, width:1 , height:40 )
        backgroundView.addSubview(verticalView)
    }
    
    @objc func cancelBtnAction(){
        LXFPopUpTool.sharedInstance()?.removePopUpView()
    }
    /// 根据/n计算高度
    func getContentH() {
        if (self.content?.contains("\n"))! {
            let splitedArray = self.content!.components(separatedBy: "\n")
            for index in 0..<splitedArray.count {
                let str = splitedArray[index]
                let h = getTextHeigh(textStr: str, font: UIFont.systemFont(ofSize: 12), width: backgroundViewW-20)
                contentH = contentH + h
                print(contentH,h,str)
            }
        } else {
            contentH = getTextHeigh(textStr: self.content!, font: UIFont.systemFont(ofSize: 12), width: backgroundViewW-10)
        }
        
    }
}
