//
//  GMVoiceSwitchView.swift
//  Gengmei
//
//  Created by 刘鹿杰 on 2019/11/12.
//  Copyright © 2019 更美互动信息科技有限公司. All rights reserved.
//

import Foundation

protocol GMVoiceSwitchViewDelegate : NSObjectProtocol {
    func switchViewDidSelected(isON: Bool)
}

class GMVoiceSwitchView : GMView {
    
    weak var delegate: GMVoiceSwitchViewDelegate?
    
    override func setup() {
        super.setup()
        // 语音开关
        self.backgroundColor = UIColor.clear
        self.addSubview(voiceSwitch)
        voiceSwitch.snp.makeConstraints { (make) in
            make.top.right.equalTo(0)
            make.size.equalTo(CGSize(width: 48, height: 27))
        }
        // 语音开关描述文字
        self.addSubview(voiceLabel)
        voiceLabel.snp.makeConstraints({ (make) in
            make.top.equalTo(voiceSwitch.snp_bottom).offset(10)
            make.right.equalTo(0)
        });
        voiceSwitch.isOn = !UserDefaults.standard.bool(forKey: "noHumanVoice")
        if UserDefaults.standard.bool(forKey: "noHumanVoice") {
            voiceLabel.text = "人声引导：关"
        }else {
            voiceLabel.text = "人声引导：开"
        }
    }
    
    // 懒加载人声引导提示
    lazy var voiceLabel : GMLabel = {
        let voiceLabel = GMLabel(textAlignment: .right, backgroundColor: .clear, textColor: .white, fontSize: 10)
        voiceLabel.layer.shadowColor = UIColor.black.cgColor
        voiceLabel.layer.shadowOpacity = 0.1
        voiceLabel.layer.shadowRadius = 5
        return voiceLabel
    }()
    
    @objc func switchAction(sender:UISwitch){
        if sender.isOn {
            UserDefaults.standard.set(false, forKey: "noHumanVoice")
            voiceLabel.text = "人声引导：开"
        }else {
            UserDefaults.standard.set(true, forKey: "noHumanVoice")
            voiceLabel.text = "人声引导：关"
        }
        Phobos.track("on_click_button", attributes:["page_name": "face_scan","tab_name" : "AI测肤","button_name" : sender.isOn ? "guide_voice_on" : "guide_voice_close"])
        if delegate != nil {  // 及时关闭声音
            delegate?.switchViewDidSelected(isON: sender.isOn)
        }
    }
    
    // MARK: - 懒加载控件
    // 懒加载人声引导开关switch
    lazy var voiceSwitch : UISwitch = {
        let voiceSwitch = UISwitch()
        voiceSwitch.onTintColor = UIColor(hex: 0x45E7DE).alpha(0.7) //设置开启状态显示的颜色
        voiceSwitch.tintColor = UIColor(white: 1.0, alpha: 0.5)   //设置关闭状态的颜色
        voiceSwitch.thumbTintColor = UIColor.white  //滑块上小圆点的颜色
        voiceSwitch.isOn = true
        voiceSwitch.addTarget(self, action: #selector(GMVoiceSwitchView.switchAction), for: .valueChanged)
        return voiceSwitch
    }()
    
}
