// Copyright 2021 99cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import React from 'react';
import { observer, inject } from 'mobx-react';
import ImageType from 'components/ImageType';
import Base from 'containers/List';
import {
  instanceStatus,
  transitionStatus,
  lockRender,
  instanceStatusFilter,
  isIronicInstance,
} from 'resources/instance';
import globalServerStore, { ServerStore } from 'stores/nova/instance';
import { ServerGroupInstanceStore } from 'stores/skyline/server-group-instance';
import actionConfigs from './actions';
export class Instance extends Base {
  init() {
    if (!this.inDetailPage) {
      this.store = globalServerStore;
    } else if (this.inServerGroupDetailPage) {
      this.store = new ServerGroupInstanceStore();
    } else {
      this.store = new ServerStore();
    }
    this.downloadStore = new ServerStore();
  }
  get tabs() {
    return [];
  }
  get policy() {
    if (this.isAdminPage) {
      return 'os_compute_api:servers:index:get_all_tenants';
    }
    return 'os_compute_api:servers:index';
  }
  get name() {
    return t('instances');
  }
  get inServerGroupDetailPage() {
    if (!this.inDetailPage) {
      return false;
    }
    const {
      match: { path },
    } = this.props;
    return path.indexOf('server-group') >= 0;
  }
  get inHostDetailPage() {
    if (!this.inDetailPage) {
      return false;
    }
    const {
      match: { path },
    } = this.props;
    return path.indexOf('hypervisors') >= 0;
  }
  get inFlavorDetailPage() {
    if (!this.inDetailPage) {
      return false;
    }
    const {
      match: { path },
    } = this.props;
    return path.indexOf('flavor') >= 0;
  }
  get isFilterByBackend() {
    return !this.inServerGroupDetailPage;
  }
  get isSortByBackend() {
    return this.isFilterByBackend;
  }
  get transitionStatusList() {
    return Object.keys(transitionStatus);
  }
  get adminPageHasProjectFilter() {
    return true;
  }
  get defaultSortKey() {
    return 'created_at';
  }
  get batchActions() {
    const { selectedRowKeys = [], data = [] } = this.store.list;
    const slectedRows = selectedRowKeys.map((key) => {
      return data.find((it) => it.id === key);
    });
    const allIronic = slectedRows.every((it) => isIronicInstance(it));
    const noIronic = slectedRows.every((it) => !isIronicInstance(it));
    if (allIronic) {
      return actionConfigs.batchActionsForIronic;
    }
    if (noIronic) {
      return actionConfigs.batchActions;
    }
    return actionConfigs.batchActionsForOthers;
  }
  getCheckboxProps(record) {
    return {
      name: record.name,
    };
  }
  getColumns() {
    const columns = [
      {
        title: t('ID/Name'),
        dataIndex: 'name',
        linkPrefix: `/compute/${this.getUrl('instance')}/detail`,
        sortKey: 'display_name',
      },
      {
        title: t('Project ID/Name'),
        dataIndex: 'project_name',
        isHideable: true,
        hidden: !this.isAdminPage,
        sortKey: 'project_id',
      },
      {
        title: t('Host'),
        dataIndex: 'host',
        isHideable: true,
        hidden: !this.isAdminPage,
      },
      {
        title: t('Image'),
        dataIndex: 'image_os_distro',
        isHideable: true,
        render: (value, record) => (