//
//  ___FILENAME___
//  ___PROJECTNAME___
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//
//  The template was designed by Hưng Nguyễn. 
//  Website, application (native/flutter), hosting, SEO.
//  Email: hungnguyen.it36@gmail.com
//  ___COPYRIGHT___
//

import RxSwift
import XMLMapper

protocol ___VARIABLE_ModuleName___Repository {
    func test(parameters: String) -> Observable<___VARIABLE_ModuleName___Model>
}

final class ___VARIABLE_ModuleName___RepositoryImpl: ___VARIABLE_ModuleName___Repository {
    func test(parameters: String) -> Observable<___VARIABLE_ModuleName___Model> {
        return Observable.create { observable in
            _ = APIRequest.shared.request(method: .post, soapAction: "", parameters: parameters)
                .asObservable()
                .subscribe { env in
                    let result = XMLMapper<___VARIABLE_ModuleName___Model>().map(XMLObject: env.body)
                    if let model = result {
                        if model.errorCode == nil || model.errorCode == "0" {
                            observable.onNext(model)
                        } else {
                            observable.onError(NSError(domain: "", code: 0, userInfo: ["error": model.errorMessage ?? ""]))
                        }
                    } else {
                        observable.onError(TrackerError.selfError)
                    }
                } onError: { error in
                    observable.onError(error)
                }
            return Disposables.create()
        }
    }
}


