import * as constants from './constants';
import { fromJS } from 'immutable';

import * as config from '../config';
import axios from '../axios_auth';

import md5 from 'js-md5';
import CryptoJs from 'crypto-js';
const CRYPTO_KEY = 'cld2_login_csrf_';

//csrfToken
export const loginSafety = () => {
    return (dispatch) => {

        axios.get(config.CLD2API + '/loginSafety').then((res) => {
            const data = res.data;
            if (res.status === 200) {
                var index = CryptoJs.AES.decrypt(data.t11, CryptoJs.enc.Utf8.parse(CRYPTO_KEY), {
                    iv: CryptoJs.enc.Utf8.parse('4875sdfw895scx85'),
                    mode: CryptoJs.mode.CBC,
                    format: CryptoJs.format.Hex,
                }).toString(CryptoJs.enc.Utf8);;
                let hashKeys = Object.keys(data);
                let values = Object.values(data); 
                let csrfToken = '';
                hashKeys.forEach(function (v, i) {
                    if (v == 't' + index) {
                        csrfToken = values[i];
                    }
                });
                dispatch({
                    type: constants.CHANGE_CSRF_TOKEN,
                    data: md5(csrfToken+index)
                });
            } else { }
        }).catch((e) => {
            console.log(e)
        })
    }
}












