//
//  NotificationSetUpController.swift
//  OrderScheduling
//
//  Created by 中道 on 2023/8/29.
//

import Foundation
import DDControlsKit_Private
import SnapKit
import DDAutoUIKit_Private
import RxSwift
import RxCocoa

class NotificationSetUpModel {
    enum typeEnum : Int {
        case sound = 0
        case alert
    }
    var name : String
    var type : typeEnum
    var isOn : Bool
    init(name: String, type:typeEnum, isOn: Bool) {
        self.name = name
        self.type = type
        self.isOn = isOn
    }
}

extension NotificationSetUpController {
    func addActions() {
        
    }
}

// MARK: UITableViewDelegate,UITableViewDataSource
extension NotificationSetUpController : UITableViewDelegate,UITableViewDataSource {
    public func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 1
        }
        return resultArr.count
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? NotificationSetUpCell
        if cell == nil {
            cell = NotificationSetUpCell(style: .default, reuseIdentifier: "cell")
        }
        if indexPath.section == 0 {
            cell?.arrow.isHidden = false
            cell?.switchButton.isHidden = true
            cell?.nameLabel.text = "前往设置"
        }
        
        if indexPath.section == 1 {
            let model = resultArr[indexPath.item]
            cell?.arrow.isHidden = true
            cell?.switchButton.isHidden = false
            cell?.nameLabel.text = model.name
            cell?.switchButton.isOn = model.isOn
            cell?.switchButton.rx.isOn
                .subscribe(onNext: { isOn in
                    if model.type == .sound {
                        GROUP.setNewMessageSound(newMessageSound: isOn)
                    }else if model.type == .alert {
//                        GROUP.setNewMessageAlert(newMessageAlert: isOn)
                    }
                })
                .disposed(by: cell!.disposeBag)
        }
        
        return cell!
    }
    
    public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = DDView()
        view.backgroundColor = .white
        let label = DDLabel.dd_init(withText: "", font: .mediumFont(auto(13)), textColor: .hex("000000").alpha(0.3))
        view.addSubview(label)
        label.snp.makeConstraints { make in
            make.left.equalTo(auto(20))
            make.bottom.equalToSuperview()
        }
        let line = DDView()
        line.isHidden = true
        line.backgroundColor = .hex("F1F2F5")
        view.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        if section == 0 {
            label.text = "应用在后台"
        }else{
            label.text = "应用在前台"
        }
        return view
    }
    
    public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return auto(30)
    }

    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            NOTIAUTHTOOL.openSetting()
        }
    }
    
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return auto(44)
    }
}

open class NotificationSetUpController : ZDViewController {
    private let notificationSetUpView = NotificationSetUpView()
    private var resultArr : [NotificationSetUpModel] = []
    private let disposeBag = DisposeBag()
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        dd_navigationItemTitle = "通知提醒"
        dd_navigationBarBackgroundColor = .hex("354683")
        dd_navigationBarTitleTextAttributes = [.foregroundColor : UIColor.white(alpha: 0.7),.font:UIFont.mediumFont(17)]
        dd_navigationBarBarButtonItemAttributes = [.foregroundColor : UIColor.white]
        
        let soundModel = NotificationSetUpModel(name: "新订单提示音", type: .sound, isOn: GROUP.newMessageSound)
//        let alertModel = NotificationSetUpModel(name: "新订单弹窗", type: .alert, isOn: GROUP.newMessageAlert)
        resultArr.append(soundModel)
//        resultArr.append(alertModel)
        
        addSubviews()
        addActions()
    }
    
    func addSubviews() {
        view.backgroundColor = .hex("F4F5F7")
        view.addSubview(notificationSetUpView)
        notificationSetUpView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(-view.safeAreaInsets.bottom)
        }

        notificationSetUpView.tableView.delegate = self
        notificationSetUpView.tableView.dataSource = self
    }
    
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
}

open class NotificationSetUpView : DDView {
    public let tableView : DDTableView
    public override init(frame: CGRect) {
        tableView = DDTableView(frame: CGRectZero, style: .plain)
        super.init(frame: frame)
        
        tableView.backgroundColor = .white
        tableView.separatorStyle = .none
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0
        } else {
            // Fallback on earlier versions
        }
        addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class NotificationSetUpCell : DDTableViewCell {
    public let radiusView : DDView
    public let nameLabel : DDLabel
    public let switchButton : UISwitch
    public let arrow : DDImageView
    public let line : DDView
    public var disposeBag : DisposeBag
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        radiusView = DDView()
        nameLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(14)), textColor: .hex("000000").alpha(0.8))
        switchButton = UISwitch()
        arrow = DDImageView()
        line = DDView.init()
        disposeBag = DisposeBag()
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        
        contentView.addSubview(radiusView)
        radiusView.addSubview(nameLabel)
        radiusView.addSubview(switchButton)
        arrow.image = UIImage(named: "mine_info_arrow")
        radiusView.addSubview(arrow)
        line.backgroundColor = .hex("F1F2F5")
        radiusView.addSubview(line)
        
        radiusView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(auto(20))
            make.centerY.equalToSuperview()
        }
        
        switchButton.snp.makeConstraints { make in
            make.right.equalTo(-auto(20))
            make.centerY.equalToSuperview()
        }
        
        arrow.snp.makeConstraints { make in
            make.right.equalTo(-auto(20))
            make.centerY.equalToSuperview()
        }
        
        line.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.left.equalTo(auto(20))
            make.right.equalTo(-auto(20))
            make.height.equalTo(1)
        }
    }
    
   public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        disposeBag = DisposeBag()
    }
}
