import { login, logout, getInfo } from "@/api/user";
import { getToken, setToken, removeToken } from "@/utils/auth";
import { resetRouter } from "@/router";
import { isLogOut, notLogOut } from "@/utils/waitLogOut";

const getDefaultState = () => {
  return {
    token: getToken(),
    isOperate: "",
    isShowPurchasePrice: "",
    isShowSellPrice: "",
    name: "",
    role: "",
    username: "",
    phone: "",
    provinceCode: "", //省份code
    gateAuth: "", //人员闸口通行数据
    portAuth: "", //人员进出港数据
    captureAuth: "", //人员摄像头抓拍数据
    vehicleAuth: "", //车辆卡口通行数据
    archivesAuth: "", //	档案查询
    behaviorArchivesAuth: "", //人员/车辆行为档案
    warningAuth: "", //	人员/车辆预警
    functionAuth: "", //单功能板块
    uavAuth: "", //无人机反制
    trafficLightsAuth: "", //红绿灯实时监控
    rainSnowAuth: "", //冰冻雨雪监控
    cockpitAuth: "", //驾驶舱大屏
  };
};

const state = getDefaultState();

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState());
  },
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  SET_NAME: (state, name) => {
    state.name = name;
  },
  SET_USERNAME: (state, username) => {
    state.username = username;
  },
  SET_PHONE: (state, phone) => {
    state.phone = phone;
  },
  SET_ISOPERATE: (state, isOperate) => {
    state.isOperate = isOperate;
  },
  SET_ROLE: (state, role) => {
    state.role = role;
  },
  SET_ISSHOWPURCHASEPRICE: (state, isShowPurchasePrice) => {
    state.isShowPurchasePrice = isShowPurchasePrice;
  },
  SET_ISSHOWSELLPRICE: (state, isShowSellPrice) => {
    state.isShowSellPrice = isShowSellPrice;
  },
  //
  SET_PROVINCECODE: (state, provinceCode) => {
    state.provinceCode = provinceCode;
  },
  SET_GATEAUTH: (state, gateAuth) => {
    state.gateAuth = gateAuth;
  },
  SET_PORTAUTH: (state, portAuth) => {
    state.portAuth = portAuth;
  },
  SET_CAPTUREAUTH: (state, captureAuth) => {
    state.captureAuth = captureAuth;
  },
  SET_VEHICLEAUTH: (state, vehicleAuth) => {
    state.vehicleAuth = vehicleAuth;
  },
  SET_ARCHIVESAUTH: (state, archivesAuth) => {
    state.archivesAuth = archivesAuth;
  },
  SET_BEHAVIORARCHIVESAUTH: (state, behaviorArchivesAuth) => {
    state.behaviorArchivesAuth = behaviorArchivesAuth;
  },
  SET_WARNINGAUTH: (state, warningAuth) => {
    state.warningAuth = warningAuth;
  },
  SET_FUNCTIONAUTH: (state, functionAuth) => {
    state.functionAuth = functionAuth;
  },
  SET_UAVAUTH: (state, uavAuth) => {
    state.uavAuth = uavAuth;
  },
  SET_TRAFFICLIGTSAUTH: (state, trafficLightsAuth) => {
    state.trafficLightsAuth = trafficLightsAuth;
  },
  SET_RAINSNOWAUTH: (state, rainSnowAuth) => {
    state.rainSnowAuth = rainSnowAuth;
  },
  SET_COCKPITAUTH: (state, cockpitAuth) => {
    state.cockpitAuth = cockpitAuth;
  },
};

const actions = {
  login({ commit }, userInfo) {
    const { username, password } = userInfo;
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password })
        .then((response) => {
          // console.log(response);
          // 标记登录,开始检测长时间未操作情况
          notLogOut();

          const { data } = response;
          commit("SET_ISOPERATE", data.isOperate);
          commit("SET_ISSHOWPURCHASEPRICE", data.isShowPurchasePrice);
          commit("SET_ISSHOWSELLPRICE", data.isShowSellPrice);
          commit("SET_NAME", data.name);
          commit("SET_PHONE", data.phone);
          commit("SET_USERNAME", data.username);
          commit("SET_ROLE", data.type);
          commit("SET_TOKEN", data.token);
          setToken(data.token);
          resolve();
        })
        .catch((error) => {
          console.log(error);
          reject(error);
        });
    });
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token)
        .then((response) => {
          const { data } = response;
          console.log(data);
          if (!data) {
            return reject("Verification failed, please Login again.");
          }

          commit("SET_ISOPERATE", data.isOperate);
          commit("SET_ISSHOWPURCHASEPRICE", data.isShowPurchasePrice);
          commit("SET_ISSHOWSELLPRICE", data.isShowSellPrice);
          commit("SET_NAME", data.name);
          commit("SET_PHONE", data.phone);
          commit("SET_USERNAME", data.username);
          commit("SET_ROLE", data.type);
          //
          // commit("SET_PROVINCECODE", data.adminAuthDto.provinceCode);
          // commit("SET_GATEAUTH", data.adminAuthDto.gateAuth);
          // commit("SET_PORTAUTH", data.adminAuthDto.portAuth);
          // commit("SET_CAPTUREAUTH", data.adminAuthDto.captureAuth);
          // commit("SET_VEHICLEAUTH", data.adminAuthDto.vehicleAuth);
          // commit("SET_ARCHIVESAUTH", data.adminAuthDto.archivesAuth);
          // commit(
          //   "SET_BEHAVIORARCHIVESAUTH",
          //   data.adminAuthDto.behaviorArchivesAuth
          // );
          // commit("SET_WARNINGAUTH", data.adminAuthDto.warningAuth);
          // commit("SET_FUNCTIONAUTH", data.adminAuthDto.functionAuth);
          // commit("SET_UAVAUTH", data.adminAuthDto.uavAuth);
          // commit("SET_TRAFFICLIGTSAUTH", data.adminAuthDto.trafficLightsAuth);
          // commit("SET_RAINSNOWAUTH", data.adminAuthDto.rainSnowAuth);
          // commit("SET_COCKPITAUTH", data.adminAuthDto.cockpitAuth);
          resolve(data);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      // 标记已经退出,不用再检测长时间未操作情况
      isLogOut();
      logout(state.token)
        .then(() => {
          removeToken(); // must remove  token  first
          resetRouter();
          commit("RESET_STATE");
          resolve();
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  // remove token
  resetToken({ commit }) {
    return new Promise((resolve) => {
      removeToken(); // must remove  token  first
      commit("RESET_STATE");
      resolve();
    });
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
