import { Component, Vue } from 'vue-property-decorator';
import FormGeneratorWrapper from '@components/common/node-generator/FormGeneratorWrapper.vue';
import { FormApi } from '@openapi/registry-service';

@Component({
  components: {
    FormGeneratorWrapper,
  },
})
export default class Mu extends Vue {
  service: FormApi = new FormApi();
  editMode = true;
  loading = true;
  comps: string[] = [];
  ready = false;
  errorIdsArr: string[] = [];
  isButtonDisable = false;
  done = false;
  totalItems = 0; // change
  progress = 0;

  mounted() {
    this.loading = false;
  }

  async start() {
    const forms = await this.service.searchForm();
    this.totalItems = forms.data.length;
    const arrId = this.getArrId(forms.data);
    this.getForm(arrId);
    this.isButtonDisable = true;
  }

  getArrId(forms: any) {
    const arr: any = [];
    forms.forEach((item: any) => {
      arr.push(item.id);
    });
    return arr;
  }

  async getForm(arrIds: string[]) {
    let iter = 0;
    const errorArr = [];
    for (const id of arrIds) {
      try {
        const form = await this.service.getForm(id);
        let newForm = this.searchPropDictionary(form.data);
        newForm = this.searchPropDictionaryNew(newForm);
        await this.service.updateForm([newForm]);
      } catch (e) {
        console.log(e, id);
        errorArr.push(id);
      } finally {
        iter += 1;
        this.progress = iter / this.totalItems;
      }
    }
    this.done = true;
    this.errorIdsArr = errorArr;
    console.log('ERRORIDS', errorArr);
  }

  searchPropDictionary(form: any) {
    const linkedAttrList = Object.keys(form.linkedSchema?.linked || {}) || [];
    if (form.attrMap) {
      const formArr = Object.keys(form.attrMap);
      formArr?.forEach((item: any) => {
        if (form.attrMap[item].dictionary) {
          if (!form.attrMap[item].props) {
            form.attrMap[item].props = {
              dictionary: form.attrMap[item].dictionary.dictionary,
            };
          } else {
            form.attrMap[item].props.dictionary =
              form.attrMap[item].dictionary.dictionary;
          }
          form.attrMap[item].dictionary = '';
        }
      });
    }
    if (form.groupList?.length) {
      form.groupList?.forEach((item: any) => {
        if (item.dictionary) {
          if (!item.props) {
            item.props = {
              dictionary: item.dictionary.dictionary,
            };
          } else {
            item.props.dictionary = item.dictionary.dictionary;
          }
          delete item.dictionary;
        }
      });
    }
    if (form.attrList?.length) {
      form.attrList?.forEach((item: any) => {
        if (item.dictionary) {
          if (!item.props) {
            item.props = {
              dictionary: item.dictionary.dictionary,
            };
          } else {
            item.props.dictionary = item.dictionary.dictionary;
          }
          delete item.dictionary;
        }
        if (item.reference?.listAttrs) {
          item.reference.listAttrs.forEach((atr: any) => {
            if (atr.dictionary) {
              if (!atr.props) {
                atr.props = {
                  dictionary: atr.dictionary.dictionary,
                };
              } else {
                atr.props.dictionary = atr.dictionary.dictionary;
              }
              delete atr.dictionary;
            }
          });
        }
      });
    }
    if (linkedAttrList.length) {
      linkedAttrList.forEach((item: any) => {
        const linkedNew =
          Object.keys(
            form.linkedSchema?.linked[item]?.linkedSchema?.linked || {}
          ) || [];
        if (linkedNew.length) {
          linkedNew.forEach((link: any) => {
            form.linkedSchema?.linked[item]?.linkedSchema?.linked[
              link
            ].attrList?.forEach((e: any) => {
              if (e.dictionary) {
                if (!e.props) {
                  e.props = {
                    dictionary: e.dictionary.dictionary,
                  };
                } else {
                  e.props.dictionary = e.dictionary.dictionary;
                }
                delete e.dictionary;
              }
            });
          });
        }
        form.linkedSchema?.linked[item].attrList?.forEach((atr: any) => {
          if (atr.dictionary) {
            if (!atr.props) {
              atr.props = {
                dictionary: atr.dictionary.dictionary,
              };
            } else {
              atr.props.dictionary = atr.dictionary.dictionary;
            }
            delete atr.dictionary;
          }
          if (atr.reference?.listAttrs) {
            atr.reference.listAttrs.forEach((ref: any) => {
              if (ref.dictionary) {
                if (!ref.props) {
                  ref.props = {
                    dictionary: ref.dictionary.dictionary,
                  };
                } else {
                  ref.props.dictionary = ref.dictionary.dictionary;
                }
                delete ref.dictionary;
              }
            });
          }
        });
        form.linkedSchema?.linked[item].groupList?.forEach((atr: any) => {
          if (atr.dictionary) {
            if (!atr.props) {
              atr.props = {
                dictionary: atr.dictionary.dictionary,
              };
            } else {
              atr.props.dictionary = atr.dictionary.dictionary;
            }
            delete atr.dictionary;
          }
        });
        if (form.linkedSchema?.linked[item].attrMap) {
          const formArr = Object.keys(form.linkedSchema?.linked[item].attrMap);
          formArr?.forEach((el: any) => {
            if (form.linkedSchema.linked[item].attrMap[el].dictionary) {
              if (!form.linkedSchema.linked[item].attrMap[el].props) {
                form.linkedSchema.linked[item].attrMap[el].props = {
                  dictionary:
                    form.linkedSchema.linked[item].attrMap[el].dictionary
                      .dictionary,
                };
              } else {
                form.linkedSchema.linked[item].attrMap[el].props.dictionary =
                  form.linkedSchema.linked[item].attrMap[
                    el
                  ].dictionary.dictionary;
              }

              form.linkedSchema.linked[item].attrMap[el].dictionary = '';
            }
          });
        }
      });
    }
    return form;
  }

