import "choerodon-ui/lib/table/style";
import _Table from "@hzero-front-ui/c7n-ui/lib/Table";
import "choerodon-ui/lib/button/style";
import _Button from "choerodon-ui/lib/button";
import "choerodon-ui/lib/tooltip/style";
import _Tooltip from "choerodon-ui/lib/tooltip";
import React, { Component } from 'react';
import { observer } from 'mobx-react-lite';
import { injectIntl, FormattedMessage } from 'react-intl';
import { withRouter } from 'react-router-dom';
import { Permission, stores } from '@choerodon/boot';
import MouserOverWrapper from "../../../../components/MouseOverWrapper";
import "../../../code-manager/contents/ciPipelineManage/index.less";
import "./BuildTable.less";
import TimePopover from "../../../../components/timePopover/TimePopover";
import UserInfo from "../../../../components/userInfo";
import { useReportsStore } from "../../stores";
var ICONS = {
  passed: {
    icon: 'icon-check_circle',
    code: 'passed',
    display: 'Passed'
  },
  success: {
    icon: 'icon-check_circle',
    code: 'success',
    display: 'Passed'
  },
  pending: {
    icon: 'icon-pause_circle_outline',
    code: 'pending',
    display: 'Pending'
  },
  running: {
    icon: 'icon-timelapse',
    code: 'running',
    display: 'Running'
  },
  failed: {
    icon: 'icon-cancel',
    code: 'failed',
    display: 'Failed'
  },
  canceled: {
    icon: 'icon-cancle_b',
    code: 'canceled',
    display: 'Cancel'
  },
  skipped: {
    icon: 'icon-skipped_b',
    code: 'skipped',
    display: 'Skipped'
  },
  created: {
    icon: 'icon-radio_button_checked',
    code: 'created',
    display: 'Created'
  },
  manual: {
    icon: 'icon-radio_button_checked',
    code: 'manual',
    display: 'Manual'
  }
};
var ICONS_ACTION = {
  pending: {
    icon: 'icon-not_interested'
  },
  running: {
    icon: 'icon-not_interested'
  },
  failed: {
    icon: 'icon-refresh'
  },
  canceled: {
    icon: 'icon-refresh'
  },
  skipped: {
    icon: 'icon-refresh'
  }
};
var BuildTable = withRouter(observer(function (props) {
  var _useReportsStore = useReportsStore(),
      formatMessage = _useReportsStore.intl.formatMessage,
      AppState = _useReportsStore.AppState,
      ReportsStore = _useReportsStore.ReportsStore;

  function getColumns() {
    return [{
      title: /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "ciPipeline.status"
      }),
      dataIndex: 'status',
      render: function render(status, record) {
        return renderStatus(status, record);
      }
    }, {
      title: /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "network.column.version"
      }),
      dataIndex: 'version',
      render: function render(version) {
        return renderVersion(version);
      }
    }, {
      title: /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "ciPipeline.commit"
      }),
      dataIndex: 'commit',
      render: function render(commit, record) {
        return renderCommit(commit, record);
      }
    }, {
      title: /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "ciPipeline.jobs"
      }),
      dataIndex: 'stages',
      render: function render(stages, record) {
        return renderstages(stages, record);
      }
    }, {
      title: /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "ciPipeline.time"
      }),
      dataIndex: 'pipelineTime',
      render: function render(pipelineTime, record) {
        return /*#__PURE__*/React.createElement("span", null, renderTime(pipelineTime, record));
      }
    }, {
      title: /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "ciPipeline.createdAt"
      }),
      dataIndex: 'creationDate',
      render: function render(creationDate, record) {
        return /*#__PURE__*/React.createElement(TimePopover, {
          content: creationDate
        });
      }
    }, {
      width: 56,
      key: 'action',
      render: function render(record) {
        return renderAction(record);
      }
    }];
  }

  var renderStatus = function renderStatus(status, record) {
    var _ICONS$status;

    return /*#__PURE__*/React.createElement("div", {
      className: "c7n-status"
    }, /*#__PURE__*/React.createElement("a", {
      href: record.gitlabUrl ? "".concat(record.gitlabUrl.slice(0, -4), "/pipelines/").concat(record.pipelineId) : null,
      target: "_blank",
      rel: "nofollow me noopener noreferrer",
      className: "c7n-status-link"
    }, /*#__PURE__*/React.createElement("i", {
      className: "icon ".concat((_ICONS$status = ICONS[status]) === null || _ICONS$status === void 0 ? void 0 : _ICONS$status.icon, " c7n-icon-").concat(status, " c7n-icon-lg")
    }), /*#__PURE__*/React.createElement("span", {
      className: "c7n-text-status black"
    }, ICONS[status].display)));
  };

  var renderVersion = function renderVersion(version) {
    if (version) {
      return /*#__PURE__*/React.createElement("div", null, version);
    }

    return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(FormattedMessage, {
      id: "report.build-duration.noversion"
    }));
  };

  var renderCommit = function renderCommit(commit, record) {
    return /*#__PURE__*/React.createElement("div", {
      className: "c7n-commit"
    }, /*#__PURE__*/React.createElement("div", {
      className: "c7n-title-commit"
    }, /*#__PURE__*/React.createElement("i", {
      className: "icon icon-branch mr7"
    }), /*#__PURE__*/React.createElement(MouserOverWrapper, {
      text: record.ref,
      width: 0.1
    }, /*#__PURE__*/React.createElement("a", {
      className: "c7n-link-decoration",
      href: record.gitlabUrl ? "".concat(record.gitlabUrl.slice(0, -4), "/commits/").concat(record.ref) : null,
      target: "_blank",
      rel: "nofollow me noopener noreferrer"
    }, /*#__PURE__*/React.createElement("span", {
      className: "black"
    }, record.ref))), /*#__PURE__*/React.createElement("i", {
      className: "icon icon-point m8"
    }), /*#__PURE__*/React.createElement(_Tooltip, {
      placement: "top",
      title: record.commit,
      trigger: "hover"
    }, /*#__PURE__*/React.createElement("a", {
      className: "c7n-link-decoration",
      href: "".concat(record.gitlabUrl.slice(0, -4), "/commit/").concat(record.commit),
      target: "_blank",
      rel: "nofollow me noopener noreferrer"
    }, /*#__PURE__*/React.createElement("span", null, record.commit ? record.commit.slice(0, 8) : '')))), /*#__PURE__*/React.createElement("div", {
      className: "c7n-des-commit",
      style: {
        marginTop: '0'
      }
    }, /*#__PURE__*/React.createElement(UserInfo, {
      name: record.commitUserName || '?',
      avatar: record.commitUserUrl,
      id: record.commitUserLoginName,
      showName: false
    }), /*#__PURE__*/React.createElement(MouserOverWrapper, {
      text: record.commitContent,
      width: 0.2
    }, /*#__PURE__*/React.createElement("a", {
      className: "c7n-link-decoration c7n-link-ml5",
      href: "".concat(record.gitlabUrl.slice(0, -4), "/commit/").concat(record.commit),
      target: "_blank",
      rel: "nofollow me noopener noreferrer"
    }, /*#__PURE__*/React.createElement("span", {
      className: "gray"
    }, record.commitContent)))));
  };

  var renderstages = function renderstages(stages, record) {
    var pipeStage = [];

    if (stages && stages.length) {
      for (var i = 0, l = stages.length; i < l; i += 1) {
        pipeStage.push( /*#__PURE__*/React.createElement("span", {
          className: "c7n-jobs",
          key: i
        }, i !== 0 ? /*#__PURE__*/React.createElement("span", {
          className: "c7n-split-before"
        }) : null, /*#__PURE__*/React.createElement(_Tooltip, {
          title: stages[i].name === 'sonarqube' && stages[i].status === 'failed' ? "".concat(stages[i].name, " : ").concat(stages[i].description) : "".concat(stages[i].name, " : ").concat(stages[i].status)
        }, stages[i].name === 'sonarqube' ? /*#__PURE__*/React.createElement("i", {
          className: "icon ".concat(ICONS[stages[i].status || 'skipped'].icon || '', "\n                c7n-icon-").concat(stages[i].status, " c7n-icon-lg")
        }) : /*#__PURE__*/React.createElement("a", {
          className: "",
          href: "".concat(record.gitlabUrl.slice(0, -4), "/-/jobs/").concat(stages[i].id),
          target: "_blank",
          rel: "nofollow me noopener noreferrer"
        }, /*#__PURE__*/React.createElement("i", {
          className: "icon ".concat(ICONS[stages[i].status || 'skipped'].icon || '', "\n                c7n-icon-").concat(stages[i].status, " c7n-icon-lg")
        })))));
      }
    }

    return /*#__PURE__*/React.createElement("div", {
      className: "c7n-jobs"
    }, pipeStage);
  };

  var renderTime = function renderTime(pipelineTime, record) {
    if (pipelineTime) {
      if (pipelineTime.split('.')[1] === '00') {
        pipelineTime = "".concat(pipelineTime.toString().split('.')[0]).concat(formatMessage({
          id: 'minutes'
        }));
      } else if (pipelineTime.split('.')[0] === '0') {
        pipelineTime = "".concat((Number(pipelineTime.toString().split('.')[1]) * 0.6).toFixed()).concat(formatMessage({
          id: 'seconds'
        }));
      } else if (pipelineTime.split('.').length === 2) {
        pipelineTime = "".concat(pipelineTime.toString().split('.')[0]).concat(formatMessage({
          id: 'minutes'
        })).concat((Number(pipelineTime.toString().split('.')[1]) * 0.6).toFixed()).concat(formatMessage({
          id: 'seconds'
        }));
      } else {
        pipelineTime = null;
      }

      return pipelineTime;
    } else {
      return '--';
    }
  };

  var renderAction = function renderAction(record) {
    var projectId = AppState.currentMenuType.id;
    var organizationId = AppState.currentMenuType.organizationId;
    var type = AppState.currentMenuType.type;

    if (record.status && record.status !== 'passed' && record.status !== 'success' && record.status !== 'skipped') {
      return /*#__PURE__*/React.createElement(Permission, {
        service: ['devops-service.project-pipeline.retry', 'devops-service.project-pipeline.cancel'],
        organizationId: organizationId,
        projectId: projectId,
        type: type
      }, /*#__PURE__*/React.createElement(_Tooltip, {
        placement: "top",
        title: record.status === 'running' || record.status === 'pending' ? 'cancel' : 'retry'
      }, /*#__PURE__*/React.createElement(_Button, {
        size: "small",
        shape: "circle",
        onClick: function onClick() {
          return handleAction(record);
        }
      }, /*#__PURE__*/React.createElement("span", {
        className: "icon ".concat(ICONS_ACTION[record.status] ? ICONS_ACTION[record.status].icon : '', " c7n-icon-action c7n-icon-sm")
      }))));
    } else {
      return null;
    }
  };

  function handleAction(record) {
    if (record.status === 'running' || record.status === 'pending') {
      ReportsStore.cancelPipeline(record.gitlabProjectId, record.pipelineId);
    } else {
      ReportsStore.retryPipeline(record.gitlabProjectId, record.pipelineId);
    }

    tableChange(ReportsStore.pageInfo);
  }
  /**
   * 表格改变函数
   * @param pagination 分页
   */


  var tableChange = function tableChange(pagination) {
    var projectId = AppState.currentMenuType.id;
    var appId = ReportsStore.getAppId;
    var startTime = ReportsStore.getStartTime.format('YYYY-MM-DD HH:mm:ss');
    var endTime = ReportsStore.getEndTime.format('YYYY-MM-DD HH:mm:ss');
    ReportsStore.loadBuildTable(projectId, appId, startTime, endTime, pagination.current, pagination.pageSize);
  };

  var loading = ReportsStore.loading,
      pageInfo = ReportsStore.pageInfo,
      allData = ReportsStore.allData;
  return /*#__PURE__*/React.createElement(_Table, {
    onChange: tableChange,
    loading: loading,
    columns: getColumns(),
    className: "c7n-buildTable-table",
    dataSource: allData,
    pagination: pageInfo,
    filterBar: false,
    rowKey: function rowKey(record) {
      return record.pipelineId;
    }
  });
}));
export default BuildTable;