// 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 { inject, observer } from 'mobx-react';
import { ModalAction } from 'containers/Action';
import { FloatingIpStore } from 'stores/neutron/floatingIp';
import { NetworkStore } from 'stores/neutron/network';
import globalProjectStore from 'stores/keystone/project';
import globalSubnetStore from 'stores/neutron/subnet';
import { QoSPolicyStore } from 'stores/neutron/qos-policy';
import { getQoSPolicyTabs } from 'resources/qos-policy';

export class Allocate extends ModalAction {
  static id = 'allocate';

  static title = t('Allocate IP');

  get name() {
    return t('Allocate IP');
  }

  static get modalSize() {
    return 'large';
  }

  getModalSize() {
    return 'large';
  }

  init() {
    this.store = new FloatingIpStore();
    this.networkStore = new NetworkStore();
    this.qosPolicyStore = new QoSPolicyStore();
    this.getExternalNetworks();
    this.isAdminPage && globalProjectStore.fetchList();
    this.state = {
      selectedNetwork: null,
      selectedSubnet: null,
      networks: [],
      subnets: [],
      qosPolicy: null,
      count: 2,
    };
  }

  async getExternalNetworks() {
    const networks = await this.networkStore.pureFetchList({
      'router:external': true,
    });
    this.setState({
      networks,
    });
  }

  get messageHasItemName() {
    return false;
  }

  static policy = 'create_floatingip';

  static allowed = () => Promise.resolve(true);

  get defaultValue() {
    return {
      project_id: this.currentProjectId,
      count: 2,
    };
  }

  handleNetworkChange = async (networkId) => {
    const subnets = await globalSubnetStore.fetchList({
      network_id: networkId,
    });
    this.setState({
      subnets: subnets.map((item) => ({
        allocation_pools: item.allocation_pools,
        ip_version: item.ip_version,
        value: item.id,
        label: item.name,
      })),
      selectedNetwork: networkId,
    });
    this.formRef.current.setFieldsValue({
      subnet_id: null,
    });
  };

  handleSubnetChange = (option) => {
    this.setState({
      selectedSubnet: option,
    });
  };

  onSubmit = ({ subnet_id, batch_allocate, count, qos_policy_id, ...rest }) => {
    const data = rest;
    if (subnet_id) {
      data.subnet_id = subnet_id.value;
    }
    if (qos_policy_id && qos_policy_id.selectedRowKeys.length > 0) {
      data.qos_policy_id = qos_policy_id.selectedRowKeys[0];
    }
    if (batch_allocate) {
      data.floating_ip_address = null;
      const promises = [];
      for (let i = 0; i < count; i++) {
        promises.push(this.store.create(data));
      }
      return Promise.all(promises);
    }
    return this.store.create(data);
  };

  onCountChange = (value) => {
    this.setState({
      count: value,
    });
  };

  get formItems() {
    const {
      networks,
      selectedNetwork,
      subnets,
      selectedSubnet,
      batchAllocate = false,
    } = this.state;
    const networkItems = networks.map((item) => ({
      label: item.name,
      value: item.id,
    }));
    const projectOptions = globalProjectStore.list.data.map((project) => ({
      label: project.name,
      value: project.id,
    }));
    return [
      {
        name: 'floating_network_id',
        label: t('Network'),
        type: 'select',
        options: networkItems,
        onChange: this.handleNetworkChange,
        required: true,
      },
      {
        name: 'project_id',
        label: t('Project'),
        type: 'select',
        showSearch: true,
        hidden: !this.isAdminPage,
        required: this.isAdminPage,
        options: projectOptions,
      },
      {
        name: 'subnet_id',
        label: t('Owned Subnet'),
        type: 'select',
        options: subnets,
        isWrappedValue: true,
        onChange: (option) => this.handleSubnetChange(option),
        extra: selectedSubnet && (
          <>
            <span>{t('Allocation Pools')}</span>
            {selectedSubnet.allocation_pools.map((pool, index) => (
              <div key={`pool.start.${index}`}>
                {pool.start}--{pool.end}
              </div>
            ))}
          </>
        ),
        hidden: !selectedNetwork,
        required: false,
      },
      {
        name: 'batch_allocate',
        label: t('Batch Allocate'),
        type: 'check',
        onChange: (e) => {
          this.setState({
            batchAllocate: e,
          });
        },
      },
      {
        name: 'count',
        label: t('Count'),
        type: 'input-number',
        min: 2,
        hidden: !batchAllocate,
        required: true,
        onChange: this.onCountChange,
      },
      {
        name: 'floating_ip_address',
        label: t('Floating IP Address'),
        hidden: !selectedSubnet || batchAllocate,
        type: 'ip-input',
        version: selectedSubnet && (selectedSubnet.ip_version || 4),
      },
      {
        name: 'description',
        label: t('Description'),
        type: 'textarea',
      },
      {
        name: 'qos_policy_id',
        label: t('QoS Policy'),
        type: 'tab-select-table',
        tabs: getQoSPolicyTabs.call(this),
        isMulti: false,
        tip: t('Choosing a QoS policy can limit bandwidth and DSCP'),
        onChange: this.onQosChange,
      },
    ];
  }
}

export default inject('rootStore')(observer(Allocate));