//
//  LNSkillSettingMenu.swift
//  Gami
//
//  Created by OneeChan on 2026/1/25.
//

import Foundation
import UIKit
import SnapKit


class LNSkillSettingMenu: LNPopupView {
    private let skill: LNGameMateSkillDetailVO
    private let openSwitch = UISwitch()
    private let mainSkillSwitch = UISwitch()
    
    init(skill: LNGameMateSkillDetailVO, switchInfo: LNSkillSwitchResponse) {
        self.skill = skill
        super.init(frame: .zero)
        
        setupViews()
        
        openSwitch.isOn = switchInfo.open
        mainSkillSwitch.isOn = switchInfo.mainSkill
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNSkillSettingMenu {
    private func setupViews() {
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.spacing = 0
        
        container.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().offset(commonBottomInset)
        }
        
        let scaleX: CGFloat = 40.0 / 51.0
        let scaleY: CGFloat = 24.5 / 31.0
        openSwitch.onTintColor = .primary_5
        openSwitch.transform = CGAffineTransform(scaleX: scaleX, y: scaleY)
        openSwitch.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            showLoading()
            LNGameMateManager.shared.enableSkill(skillId: skill.id, open: openSwitch.isOn)
            { [weak self] success in
                dismissLoading()
                guard let self else { return }
                if !success {
                    openSwitch.isOn.toggle()
                }
            }
        }), for: .valueChanged)
        stackView.addArrangedSubview(buildMenuItem(.icWallet, title: .init(key: "A00169"), contentView: openSwitch))
        stackView.addArrangedSubview(buildLine())
        
        mainSkillSwitch.onTintColor = .primary_5
        mainSkillSwitch.onTintColor = .primary_5
        mainSkillSwitch.transform = CGAffineTransform(scaleX: scaleX, y: scaleY)
        mainSkillSwitch.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            showLoading()
            LNGameMateManager.shared.enableSkill(skillId: skill.id,
                                                 open: openSwitch.isOn, mainSkill: mainSkillSwitch.isOn)
            { [weak self] success in
                dismissLoading()
                guard let self else { return }
                if !success {
                    mainSkillSwitch.isOn.toggle()
                }
            }
        }), for: .valueChanged)
        stackView.addArrangedSubview(buildMenuItem(.icTag, title: .init(key: "B00096"), contentView: mainSkillSwitch))
        stackView.addArrangedSubview(buildLine())
    }
    
    private func buildMenuItem(_ icon: UIImage, title: String, contentView: UIView) -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.height.equalTo(50)
        }
        
        let ic = UIImageView()
        ic.image = icon.withRenderingMode(.alwaysTemplate)
        ic.tintColor = .text_5
        container.addSubview(ic)
        ic.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(18)
        }
        
        let titleLabel = UILabel()
        titleLabel.text = title
        titleLabel.font = .body_m
        titleLabel.textColor = .text_5
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.equalTo(ic.snp.trailing).offset(10)
            make.centerY.equalToSuperview()
        }
        
        container.addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-16)
        }
        
        return container
    }
    
    private func buildLine() -> UIView {
        let container = UIView()
        
        let line = UIView()
        line.backgroundColor = .fill_2
        container.addSubview(line)
        line.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.height.equalTo(1)
            make.horizontalEdges.equalToSuperview().inset(16)
        }
        
        return container
    }
}
