//
//  SpeakEleTabbarViewCtr.swift
//  SpeakEasyLearnEnglish
//
//  Created by edy on 2025/7/3.
//

import UIKit

class SpeakEleTabbarViewCtr: UIViewController {

    public var selectIndex = 0 {
        didSet {
            for (i,but) in tabBarItems.enumerated() {
                but.isSelected = (i == selectIndex)
            }
            UIView.animate(withDuration: 0.35, delay: 0, options: .curveEaseOut) {
                self.scroll.contentOffset = CGPointMake(CGFloat(self.selectIndex) * self.scroll.width, 0)
            }
        }
    }
    
    private var tabBarItems:[TabbarButton] = [] {
        didSet {
            stack.arrangedSubviews.forEach({stack.removeArrangedSubview($0)})
            tabBarItems.forEach({stack.addArrangedSubview($0)})
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        AddchildVC()
        setup()
        selectIndex = 0
    }
    
    private func AddchildVC() -> Void {
        let home = TabbarButton(title: "Home", normalImage: nil, selectImage: nil)
        let homevc = SpeakEleHomeViewCtr()
        self.addChild(homevc)
        contentStack.addArrangedSubview(homevc.view)
        
        let set = TabbarButton(title: "Setting", normalImage: nil, selectImage: nil)
        let setvc = SpeakEleSetViewCtr()
        self.addChild(setvc)
        contentStack.addArrangedSubview(setvc.view)
        
        let pro = TabbarButton(title: "Progress", normalImage: nil, selectImage: nil)
        let provc = SpeakEleProgressViewCtr()
        self.addChild(provc)
        contentStack.addArrangedSubview(provc.view)
        

        
        tabBarItems = [
            home,
            set,
            pro
        ]
        for (i,btn) in tabBarItems.enumerated() {
            btn.tag = i
            btn.addTarget(self, action: #selector(SpeakTouches(_:)), for: .touchUpInside)
        }
    }
    
    @objc private func SpeakTouches(_ sender:UIButton) -> Void {
        if sender.tag == selectIndex {
            return
        }
        selectIndex = sender.tag
    }
    
    private func setup() -> Void {
        scroll.snp.makeConstraints { make in
            make.left.right.bottom.top.equalToSuperview()
        }
        contentStack.snp.makeConstraints { make in
            make.left.top.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(self.tabBarItems.count)
            make.height.equalToSuperview()
        }
        stack.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().inset(Dev.safeAreaInsets.bottom)
            make.height.equalTo(Dev.tabHeight)
        }
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.view.layoutIfNeeded()
        view.bringSubviewToFront(stack)
        scroll.contentSize = CGSizeMake(CGFloat(self.tabBarItems.count) * scroll.width, 0)
    }
    
    private lazy var stack: UIStackView = {
        let stack = UIStackView()
        stack.spacing = 0
        stack.distribution = .fillEqually
        stack.alignment = .fill
        stack.axis = .horizontal
        view.addSubview(stack)
        return stack
    }()
    
    private lazy var scroll: UIScrollView = {
        let scroll = UIScrollView()
        scroll.isPagingEnabled = true
        scroll.isScrollEnabled = false
        scroll.showsVerticalScrollIndicator = false
        scroll.showsHorizontalScrollIndicator = false
        view.addSubview(scroll)
        return scroll
    }()
    
    private lazy var contentStack: UIStackView = {
        let stack = UIStackView()
        stack.axis = .horizontal
        stack.spacing = 0
        stack.distribution = .fillEqually
        stack.alignment = .fill
        scroll.addSubview(stack)
        return stack
    }()
}


class TabbarButton: UIButton {
    
    convenience init(title:String?, normalImage:String?, selectImage:String?) {
        self.init(type: .custom)
        var configs = UIButton.Configuration.filled()
        configs.title = title
        configs.baseBackgroundColor = .clear
        configs.baseForegroundColor = .gray
        configs.imagePlacement = .top
        configs.imagePadding = 2
        configs.titleAlignment = .center
        configs.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer({ containar in
            var att = containar
            att.font = UIFont.systemFont(ofSize: 12, weight: .regular)
            return att
        })
        self.configuration = configs
        self.configurationUpdateHandler = { button in
            var updateConfigs = button.configuration
            if button.isSelected {
                updateConfigs?.baseForegroundColor = .init(with: "#17CB81")
                guard let simg = selectImage else { return }
                updateConfigs?.image = UIImage(named: simg)?.withRenderingMode(.alwaysOriginal)
            }else{
                updateConfigs?.baseForegroundColor = .gray
                guard let img = normalImage else { return}
                updateConfigs?.image = UIImage(named: img)?.withRenderingMode(.alwaysOriginal)
            }
            button.configuration = updateConfigs
        }
    }
    
}