  searchPropDictionaryNew(form: any) {
    const linkedAttrList = Object.keys(form.linkedSchema?.linked || {}) || [];
    const dictionaryProp = 'dictionary';
    if (form.attrMap) {
      const formArr = Object.keys(form.attrMap);
      formArr?.forEach((item: any) => {
        if (
          form.attrMap[item].props?.dictionary &&
          form.attrMap[item].props?.dictionary.hasOwnProperty(dictionaryProp)
        ) {
          form.attrMap[item].props.dictionary =
            form.attrMap[item].props.dictionary.dictionary;
        }
      });
    }
    if (form.groupList?.length) {
      form.groupList?.forEach((item: any) => {
        if (
          item.props?.dictionary &&
          item.props?.dictionary.hasOwnProperty(dictionaryProp)
        ) {
          item.props.dictionary = item.props.dictionary.dictionary;
        }
      });
    }
    if (form.attrList?.length) {
      form.attrList?.forEach((item: any) => {
        if (
          item.props?.dictionary &&
          item.props?.dictionary.hasOwnProperty(dictionaryProp)
        ) {
          item.props.dictionary = item.props.dictionary.dictionary;
        }
        if (item.reference?.listAttrs) {
          item.reference.listAttrs.forEach((atr: any) => {
            if (
              atr.props?.dictionary &&
              atr.props?.dictionary.hasOwnProperty(dictionaryProp)
            ) {
              atr.props.dictionary = atr.props.dictionary.dictionary;
            }
          });
        }
      });
    }
    if (linkedAttrList.length) {
      linkedAttrList.forEach((item: any) => {
        const linkedNew =
          Object.keys(
            form.linkedSchema?.linked[item]?.linkedSchema?.linked || {}
          ) || [];
        if (linkedNew.length) {
          linkedNew.forEach((link: any) => {
            form.linkedSchema?.linked[item]?.linkedSchema?.linked[
              link
            ].attrList?.forEach((e: any) => {
              if (
                e.props?.dictionary &&
                e.props?.dictionary.hasOwnProperty(dictionaryProp)
              ) {
                e.props.dictionary = e.props.dictionary.dictionary;
              }
            });
          });
        }
        form.linkedSchema?.linked[item].attrList?.forEach((atr: any) => {
          if (
            atr.props?.dictionary &&
            atr.props?.dictionary.hasOwnProperty(dictionaryProp)
          ) {
            atr.props.dictionary = atr.props.dictionary.dictionary;
          }
          if (atr.reference?.listAttrs) {
            atr.reference.listAttrs.forEach((ref: any) => {
              if (
                ref.props?.dictionary &&
                ref.props?.dictionary.hasOwnProperty(dictionaryProp)
              ) {
                ref.props.dictionary = ref.props.dictionary.dictionary;
              }
            });
          }
        });
        form.linkedSchema?.linked[item].groupList?.forEach((atr: any) => {
          if (
            atr.props?.dictionary &&
            atr.props?.dictionary.hasOwnProperty(dictionaryProp)
          ) {
            atr.props.dictionary = atr.props.dictionary.dictionary;
          }
        });
        if (form.linkedSchema?.linked[item].attrMap) {
          const formArr = Object.keys(form.linkedSchema?.linked[item].attrMap);
          formArr?.forEach((el: any) => {
            if (
              form.linkedSchema.linked[item].attrMap[el].props?.dictionary &&
              form.linkedSchema.linked[item].attrMap[
                el
              ].props?.dictionary.hasOwnProperty(dictionaryProp)
            ) {
              form.linkedSchema.linked[item].attrMap[el].props.dictionary =
                form.linkedSchema.linked[item].attrMap[
                  el
                ].props.dictionary.dictionary;
            }
          });
        }
      });
    }
    return form;
  }

