//
//  NewPasswordVM.swift
//  WOKA
//
//  Created by Bilal on 10/05/2024.
//

import UIKit
import Alamofire

class NewPasswordVM{
    
    weak var vc : NewPasswordVC!
    var userName : String?
    
    func initView(){
        vc.enterNewPasswordTF.enablePasswordToggle()
        vc.confirmPassTF.enablePasswordToggle()
        vc.enterNewPasswordTF.delegate = self.vc
        vc.confirmPassTF.delegate = self.vc
        
        vc.enterNewPasswordTF.roundCorner()
        vc.confirmPassTF.roundCorner()
        let color1 = #colorLiteral(red: 0.144693464, green: 0.1426281333, blue: 0.6686832905, alpha: 1)
        let color2 = #colorLiteral(red: 0.6901960784, green: 0.2745098039, blue: 0.7568627451, alpha: 1)
        vc.nextBtn.applyGradient(colors: [color1, color2], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0.8, y: 0))
        vc.nextBtn.roundCorner()
    }
    
    func updatePassword(){
        guard let userName else{return}
        let params: Parameters = [
            "username": userName,
            "password": vc.enterNewPasswordTF.text!,
            "password_confirmation": vc.confirmPassTF.text!
        ]
        let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi"]

        Utilities.startProgressHUD()
        NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Auth.update_password, method: .post ,parameters: params,headers: headers) {(result : Result<BaseResponseModel<ForgotPassDM>, NetworkManager.APIError>) in
            switch result{
            case .success(let data):
                switch data.success{
                case 0: // some error
                    Utilities.dismissProgressHUD()
                    self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 2)
                case 1: // Success
                    Utilities.dismissProgressHUD()
                    self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 1) {
                        UIApplication.setRootView(LoginNavVC.instantiate(from: .AuthenticationSB))
                    }
                default:
                    Utilities.dismissProgressHUD()
                    self.vc.toast(msg: K.ConstantString.unRecognised , time: 1)
                }
            case .failure(let error):
                Utilities.dismissProgressHUD()
                self.vc.toast(msg: error.localizedDescription , time: 2)
            }
        }
    }
}
