//
//  MyTabbar.swift
//  MobilePortalTwo
//
//  Created by xiejiangbo on 2019/6/18.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit

class MyTabbar: UITabBar {
    var actionBlock:selectBlock!
    
    lazy var publishBtn: PublishBtn = {
        let publishBtn:PublishBtn = PublishBtn.init(type: .custom)
        publishBtn.setBackgroundImage(UIImage.init(named: "home_image"), for: .normal)
        publishBtn.sizeToFit()
//        publishBtn.backgroundColor = UIColor.yellow
        publishBtn.addTarget(self, action: #selector(publishBtnAction), for: .touchUpInside)
        return publishBtn
    }()
    
    lazy var myTabbarImageView:UIImageView = {
        let myTabbarImageView = UIImageView.init(frame: CGRect(x:0, y: -56, width:SCREEN_WHIDTH() , height:56 ))
        myTabbarImageView.image = UIImage.init(named: "tabbar_image")
        myTabbarImageView.contentMode = .scaleToFill
//        ribbonItemImageView.backgroundColor = UIColor.black
        return myTabbarImageView
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(myTabbarImageView)
        self.addSubview(publishBtn)
        NotificationCenter.default.addObserver(self, selector: #selector(publishBtnEetNum), name: NSNotification.Name(rawValue: "publishBtnEetNum"), object: nil)
    }
    @objc func publishBtnEetNum() {
        self.publishBtn.setNum(numBer: judgmentStrNil(str: AccountInfo.sharedInstance().UnReadNumber))
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.publishBtn.center = CGPoint.init(x: SCREEN_WHIDTH()/2, y: 49/2-10)
        
//        self.myTabbarImageView.center = CGPoint.init(x: SCREEN_WHIDTH()/2, y: 49/2-10)
        
        let buttonY:CGFloat = 0
        let buttonW:CGFloat = SCREEN_WHIDTH()/5
        let buttonH:CGFloat = 49
        var index:Int = 0
        for i in 0..<self.subviews.count {
            let button = self.subviews[i]
            if button .isKind(of: NSClassFromString("UITabBarButton")!) == false {
                continue
            }
            if button.tag == 0 {
                button.frame = CGRect(x: CGFloat(index) * buttonW, y: buttonY, width: buttonW, height: buttonH)
                index = index + 1
                if index == 2 {
                    index = index + 1
                }
                button.tag = 100 + index
            } else if button.tag != 0 {
                if button.tag == 103 {
                    button.frame = CGRect(x: CGFloat(button.tag-102) * buttonW, y: buttonY, width: buttonW, height: buttonH)
                } else {
                    button.frame = CGRect(x: CGFloat(button.tag-101) * buttonW, y: buttonY, width: buttonW, height: buttonH)
                }
            }
        }
    }
    @objc func publishBtnAction (){
        self.actionBlock()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
