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

import Foundation
import UIKit
import SnapKit
import Combine


extension UIView {
    func pushToSkillCreate(skill: LNGameCategoryItemVO, info: LNCreateSkillInputFieldsVO) {
        let vc = LNSkillCreateViewController(skill: skill, info: info)
        navigationController?.pushViewController(vc, animated: true)
    }
}


class LNSkillCreateViewController: LNViewController {
    private let skill: LNGameCategoryItemVO
    private let scrollView = UIScrollView()
    private let info: LNCreateSkillInputFieldsVO
    private let editPanel = LNSkillFieldsEditView()
    private let confirmButton = UIButton()
    
    init(skill: LNGameCategoryItemVO, info: LNCreateSkillInputFieldsVO) {
        self.skill = skill
        self.info = info
        
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupViews()
        checkConfirmButton()
    }
}

extension LNSkillCreateViewController {
    private func commit() {
        guard editPanel.checkAvailable else {
            return
        }
        showLoading()
        DispatchQueue.global().async { [weak self] in
            guard let self else {
                dismissLoading()
                return
            }
            let voices = info.fields.filter({ $0.type == .voice })
            let group = DispatchGroup()
            var uploadDone = true
            for voice in voices {
                if let path = voice.value as? String,
                   !path.starts(with: "http") {
                    group.enter()
                    LNFileUploader.shared.startUpload(type: .voice, fileURL: URL(fileURLWithPath: path), progressHandler: nil)
                    { url, err in
                        if let err {
                            showToast(err)
                            uploadDone = false
                        } else if let url {
                            voice.value = url
                        } else {
                            uploadDone = false
                        }
                        group.leave()
                    }
                }
            }
            group.wait()
            
            if !uploadDone {
                dismissLoading()
                return
            }
            let input = LNCreateSkillFieldsInfo()
            input.bizCategoryCode = skill.code
            for item in info.fields {
                let config = LNCreateSkillFieldInfo()
                config.fieldCode = item.fieldCode
                config.value = item.value
                if item.type == .voice {
                    config.duration = item.duration
                }
                input.fields.append(config)
            }
            LNGameMateManager.shared.createSkill(info: input) { [weak self] success in
                dismissLoading()
                guard let self else { return }
                guard success else { return }
                view.pushToSkillReview()
            }
        }
    }
}

extension LNSkillCreateViewController: LNSkillFieldBaseEditViewDelegate {
    func onSkillFieldBaseEditViewInputChanged(view: LNSkillFieldBaseEditView) {
        checkConfirmButton()
    }
}

extension LNSkillCreateViewController {
    private func checkConfirmButton() {
        let allInput = editPanel.hasAllInput
        
        if allInput != confirmButton.isEnabled {
            confirmButton.isEnabled = allInput
            confirmButton.setBackgroundImage(allInput ? .primary_8 : nil, for: .normal)
        }
    }
    
    private func setupViews() {
        title = .init(key: "B00093")
        view.backgroundColor = .primary_1
        view.onTap { [weak self] in
            guard let self else { return }
            view.endEditing(true)
        }
        
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.contentInset = .init(top: 12, left: 0, bottom: -view.commonBottomInset + 32, right: 0)
        scrollView.adjustKeyoard()
        view.addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        scrollView.addSubview(editPanel)
        editPanel.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalToSuperview()
        }
        
        editPanel.delegate = self
        editPanel.update(skill.name, fields: info.fields)
        
        let bottomMenu = UIView()
        view.addSubview(bottomMenu)
        bottomMenu.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        let bottomGradient = CAGradientLayer()
        bottomGradient.colors = [
            UIColor.white.withAlphaComponent(0).cgColor,
            UIColor.white.cgColor,
            UIColor.white.cgColor
        ]
        bottomGradient.locations = [0, 0.5, 1]
        bottomGradient.startPoint = .init(x: 0, y: 0)
        bottomGradient.endPoint = .init(x: 0, y: 1)
        bottomMenu.layer.addSublayer(bottomGradient)
        bottomMenu.publisher(for: \.bounds).removeDuplicates().sink { [weak bottomGradient] newValue in
            guard let bottomGradient else { return }
            bottomGradient.frame = newValue
        }.store(in: &bag)
        
        confirmButton.setTitle(.init(key: "A00002"), for: .normal)
        confirmButton.setTitleColor(.text_1, for: .normal)
        confirmButton.titleLabel?.font = .heading_h3
        confirmButton.layer.cornerRadius = 23.5
        confirmButton.backgroundColor = .fill_4
        confirmButton.clipsToBounds = true
        confirmButton.isEnabled = false
        confirmButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            commit()
        }), for: .touchUpInside)
        bottomMenu.addSubview(confirmButton)
        confirmButton.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.bottom.equalToSuperview().offset(view.commonBottomInset)
            make.top.equalToSuperview()
            make.height.equalTo(47)
        }
    }
}

