import * as logSymbols from 'log-symbols';
import { TargetProtocol, TargetHardware } from '../../core/app';
import { echo } from '../../util';
import { fetchProjectModels } from '../project';
import { checkUserIsLoggedInComponent } from '../user';
import { appInitComponent } from './app-init-component';
import { appModelsAddComponent } from './models';
import { targetJsonComponent } from './target';

export async function appConfigureComponent(props: {
  yes: boolean;
  targetProtocol?: TargetProtocol;
  targetHardware?: TargetHardware;
  targetHostname?: string;
  targetPath?: string;
  project?: string;
  deviceId?: string;
  syncModels?: boolean;
}) {
  const {
    yes,
    targetProtocol,
    targetHardware,
    targetHostname,
    targetPath,
    project,
    deviceId,
    syncModels = false
  } = props;

  await checkUserIsLoggedInComponent({ yes });
  await appInitComponent({ yes, project });
  if (syncModels) {
    const projectModels = await fetchProjectModels();
    for (const model of projectModels) {
      await appModelsAddComponent({
        yes,
        id: model.id,
        addToProject: false
      });
    }
    echo(`${logSymbols.success} Add models from project`);
  }

  await targetJsonComponent({
    yes,
    targetProtocol,
    targetHardware,
    targetHostname,
    targetPath,
    deviceId
  });
}
