import { Injectable } from '@angular/core';
import { StorageService } from 'src/app/core/services/storage.service';
import { Projects } from '../models/project';
import { StoreInstanceService } from './store-instance.service';
const initialState = {
  projects: [] as Projects[],
  listViewTwo: [] as Projects,
  listViewThree: [] as Projects,
  setupProject: [],
  details: [],
  currentTab: [],
  isChangeRoute: false,
  domaineDD: {},
  new_project: {},
};

@Injectable({
  providedIn: 'root',
})
export class ProjectsStore extends StoreInstanceService<typeof initialState> {
  constructor(public storage: StorageService) {
    super(initialState);

    if (this.storage.getObject('znak'))
      this.setSetupProject(this.storage.getObject('project_setup'));
  }

  public setProjects(data: any) {
    this.update((state) => {
      return {
        ...state,
        projects: data,
        listViewTwo: data,
        listViewThree: data,
      };
    });
  }

  public setSetupProject(data: any) {
    this.update((state) => {
      return {
        ...state,
        setupProject: data,
      };
    });
  }

  public setDomaineDrivenAndModulename(data: any) {
    this.update((state) => {
      return {
        ...state,
        domaineDD: data
      };
    });
  }
  public setCurrentTab(data: any) {
    this.update((state) => {
      return {
        ...state,
        currentTab: data
      };
    });
  }

  public setDetails(data: any) {
    this.update((state) => {
      return {
        ...state,
        details: data,
      };
    });
  }

  public setNewProject(data: any) {
    this.update((state) => {
      return {
        ...state,
        new_project: data,
      };
    });
  }
  public resetNewProject() {
    this.update((state) => {
      return {
        ...state,
        new_project: {},
      };
    });
  }
  public resetStoreProject() {
    this.update((state) => {
      return {
        ...state,
        projects: [] as Projects[],
        listViewTwo: [] as Projects,
        listViewThree: [] as Projects,
        setupProject: [],
        isChangeRoute: false,
      };
    });
  }

  public addNewRecordData(data: any) {
    this.update((state: any) => {
      return {
        ...state,
        projects: [data].concat(state.projects),
      };
    });
  }

  public updateProjects(data: any, id: string) {
    this.update((state: any) => {
      return {
        ...state,
        projects: this.checkEdit(state.projects, data, id),
      };
    });
  }

  private checkEdit(project: any, data: any, id: string) {
    const response = project.map((res: any) => {
      if (res.projetsid === id) {
        res = data;
      }

      return res;
    });

    return response;
  }

  public removeProjects(data: any) {
    this.update((state: any) => {
      return {
        ...state,
        projects: state.projects.filter(
          (item: Projects) => !data.includes(item.projetsid)
        ),
      };
    });
  }

  public editMass(data: any) {
    this.update((state: any) => {
      return {
        ...state,
        projects: this.updateMultipleCheck(state.projects, data),
      };
    });
  }

  public updateMultipleCheck(old_version: any, new_version: any) {
    const real_new = old_version.map((el: any) => {
      var found = new_version.find(
        (s: any) => el['projetsid'] !== '' && s['projetsid'] === el['projetsid']
      );

      if (found) {
        el = Object.assign(el, found);
      }
      return el;
    });

    return real_new;
  }

  public updateIsChangeRoute(data: any) {
    this.update((state: any) => {
      return {
        ...state,
        isChangeRoute: data,
      };
    });
  }
}
