//
//  CCRequestLoadingView.swift
//  SwiftProject
//
//  Created by soldoros on 2024/1/10.
//

import UIKit

/// 网络界面的加载状态
///
/// - loading: 加载中
/// - empty: 数据为空
/// - failure: 加载失败
enum RequestStatus {
    case loading
    case empty
    case failure
}


/// 加载界面加载后的回调
typealias LoadingBlock = (_ status:RequestStatus) -> ()


class CCRequestLoadingView: UIView{

    //点击界面回调
    var _loadingBlock:LoadingBlock?
    //进度指示器
    var _mActivity:UIActivityIndicatorView?
    var _statusCode:RequestStatus?
    //提示
    var mTitleLab:UILabel?
    var mDetailLab:UILabel?

    init(frame: CGRect, superView:UIView, statusCode:RequestStatus, message:String,block:@escaping LoadingBlock) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        superView.addSubview(self)
        
        _mActivity = UIActivityIndicatorView.init()
        _mActivity?.bounds = CGRect.init(x: 0, y: 0, width: 45, height: 45)
        _mActivity?.centerX = self.width * 0.5
        _mActivity?.centerY = self.height * 0.5 - 50
        _mActivity?.style = UIActivityIndicatorView.Style.medium
        _mActivity?.color = UIColor.hexStringToColor(hexString: "#555555")
        _mActivity?.backgroundColor = UIColor.clear
        self.addSubview(_mActivity!)
        _mActivity?.startAnimating()
        
        
        mTitleLab = UILabel.init()
        mTitleLab?.textAlignment = NSTextAlignment.center
        mTitleLab?.font = UIFont.systemFont(ofSize: 18)
        mTitleLab?.textColor = UIColor.colorWithRGB(_r: 200, _g: 200, _b: 200)
        mTitleLab?.text = message
        mTitleLab?.numberOfLines = 10
        self.addSubview(mTitleLab!)
        mTitleLab?.snp.makeConstraints({ (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-50)
            make.left.equalTo(30)
            make.right.equalTo(-30)
        })
        
        mDetailLab = UILabel.init()
        mDetailLab?.textAlignment = NSTextAlignment.center
        mDetailLab?.font = UIFont.systemFont(ofSize: 12)
        mDetailLab?.textColor = UIColor.hexStringToColor(hexString: "#555555")
        mDetailLab?.text = "点击屏幕刷新"
        self.addSubview(mDetailLab!)
        mDetailLab?.snp.makeConstraints({ (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo((mTitleLab?.snp.bottom)!).offset(15)
        })
        
        
        let tap:UITapGestureRecognizer = UITapGestureRecognizer.init()
        tap.numberOfTapsRequired = 1
        self.addGestureRecognizer(tap)
        tap.addTarget(self, action: #selector(tapOver))
        
        
        _statusCode = statusCode
        _loadingBlock = block
        self.setUserInterfaceWithStatusCode()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func stopLoadingAnimation(){
        _mActivity?.stopAnimating()
    }
    
    @objc func tapOver(){
        if(RequestStatus.loading != _statusCode &&
            _loadingBlock != nil){
            _loadingBlock!(_statusCode!)
        }
    }
    
    func setUserInterfaceWithStatusCode(){
            
            //加载中
            if(_statusCode == RequestStatus.loading){
                _mActivity?.startAnimating()
                mTitleLab?.isHidden = true
                mDetailLab?.isHidden = true
                _mActivity?.isHidden = false
                print("加载中")
            }
            //加载失败
            else if(_statusCode == RequestStatus.failure){
                _mActivity?.stopAnimating()
                mTitleLab?.isHidden = false
                mDetailLab?.isHidden = false
                _mActivity?.isHidden = true
                print("加载失败")
            }
            //数据为空
            else{
                _mActivity?.stopAnimating()
                mTitleLab?.isHidden = false
                mDetailLab?.isHidden = false
                _mActivity?.isHidden = true
                print("数据为空")
            }
        
    }
    
}
