/*
 * @Author: sjl
 * @Date: 2023-05-15 20:12:17
 * @Descripttion:
 */

import { defineStore } from 'pinia';
import { store } from '/@/store';
import { message } from 'ant-design-vue';
import {
  dzdxInfo,
  gcInfo,
  GcbBaseInfoPage,
  GetDzRiskTypeList,
  GetDzRiskLevelList,
  MapConfig,
  TJKJList,
  GetDzdxInfoTxData,
  UpdateDzdxInfo,
  UpdateGcdxInfo,
  Visualangle,
  getVisualangle,
  videoprojection,
  getVideoprojection,
} from '/@/api/sys/geological';

interface Geological {
  sideMenu: boolean;
  gcList: any;
  dzList: any;
  dzTypeList: any;
}
export const useGeologicalStore = defineStore({
  id: 'geological',
  state: (): Geological => ({
    sideMenu: true,
    gcList: [],
    dzList: [],
    dzTypeList: [],
  }),
  getters: {
    getSideMenu(): boolean {
      return this.sideMenu;
    },

    getGcList(): any {
      return this.gcList;
    },

    getDzList(): any {
      return this.dzList;
    },

    getDzTypeList(): any {
      return this.dzTypeList;
    },
  },
  actions: {
    setSideMenu(): void {
      this.sideMenu = !this.sideMenu;
    },

    setGcList(list): void {
      this.gcList = list;
    },

    setDzList(list): void {
      this.dzList = list;
    },

    setDzTypeList(list): void {
      this.dzTypeList = list;
    },

    DzdxInfo(params): Promise<UserInfo | null> {
      try {
        // if (this.gcList.length) return this.gcList;
        // const data = await dzdxInfo(params);
        // if (data.code == 1) {
        //   this.setDzList(data.result);
        //   return data.result;
        // }
        if (this.gcList && this.gcList.length) {
          return new Promise((resolve, reject) => resolve(this.gcList));
        } else {
          return new Promise((resolve, reject) => {
            dzdxInfo(params).then((data) => {
              if (data.code == 1) {
                this.setDzList(data.result);
                resolve(data.result);
              }
            });
          });
        }
      } catch (error) {
        console.log(error);
        return Promise.reject(error);
      }
    },

    GcInfo(params): Promise<UserInfo | null> {
      try {
        // const data = await gcInfo(params);
        // if (data.code == 1) {
        //   this.setGcList(data.result);
        //   return data.result;
        // }
        return new Promise((resolve, reject) => {
          gcInfo(params).then((data) => {
            if (data.code == 1) {
              this.setGcList(data.result);
              resolve(data.result);
            }
          });
        });
      } catch (error) {
        console.log(error);
        return Promise.reject(error);
      }
    },

    GcbBaseInfoPage(params): Promise<UserInfo | null> {
      try {
        // const data = await GcbBaseInfoPage(params);
        // if (data.code == 1) {
        //   return data.result;
        // }
        return new Promise((resolve, reject) => {
          GcbBaseInfoPage(params).then((data) => {
            if (data.code == 1) {
              resolve(data.result);
            }
          });
        });
      } catch (error) {
        console.log(error);
        return Promise.reject(error);
      }
    },

    GetDzRiskTypeList(params): Promise<UserInfo | null> {
      try {
        // const data = await GetDzRiskTypeList(params);
        // if (data.code == 1) {
        //   this.setDzTypeList(data.result);
        //   return data.result;
        // }
        return new Promise((resolve, reject) => {
          GetDzRiskTypeList(params).then((data) => {
            if (data.code == 1) {
              this.setDzTypeList(data.result);
              resolve(data.result);
            }
          });
        });
      } catch (error) {
        console.log(error);
        return Promise.reject(error);
      }
    },

    GetDzRiskLevelList(params): Promise<UserInfo | null> {
      try {
        // const data = await GetDzRiskLevelList(params);
        // if (data.code == 1) {
        //   return data.result;
        // }
        return new Promise((resolve, reject) => {
          GetDzRiskLevelList(params).then((data) => {
            if (data.code == 1) {
              resolve(data.result);
            }
          });
        });
      } catch (error) {
        console.log(error);
        return Promise.reject(error);
      }
    },

    MapConfig(params): Promise<UserInfo | null> {
      try {
        return new Promise((resolve, reject) => {
          MapConfig(params).then((data) => {
            if (data.code == 1) {
              resolve(data.result);
            }
          });
        });
      } catch (error) {
        console.log(error);
        return Promise.reject(error);
      }
    },

    TJKJList(params): Promise<UserInfo | null> {
      try {
        return new Promise((resolve, reject) => {
          TJKJList(params).then((data) => {
            if (data.code == 1) resolve(data.result);
          });
        });
      } catch (error) {
        console.log(error);
        return Promise.reject(error);
      }
    },

    GetDzdxInfoTxData(params): Promise<UserInfo | null> {
      try {
        return new Promise((resolve, reject) => {
          GetDzdxInfoTxData(params).then((data) => {
            if (data.code == 1) {
              resolve(data.result);
            }
          });
        });
      } catch (error) {
        console.log(error);
        return Promise.reject(error);
      }
    },

    Visualangle(params): Promise<UserInfo | null> {
      try {
        return new Promise((resolve, reject) => {
          Visualangle(params).then((res) => {
            var type = res.code == 1 ? 'success' : 'error';
            var text = res.code == 1 ? '成功！' : '失败！';
            message[type](`当前点位视角保存${text}`);
          });
        });
      } catch (error) {
        console.log(error);
        return Promise.reject(error);
      }
    },

    Videoprojection(params): Promise<UserInfo | null> {
      try {
        return new Promise((resolve, reject) => {
          videoprojection(params).then((res) => {
            var type = res.code == 1 ? 'success' : 'error';
            var text = res.code == 1 ? '成功！' : '失败！';
            message[type](`当前点位视频投影保存${text}`);
            resolve(true);
          });
        });
      } catch (error) {
        console.log(error);
        return Promise.reject(error);
      }
    },

    getVisualangle(params): Promise<UserInfo | null> {
      try {
        return new Promise((resolve, reject) => {
          getVisualangle(params)
            .then((res) => {
              if (res.code == 1) {
                if (res.result.visualAngle) {
                  resolve(JSON.parse(res.result.visualAngle));
                } else {
                  resolve(false);
                }
              }
            })
            .catch(() => {
              resolve(false);
            });
        });
      } catch (error) {
        console.log(error);
        return Promise.reject(error);
      }
    },

    getVideoprojection(params): Promise<UserInfo | null> {
      try {
        return new Promise((resolve, reject) => {
          getVideoprojection(params)
            .then((res) => {
              if (res.code == 1) {
                if (res.result.projectionPoint) {
                  resolve(JSON.parse(res.result.projectionPoint));
                } else {
                  resolve(false);
                }
              }
            })
            .catch(() => {
              resolve(false);
            });
        });
      } catch (error) {
        console.log(error);
        return Promise.reject(error);
      }
    },

    async UpdateDzdxInfo(params): Promise<UserInfo | null> {
      try {
        const data = await UpdateDzdxInfo(params);
        if (data.code == 1) {
          return data.result;
        }
      } catch (error) {
        console.log(error);
        return Promise.reject(error);
      }
    },

    async UpdateGcdxInfo(params): Promise<UserInfo | null> {
      try {
        const data = await UpdateGcdxInfo(params);
        if (data.code == 1) {
          return data.result;
        }
      } catch (error) {
        console.log(error);
        return Promise.reject(error);
      }
    },
  },
});
// Need to be used outside the setup
export function useGeologicalStoreOut() {
  return useGeologicalStore(store);
}
