//
//  LAFilterJobToolBar.swift
//  Bole
//
//  Created by 张思源 on 2018/11/27.
//  Copyright © 2018 huatec. All rights reserved.
//

import UIKit

@objc protocol LAFilterJobToolBarDelegate {
    
    @objc optional func clickActionWithButton(sender : LACustomButton)
    
}

class LAFilterJobToolBar: UIView, Lineable {
    private let fontSize : CGFloat = 12
    //颜色
    private let btnTitleColor : UIColor = UIColor.hexColor(0x888888)
    private let lineColor = UIColor.hexColor(0xDDDDDD)
    //图片
    private let btnImageNormal = UIImage(named: "arrow_down")
    private let btnImageSelected = UIImage(named: "arrow_up")
    
    var contentView : UIView?
    
    private var selectedBtn : LACustomButton?
    
    //第一个按钮
    var fristButton : LACustomButton?
    //地点Button
    var regionButton : LACustomButton?
    //第三个按钮公司
    var companyButton : LACustomButton?
    //公司要求
    var requirementButton : LACustomButton?

    //代理
    weak var delegate : LAFilterJobToolBarDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    
        setupUI()
        
        NotificationCenter.default.addObserver(self, selector: #selector(LAFilterJobToolBar.presentViewControllerWillDismiss(noti:)), name: NSNotification.Name(kPresentControllerWillDismissNotification), object: nil)

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        let contentView = UIView()
        contentView.backgroundColor = UIColor.hexColor(0xFFFFFF)
        self.addSubview(contentView)
        self.contentView = contentView
        
        let button1 = LACustomButton(type: .custom)
        button1.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button1.setTitleColor(btnTitleColor, for: .normal)
        button1.setTitleColor(kThemeForegroundUIColor, for: .selected)
        button1.setTitle("最新", for: .normal)
        button1.titleLabel?.textAlignment = .right
        button1.setImage(btnImageNormal, for: .normal)
        button1.setImage(btnImageSelected, for: .selected)
        button1.tag =  20000
        button1.addTarget(self, action: #selector(LAFilterJobToolBar.tapButtonWithTag(sender:)), for: .touchUpInside)
        
        contentView.addSubview(button1)
        self.fristButton = button1
        
        let line1 = UIView()
        line1.backgroundColor = lineColor
        contentView.addSubview(line1)
        
        
        let button2 = LACustomButton(type: .custom)
        button2.titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        button2.titleLabel?.textAlignment = .right
        button2.setTitleColor(kThemeForegroundUIColor, for: .selected)
        button2.setTitleColor(btnTitleColor, for: .normal)
        button2.setTitle("定位中", for: .normal)
        button2.setImage(btnImageNormal, for: .normal)
        button2.setImage(btnImageSelected, for: .selected)
        button2.tag =  20001
        button2.addTarget(self, action: #selector(LAFilterJobToolBar.tapButtonWithTag(sender:)), for: .touchUpInside)
        contentView.addSubview(button2)
        regionButton = button2
        
        let line2 = UIView()
        line2.backgroundColor = lineColor
        contentView.addSubview(line2)
        
        let button3 = LACustomButton(type: .custom)
        button3.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button3.titleLabel?.textAlignment = .right
        button3.setTitleColor(btnTitleColor, for: .normal)
        button3.setTitleColor(kThemeForegroundUIColor, for: .selected)
        button3.setTitle("公司", for: .normal)
        button3.setImage(btnImageNormal, for: .normal)
        button3.setImage(btnImageSelected, for: .selected)
        button3.tag = 20002
        button3.addTarget(self, action: #selector(LAFilterJobToolBar.tapButtonWithTag(sender:)), for: .touchUpInside)
        contentView.addSubview(button3)
        self.companyButton = button3

        let line3 = UIView()
        line3.backgroundColor = lineColor
        contentView.addSubview(line3)
        
        let button4 = LACustomButton(type: .custom)
        button4.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button4.titleLabel?.textAlignment = .right
        button4.setTitleColor(btnTitleColor, for: .normal)
        button4.setTitleColor(kThemeForegroundUIColor, for: .selected)
        button4.setTitle("要求", for: .normal)
        button4.setImage(btnImageNormal, for: .normal)
        button4.setImage(btnImageSelected, for: .selected)
        button4.tag = 20003
        button4.addTarget(self, action: #selector(LAFilterJobToolBar.tapButtonWithTag(sender:)), for: .touchUpInside)
        contentView.addSubview(button4)
        self.requirementButton = button4
        
        contentView.snp.makeConstraints { (make) in
            make.left.right.bottom.top.equalTo(self)
        }
        let btnW = kScreenW / 4
        
        button1.snp.makeConstraints { (make) in
            make.left.equalTo(contentView.snp.left)
            make.centerY.equalTo(contentView.snp.centerY)
            make.size.equalTo(CGSize(width: btnW, height: 40))
        }
        
        line1.snp.makeConstraints { (make) in
            make.left.equalTo(button1.snp.right)
            make.centerY.equalTo(contentView.snp.centerY)
            make.size.equalTo(CGSize(width: 0.5, height: 25))
        }
        
        button2.snp.makeConstraints { (make) in
            make.left.equalTo(line1.snp.right)
            make.centerY.equalTo(contentView.snp.centerY)
            make.size.equalTo(CGSize(width: btnW, height: 40))
        }
        
        line2.snp.makeConstraints { (make) in
            make.centerY.equalTo(contentView.snp.centerY)
            make.centerX.equalTo(contentView.snp.centerX)
            make.size.equalTo(CGSize(width: 0.5, height: 25))
        }
        
        button3.snp.makeConstraints { (make) in
            make.left.equalTo(line2.snp.right)
            make.centerY.equalTo(contentView.snp.centerY)
            make.size.equalTo(CGSize(width: btnW, height: 40))
        }
        
        line3.snp.makeConstraints { (make) in
            make.left.equalTo(button3.snp.right)
            make.centerY.equalTo(contentView.snp.centerY)
            make.size.equalTo(CGSize(width: 0.5, height: 25))
        }
        
        button4.snp.makeConstraints { (make) in
            make.left.equalTo(line3.snp.right)
            make.centerY.equalTo(contentView.snp.centerY)
            make.size.equalTo(CGSize(width: btnW, height: 40))
        }
        
        bottomLine()
    }
    
    @objc func presentViewControllerWillDismiss(noti: Notification) {
        LALog("页面即将消失")
        
        selectedBtn?.isSelected = false
    }
    
    @objc private func tapButtonWithTag(sender : LACustomButton) {
//        sender.isSelected = !sender.isSelected
//        LALog("点击了:\(sender.tag)")
        
        if (selectedBtn == nil) {
            sender.isSelected = true
            selectedBtn = sender
        }else if(selectedBtn != nil && selectedBtn == sender){
            selectedBtn?.isSelected = !sender.isSelected
        }else if(selectedBtn != nil && selectedBtn != sender){
            selectedBtn?.isSelected = false
            sender.isSelected = true
            selectedBtn = sender
        }
        
        if let od = self.delegate {
            od.clickActionWithButton?(sender: sender)
        }
        
    }
    
    /// 展示地名
    func changeAddress(cityName: String) {
        regionButton?.setTitle(cityName, for: UIControl.State.normal)
    }
    
    /// 改变第一个条件文字
    func changeFristCondition(_ conditionName : String) {
        fristButton?.setTitle(conditionName, for: .normal)
    }
    
    /// 改变公司筛选条件
    func changeCompanyCondition(_ condition : String) {
        companyButton?.setTitle(condition, for: .normal)
    }
    
    /// 改变要求
    func changeRequirementConditionName(_ condition : String) {
        requirementButton?.setTitle(condition, for: .normal)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }

}


