import moment from 'moment';
import { validationCPF, validationEmail, validationPhone, validationDate } from '@/utils/validation.js'
import AddressModel from "@/models/AddressModel.js";
/**
 * @type {SelfSignupModel}
 */


export default class SelfSignupModel {



    constructor(selfSignup = {}) {
        this.cpf = (selfSignup.cpf)? selfSignup.cpf : "";
        this.name = (selfSignup.name)? selfSignup.name: "";
        this.email = (selfSignup.email)? selfSignup.email: '';
        this.email_confirmation = (selfSignup.email)? selfSignup.email : "";
        this.document_identification_type = (selfSignup.document_identification_type)? selfSignup.document_identification_type : 'RG';
        this.phone = (selfSignup.contact)? selfSignup.contact.ddd + selfSignup.contact.number :  "";
       this.setBirthDate(selfSignup); 
        this.phone_verification="";
        this.setShippingAddress(selfSignup);
        this.setBillingAddress(selfSignup);
       this.document_identification_selfie_url = (selfSignup.document_identification_selfie_url)? selfSignup.document_identification_selfie_url : "";
       this.document_identification_photo_front_url = (selfSignup.document_identification_photo_front_url)? selfSignup.document_identification_photo_front_url : "";
       this.document_identification_photo_back_url = (selfSignup.document_identification_photo_back_url)? selfSignup.document_identification_photo_back_url : ""; 
    this.setCampaign(selfSignup);
    this.setSector(selfSignup);
    }

    setBirthDate(selfSignup){
        this.birth_date = (selfSignup.birth_date)? new moment(selfSignup.birth_date).format('DD/MM/YYYY'): '';
    }
    setShippingAddress(selfSignup){
        if(selfSignup.shipping_address){
            this.shipping_address = new AddressModel(selfSignup.shipping_address);
            }else{
                this.shipping_address = new AddressModel();
            }
    }
    setBillingAddress(selfSignup){
        if(selfSignup.billing_address){
            this.billing_address = new AddressModel(selfSignup.billing_address);
            }else{
                this.billing_address = new AddressModel();
            }
    }

    setCampaign(selfSignup){


        if(selfSignup.campaign){
            this.campaign = selfSignup.campaign;
        }
        
        this.campaign_id = selfSignup.campaign_id;
    }
   
    setSector(selfSignup){


        if(selfSignup.sector){
            this.sector = selfSignup.sector;
            this.sector_name = selfSignup.sector.name;
        }

    }

    nameIsValid(){
        if(this.name.split(" ").length < 2 || this.name.length < 8){
            return false;
        }

        return true;
    }

    emailConfirmationIsValid(){
        return validationEmail(this.email_confirmation) 
        && this.email.toLowerCase() === this.email_confirmation.toLowerCase();
    }


    birthDateIsValid(){
       
        
        if(!validationDate(this.birth_date)){
            return false;
        }

        let mDate = new moment(this.birth_date,'DD/MM/YYYY');
        let now = new moment();
        
        return  Math.floor(now.diff(mDate, 'years',true)) >= 18 ;

    }


    billingAddressIsValid(){
        return this.billing_address.isValid();
    }

    shippingAddressIsValid(){
        return this.shipping_address.isValid();
    }

    isValid() {
        return validationCPF(this.cpf) && validationEmail(this.email) && validationPhone(this.phone)
        && this.nameIsValid() && this.emailConfirmationIsValid() && this.birthDateIsValid();
    }



}