import {Module} from "vuex";

import userConfig from '$config/user';

import storage from '$utils/tool/storage';

import { DateFormat } from '@/utils/tool/date';

import $utils from '$utils/tool/utils';

import $request from '$utils/request';

import money from '$utils/tool/money';
import request from "$utils/request";
import socket from "$utils/socket";

export default <Module<any,any>>{

    state:{
        user:{},
        shield:{}
    },

    getters:{
        isLogin(state){
          return !!(state.user && state.user.login_token);
        },
        // 年龄
        age(state){

          if (state.user.birthday) {

              let nowYear = new Date().getFullYear();

              let usePeople = DateFormat.inverse(state.user.birthday,'YYYY-MM-DD');

              let age = Math.ceil(nowYear - usePeople.year);

              return  age < 0 ? 0 : age;

          } else {
              return 0;
          }

        },

        // 星座
        constellation(state){
          if (state.user.birthday) {
              let constellation =  $utils.constellation(state.user.birthday);
              return  constellation ? constellation: state.user.constellation || ''
          } else {
              return state.user.constellation || '';
          }
        },

        // 照片墙
        photoWall(state){
          if (state.user.images) {
              return state.user.images.split(',') || [];
          } else {
              return [];
          }
        },

        integral(state){
             if (state.user.integral) {
                 return money.checkIntMoney(state.user.integral || 0) || 0;
             } else {
                 return 0;
             }
        },
        money(state){
             if (state.user.money) {
                 return money.checkIntMoney(state.user.money || 0) || 0;
             } else {
                 return 0;
             }
        }

    },

    mutations:{

        setUserOtherInfo(state,data){

            if (data && state[data.key]) {
                state[data.key] = data.value;
            }

        },

        initializationUser(state){
            let userData = storage.getItem(userConfig.storageKey);

            if (userData) {
                userConfig.user = userData;
                state.user = userData;
            }
        },

        // 设置缓存
        setUserInfo(state,user){

            let storageData = JSON.stringify(user);
            if (user && userConfig.stringify !== JSON.stringify(user)) {


                userConfig.stringify = storageData;

                userConfig.user = user;

                state.user = user;
                return storage.setItem(userConfig.storageKey,user,userConfig.storageTime);
            }

        },

        // 退出登录
        outLogin(state){

            userConfig.user = null;
            userConfig.stringify = null;
            state.user = {};

            socket.uninstall();

            return storage.removeItem(userConfig.storageKey);

        }

    },

    actions:{

        outLogin({ commit }){
            // 清除用户信息
            commit('outLogin');
            // 清除消息
            commit('clearSession');

        },

        initializationUserPromise({commit}){

            commit('initializationUser');

            if (userConfig.user) {
                return $request.getUserInfo().then((data:Record<string, any>)=>{
                   commit('setUserInfo',{
                      ...userConfig.user,
                      ...data
                   });
                })
            }

        },

        updateUserLevelPromise({commit}){
            if (userConfig.user) {

                return request({
                    url:'user/get_user_level_info',
                    token:true
                }).then((data:Record<string, any>)=>{

                    data =data.data || {};
                    let resultData = {};
                    for (let key in data){
                        if (data.hasOwnProperty(key)) {
                            let useName = key.split('_')[0];
                            resultData[useName+'_level'] = data[key].now_level;
                            resultData[useName+'_value'] = data[key].now_value;
                        }
                    }

                    commit('setUserInfo',{
                        ...userConfig.user,
                        other:resultData,
                        ...data
                    });
                })
            }
        },

        updateUserMoneyPromise({commit}){
            if (userConfig.user) {
                return request({
                    url:'user/get_user_money',
                    token:true
                }).then((data:Record<string, any>)=>{
                    commit('setUserInfo',{
                        ...userConfig.user,
                        ...data.data
                    });
                })
            }

        }

    }

}
