//
//  UserDetailsRegisterVM.swift
//  WOKA
//
//  Created by MacBook Pro on 29/04/24.
//

import UIKit
import Alamofire

class UserDetailsRegisterVM{
    
    weak var vc : UserDetailsRegisterVC!
    
    // This will become true only if parent is adding child
    var createChildAccount : Bool?
    
    func initView(){
        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()
        tfMod(textField: [vc.enterNameTF, vc.enterPasswordTF, vc.enterUserNameTF])
        hindiEnglishLanguage()
        
        vc.enterNameTF.delegate = self.vc
        vc.enterPasswordTF.delegate = self.vc
        vc.enterUserNameTF.delegate = self.vc
        
        //Adding eye to the password tf
        vc.enterPasswordTF.enablePasswordToggle()
        vc.enterPasswordTF.rightView?.isHidden = true
        
        if createChildAccount != nil{ // this is from the home screen when a parent can adda child
            vc.dontSharePassLabel.isHidden = true
            vc.viewTitleLabel.text = "Add Child Account".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
            vc.userNameTitleLabel.text = "USERNAME".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
            vc.nameTitleLabel.text = "CHILD NAME?".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
            vc.passTitleLabel.text = "PASSWORD".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
        }else{
            AuthFunc.shareInstance.userType == .adult ? (vc.dontSharePassLabel.isHidden = true) : (vc.dontSharePassLabel.isHidden = false)
        }
    }
    
    func hindiEnglishLanguage(){
        vc.enterNameTF.placeholder = K.AuthenticationStringConstant.enterName.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
        vc.enterUserNameTF.placeholder = K.AuthenticationStringConstant.selectUsername.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
        vc.enterPasswordTF.placeholder = K.AuthenticationStringConstant.enterPassword.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
    }
    
    private func tfMod(textField : [UITextField]){
        for i in textField{
            i.roundCorner(radius: i.frame.height / 2)
            i.delegate = vc.self
        }
    }
    
    // MARK: - Get Intrests
    
    func checkUsernameExist(onCompletion: @escaping (Bool) -> Void){
        let params: Parameters = [
            "email": AuthFunc.shareInstance.regData.email!,
            "username": vc.enterUserNameTF.text!,
            "user_type": (createChildAccount != nil) ? "1" : (AuthFunc.shareInstance.userType == .adult ? "2" :"1")
        ]
        let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi"]

        Utilities.startProgressHUD(msg: K.ConstantString.userNameVerify)
        NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Auth.check_exist_username, method: .post,parameters: params, headers: headers) {(result : Result<BaseResponseModel<IntrestTopicDM>, NetworkManager.APIError>) in
            switch result{
            case .success(let data):
                //0 -> Username already used. , 1-> Username available
                switch data.success{
                case 0:
                    Utilities.dismissProgressHUD()
                    self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 2)
                    onCompletion(false)
                case 1:
                    Utilities.dismissProgressHUD()
                    onCompletion(true)
                default:
                    break
                }
            case .failure(let error):
                Utilities.dismissProgressHUD()
                self.vc.toast(msg: error.localizedDescription, time: 2)
                onCompletion(false)
            }
        }
    }
}