  // searchProp(form: any) {
  //   const linkedAttrList = Object.keys(form.linkedSchema?.linked || {}) || [];
  //   if (form.attrMap) {
  //     const formArr = Object.keys(form.attrMap);
  //     formArr?.forEach((item: any) => {
  //       form.attrMap[item].previewReadOnly = '';
  //     });
  //   }
  //   if (form.groupList?.length) {
  //     form.groupList?.forEach((item: any) => {
  //       delete item.previewReadOnly;
  //     });
  //   }
  //   if (form.attrList?.length) {
  //     form.attrList?.forEach((item: any) => {
  //       delete item.previewReadOnly;
  //       if (item.reference?.listAttrs) {
  //         item.reference.listAttrs.forEach((atr: any) => {
  //           delete atr.previewReadOnly;
  //         });
  //       }
  //     });
  //   }
  //   if (linkedAttrList.length) {
  //     linkedAttrList.forEach((item: any) => {
  //       const linkedNew =
  //         Object.keys(
  //           form.linkedSchema?.linked[item]?.linkedSchema?.linked || {}
  //         ) || [];
  //       if (linkedNew.length) {
  //         linkedNew.forEach((link: any) => {
  //           form.linkedSchema?.linked[item]?.linkedSchema?.linked[
  //             link
  //           ].attrList?.forEach((e: any) => {
  //             delete e.previewReadOnly;
  //           });
  //         });
  //       }
  //       form.linkedSchema?.linked[item].attrList?.forEach((atr: any) => {
  //         delete atr.previewReadOnly;
  //         if (atr.reference?.listAttrs) {
  //           atr.reference.listAttrs.forEach((ref: any) => {
  //             delete ref.previewReadOnly;
  //           });
  //         }
  //       });
  //       form.linkedSchema?.linked[item].groupList?.forEach((atr: any) => {
  //         delete atr.previewReadOnly;
  //       });
  //       if (form.linkedSchema?.linked[item].attrMap) {
  //         const formArr = Object.keys(form.linkedSchema?.linked[item].attrMap);
  //         formArr?.forEach((el: any) => {
  //           form.linkedSchema.linked[item].attrMap[el].previewReadOnly = '';
  //         });
  //       }
  //     });
  //   }
  //   console.log('Form', form);
  //   return form;
  // }
}
