//
//  IrregularView.swift
//  SYMobilePortal
//
//  Created by xiejiangbo on 2018/4/26.
//  Copyright © 2018年 yin chen. All rights reserved.
//

import UIKit

class IrregularView: UIView {

    var maskLayer = CAShapeLayer()
    var borderPath = UIBezierPath()
    var scanCodeBlock:selectBlock!
    var customerServiceBlock:selectBlock!
    var humanFaceBlock:selectBlock!
    var setBlock:selectBlock!
    let titleArray = ["扫码追溯","智能客服","修改密码","部门切换"]
    //扫码追溯
    lazy var scanCodeBtn : UIButton = {
        let scanCodeBtn = UIButton.init(type: .custom)
        scanCodeBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        scanCodeBtn.titleLabel?.textAlignment = .center
        scanCodeBtn.setTitle("扫码追溯", for: .normal)
        scanCodeBtn.addTarget(self, action: #selector(scanCodeActionBlock), for: .touchUpInside)
        scanCodeBtn.setTitleColor(blackTitleColor, for: .normal)
        self.addSubview(scanCodeBtn)
        return scanCodeBtn
    }()
    //智能客服
    lazy var customerServiceBtn : UIButton = {
        let customerServiceBtn = UIButton.init(type: .custom)
        customerServiceBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        customerServiceBtn.titleLabel?.textAlignment = .center
        customerServiceBtn.setTitleColor(blackTitleColor, for: .normal)
        customerServiceBtn.setTitle("智能客服", for: .normal)
        customerServiceBtn.addTarget(self, action: #selector(customerServiceActionBlock), for: .touchUpInside)
        self.addSubview(customerServiceBtn)
        return customerServiceBtn
    }()
    //修改密码
    lazy var changePasswordBtn : UIButton = {
        let changePasswordBtn = UIButton.init(type: .custom)
        changePasswordBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        changePasswordBtn.titleLabel?.textAlignment = .center
        changePasswordBtn.setTitleColor(blackTitleColor, for: .normal)
        changePasswordBtn.setTitle("修改密码", for: .normal)
        changePasswordBtn.addTarget(self, action: #selector(changePasswordActionBlock), for: .touchUpInside)
        self.addSubview(changePasswordBtn)
        return changePasswordBtn
    }()
    //部门切换
    lazy var departmentwitchingBtn : UIButton = {
        let departmentwitchingBtn = UIButton.init(type: .custom)
        departmentwitchingBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        departmentwitchingBtn.titleLabel?.textAlignment = .center
        departmentwitchingBtn.setTitleColor(blackTitleColor, for: .normal)
        departmentwitchingBtn.setTitle("部门切换", for: .normal)
        departmentwitchingBtn.addTarget(self, action: #selector(departmentSwitchingActionBlock), for: .touchUpInside)
        self.addSubview(departmentwitchingBtn)
        return departmentwitchingBtn
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        maskLayer = CAShapeLayer()
        self.clipsToBounds = true
        self.layer.mask = maskLayer
        borderPath = UIBezierPath()
        initView()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.maskLayer.frame = self.bounds
        self.borderPath.move(to: CGPoint(x: 0, y: 7*HEIGHT_RATIO()))
        
        self.borderPath.addLine(to: CGPoint(x: 118*WHIDTH_RATIO(), y: 7*HEIGHT_RATIO()))
        self.borderPath.addLine(to: CGPoint(x: 121.5*WHIDTH_RATIO(), y: 0))
        self.borderPath.addLine(to: CGPoint(x: 125*WHIDTH_RATIO(), y: 7*HEIGHT_RATIO()))
        
        self.borderPath.addLine(to: CGPoint(x: self.bounds.width, y: 7*HEIGHT_RATIO()))
        self.borderPath.addLine(to: CGPoint(x: self.bounds.width, y:self.bounds.height))
        self.borderPath.addLine(to: CGPoint(x: 0, y:self.bounds.height))
        self.borderPath.addLine(to: CGPoint(x: 0, y: 7*HEIGHT_RATIO()))
        self.maskLayer.path = self.borderPath.cgPath
        
    }
    func addBlock(scanCodeBlock:@escaping selectBlock,customerServiceBlock:@escaping selectBlock,humanFaceBlock:@escaping selectBlock)  {
        self.scanCodeBlock = scanCodeBlock
        self.customerServiceBlock = customerServiceBlock
        self.humanFaceBlock = humanFaceBlock
    }
    func initView(){
        
        let w = self.frame.size.width
        let h = 187*HEIGHT_RATIO()-7*HEIGHT_RATIO()
        let btnH = h/CGFloat(titleArray.count)
        scanCodeBtn.frame = CGRect(x:0, y: 7*HEIGHT_RATIO(), width: w, height:btnH )
        customerServiceBtn.frame = CGRect(x:0, y: btnH+7*HEIGHT_RATIO(), width: w, height:btnH )
        changePasswordBtn.frame = CGRect(x:0, y: btnH*2+7*HEIGHT_RATIO(), width: w, height:btnH )
        departmentwitchingBtn.frame = CGRect(x:0, y: btnH*3+7*HEIGHT_RATIO(), width: w, height:btnH )
        for index in 0..<titleArray.count {
            if index != titleArray.count{
                let linView = self.getLineView()
                linView.frame = CGRect(x:5*WHIDTH_RATIO(), y: btnH*CGFloat(index+1)+7*HEIGHT_RATIO(), width:w-10*WHIDTH_RATIO() , height:1*HEIGHT_RATIO() )
            }
        }
    }
    func initInfoNotCompanyView() {
        let w = self.frame.size.width
        let h = 187*HEIGHT_RATIO()-7*HEIGHT_RATIO()
        let btnH = h/CGFloat(titleArray.count)
        scanCodeBtn.isHidden = true
        changePasswordBtn.isHidden = false
        customerServiceBtn.frame = CGRect(x:0, y: 7*HEIGHT_RATIO(), width: w, height:btnH )
        changePasswordBtn.frame = CGRect(x:0, y: btnH+7*HEIGHT_RATIO(), width: w, height:btnH )
        departmentwitchingBtn.frame = CGRect(x:0, y: btnH*2+7*HEIGHT_RATIO(), width: w, height:btnH )
    }
    func initInfoCompanyView() {
        let w = self.frame.size.width
        let h = 187*HEIGHT_RATIO()-7*HEIGHT_RATIO()
        let btnH = h/CGFloat(titleArray.count)
        scanCodeBtn.isHidden = false
        changePasswordBtn.isHidden = false
        scanCodeBtn.frame = CGRect(x:0, y: 7*HEIGHT_RATIO(), width: w, height:btnH )
        customerServiceBtn.frame = CGRect(x:0, y: btnH+7*HEIGHT_RATIO(), width: w, height:btnH )
        changePasswordBtn.frame = CGRect(x:0, y: btnH*2+7*HEIGHT_RATIO(), width: w, height:btnH )
        departmentwitchingBtn.frame = CGRect(x:0, y: btnH*3+7*HEIGHT_RATIO(), width: w, height:btnH )
    }
    
    func btnAction(sender:UIButton){
        if sender.tag == 100 {
            self.scanCodeBlock()
        }else if sender.tag == 101{
            self.customerServiceBlock()
        }else if sender.tag == 102{
            self.humanFaceBlock()
        }else if sender.tag == 103{
            self.setBlock()
        }
    }
    @objc func scanCodeActionBlock(){
        self.scanCodeBlock()
    }
    @objc func customerServiceActionBlock(){
        self.customerServiceBlock()
    }
    @objc func changePasswordActionBlock(){
        self.humanFaceBlock()
    }
    @objc func departmentSwitchingActionBlock(){
        self.setBlock()
    }
    func getLineView()->UIImageView{
        let lineView = UIImageView.init()
        lineView.backgroundColor = lineColor
        self.addSubview(lineView)
        return lineView
    }
    func getUIbutton()->UIButton{
        let buttonView = UIButton.init(type: .custom)
        buttonView.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        buttonView.titleLabel?.textAlignment = .center
        buttonView.setTitleColor(blackTitleColor, for: .normal)
        //buttonView.backgroundColor = UIColor.red
        self.addSubview(buttonView)
        return buttonView
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
