import { Request, Response } from 'express';
import dayjs from 'dayjs';

import { AdminAPI } from '../src/constants/api';
import { DeclarationType } from '../src/constants/product';

const genJobList = (current: number, pageSize: number) => {
  const tableListDataSource: API.JobListItem[] = [];
  for (let i = 0; i < pageSize; i += 1) {
    const index = (current - 1) * 10 + i;
    tableListDataSource.push({
      id: `${index}-id`,
      jobId: `${index}-id`,
      title: '招聘主播',
      jobDescription: '招聘主播',
      sourceText: '招聘主播',
      cityCode: index % 2 === 0 ? '110100' : '430100',
      category: 'ALL',
      employType: 'ALL',
      publisher: '甲子',
      blPublisherId: `${index}-blPublisherId`,
      publisherAcctNo: `${index}-publisherAcctNo`,
      blGroupId: `${index}-blGroupId`,
      imGroupId: `${index}-imGroupId`,
      imGroupNick: '杭州主播群3',
      robotId: `${index}-robotId`,
      robotImNo: `${index}-robotImNo`,
      robotImNick: `${index}-robotImNick`,
      relateGroupCount: 100,
      created: Date.now().toString(),
      updated: Date.now().toString(),
      disable: index % 2 === 0,
    });
  }
  tableListDataSource.reverse();
  return tableListDataSource;
};

const genGroupList = (current: number, pageSize: number) => {
  const tableListDataSource: API.GroupListItem[] = [];
  for (let i = 0; i < pageSize; i += 1) {
    const index = (current - 1) * 10 + i;
    tableListDataSource.push({
      id: `${index}-id`,
      imGroupId: `${index}-groupId`,
      imGroupNick: '杭州主播群3',
      groupType: '',
      groupOwnerNick: '甲子',
      groupOwnerAcctNo: `${index}-boluoId`,
      groupOwnerImAcctNo: `${index}-wxId`,
      robotId: `${index}-robotId`,
      robotImNo: `${index}-robotImNo`,
      robotImNick: `${index}-robotImNick`,
      city: index % 2 === 0 ? '110100' : '430100',
      jobCount: 10,
      // created: dayjs().format('YYYY-MM-DD HH:mm:ss'),
      created: Date.now().toString(),
      updated: Date.now().toString(),
      disable: index % 2 === 0,
      isDeleted: index % 2 === 0,
    });
  }
  tableListDataSource.reverse();
  return tableListDataSource;
};

const genAnchorGroupList = (current: number, pageSize: number) => {
  const tableListDataSource: API.AnchorGroupListItem[] = [];
  for (let i = 0; i < pageSize; i += 1) {
    const index = (current - 1) * 10 + i;
    tableListDataSource.push({
      userId: `${index}-id`,
      nickName: '甲子',
      userPhone: '18222020020',
      blGroupId: `${index}-blGroupId`,
      imGroupId: `${index}-imGroupId`,
      imGroupNick: '杭州主播群3',
      creator: '甲子',
      payed: index % 2 === 0,
      payType: 'wx',
      created: Date.now().toString(),
      status: 0,
    });
  }
  tableListDataSource.reverse();
  return tableListDataSource;
};

const genPublisherList = (current: number, pageSize: number) => {
  const tableListDataSource: API.PublisherListItem[] = [];
  for (let i = 0; i < pageSize; i += 1) {
    const index = (current - 1) * 10 + i;
    tableListDataSource.push({
      publisher: '甲子',
      blPublisherId: `${index}-blPublisherId`,
      publisherAcctNo: `${index}-publisherAcctNo`,
      publisherAcctStatus: index % 2 === 0 ? 0 : 1,
      status: index % 2 === 0 ? 0 : 1,
      addAcctStatus: index % 2 === 0 ? 0 : index % 3 === 0 ? 1 : index % 4 === 0 ? 2 : 3,
      robotImNick: `${index}-robotImNick`,
      phone: `${index}-phone`,
      email: `${index}-email`,
      operator: '宵夜',
      imGroupId: `${index}-imGroupId`,
      blGroupId: `${index}-blGroupId`,
      imGroupNick: `${index}-imGroupNick`,
      created: Date.now().toString(),
      updated: Date.now().toString(),
    });
  }
  tableListDataSource.reverse();
  return tableListDataSource;
};

const genAnchorList = (current: number, pageSize: number) => {
  const tableListDataSource: API.AnchorListItem[] = [];
  for (let i = 0; i < pageSize; i += 1) {
    const index = (current - 1) * 10 + i;
    tableListDataSource.push({
      userId: `${index}-id`,
      nickName: '甲子',
      userPhone: index % 2 === 0 ? '18222020020' : '',
      isBindPhone: index % 2 === 0,
      created: Date.now().toString(),
      lastLoginDate: Date.now().toString(),
      status: index % 2 === 0 ? 0 : 1,
      city: '400100',
    });
  }
  tableListDataSource.reverse();
  return tableListDataSource;
};

const genDeclarationList = (current: number, pageSize: number) => {
  const tableListDataSource: API.DeclarationListItem[] = [];
  for (let i = 0; i < pageSize; i += 1) {
    const index = (current - 1) * 10 + i;
    tableListDataSource.push({
      id: `${index}-id`,
      type: index % 2 === 0 ? DeclarationType.CS : DeclarationType.Direct,
      userId: `${index}-userId`,
      nickName: '甲子',
      userPhone: '18222020020',
      jobId: `${index}-id`,
      title: '招聘主播',
      useDate: Date.now().toString(),
      publisher: '甲子',
      blPublisherId: `${index}-blPublisherId`,
      publisherAcctNo: `${index}-publisherAcctNo`,
      declaredStatus: index % 2 === 0 ? 0 : 1,
      declarationDate: Date.now().toString(),
      declaredDate: Date.now().toString(),
      weComStatus: index % 2 === 0 ? 0 : 1,
      jobCityCode: '400100',
    });
  }
  tableListDataSource.reverse();
  return tableListDataSource;
};

let jobListDataSource = genJobList(1, 100);
let groupListDataSource = genGroupList(1, 100);
let anchorGroupListDataSource = genAnchorGroupList(1, 100);
let publisherListDataSource = genPublisherList(1, 100);
let anchorListDataSource = genAnchorList(1, 100);
let declarationListDataSource = genDeclarationList(1, 100);

function getGroupList(req: Request, res: Response, u: string, b: Request) {
  const body = (b && b.body) || req.body;
  const { page = 1, pageSize = 10 } = body;

  const dataSource = [...groupListDataSource].slice(
    ((page as number) - 1) * (pageSize as number),
    (page as number) * (pageSize as number),
  );

  const result = {
    data: dataSource,
    total: groupListDataSource.length,
    pageSize,
    page: parseInt(`${page}`, 10) || 1,
  };

  // res.status(417).send({
  //   data: {
  //     code: 'NEED_LOGIN',
  //   },
  //   errorCode: '401',
  //   errorMessage: '请先登录！',
  // });
  // return;

  return res.json(result);
}

function updateGroup(req: Request, res: Response, u: string, b: Request) {
  const body = (b && b.body) || req.body;
  const { id } = body;
  let newRule = {};
  groupListDataSource = groupListDataSource.map(item => {
    if (item.id === id) {
      newRule = { ...item, ...body };
      return { ...item, ...body };
    }
    return item;
  });
  res.json(newRule);
}

function getJobList(req: Request, res: Response, u: string, b: Request) {
  const body = (b && b.body) || req.body;
  const { page = 1, pageSize = 10 } = body;

  const dataSource = [...jobListDataSource].slice(
    ((page as number) - 1) * (pageSize as number),
    (page as number) * (pageSize as number),
  );

  const result = {
    data: dataSource,
    total: jobListDataSource.length,
    pageSize,
    page: parseInt(`${page}`, 10) || 1,
  };

  return res.json(result);
}

function updateJob(req: Request, res: Response, u: string, b: Request) {
  const body = (b && b.body) || req.body;
  const { jobId } = body;
  let newRule = {};
  jobListDataSource = jobListDataSource.map(item => {
    if (item.jobId === jobId) {
      newRule = { ...item, ...body };
      return { ...item, ...body };
    }
    return item;
  });
  res.json(newRule);
}

function getAnchorGroupList(req: Request, res: Response, u: string, b: Request) {
  const body = (b && b.body) || req.body;
  const { page = 1, pageSize = 10 } = body;

  const dataSource = [...anchorGroupListDataSource].slice(
    ((page as number) - 1) * (pageSize as number),
    (page as number) * (pageSize as number),
  );

  const result = {
    data: dataSource,
    total: anchorGroupListDataSource.length,
    pageSize,
    page: parseInt(`${page}`, 10) || 1,
  };

  return res.json(result);
}

function addAnchorGroup(req: Request, res: Response, u: string, b: Request) {
  const body = (b && b.body) || req.body;
  anchorGroupListDataSource.unshift({ ...body, creator: '小叶' });
  res.json({ success: true });
}

function getPublisherList(req: Request, res: Response, u: string, b: Request) {
  const body = (b && b.body) || req.body;
  const { page = 1, pageSize = 10 } = body;

  const dataSource = [...publisherListDataSource].slice(
    ((page as number) - 1) * (pageSize as number),
    (page as number) * (pageSize as number),
  );

  const result = {
    data: dataSource,
    total: publisherListDataSource.length,
    pageSize,
    page: parseInt(`${page}`, 10) || 1,
  };

  return res.json(result);
}

function updatePublisher(req: Request, res: Response, u: string, b: Request) {
  const body = (b && b.body) || req.body;
  const { blPublisherId } = body;
  let newRule = {};
  publisherListDataSource = publisherListDataSource.map(item => {
    if (item.blPublisherId === blPublisherId) {
      newRule = { ...item, ...body };
      return { ...item, ...body };
    }
    return item;
  });
  res.json(newRule);
}

function getAnchorList(req: Request, res: Response, u: string, b: Request) {
  const body = (b && b.body) || req.body;
  const { page = 1, pageSize = 10 } = body;

  const dataSource = [...anchorListDataSource].slice(
    ((page as number) - 1) * (pageSize as number),
    (page as number) * (pageSize as number),
  );

  const result = {
    data: dataSource,
    total: anchorListDataSource.length,
    pageSize,
    page: parseInt(`${page}`, 10) || 1,
  };

  return res.json(result);
}

function getDeclarationList(req: Request, res: Response, u: string, b: Request) {
  const body = (b && b.body) || req.body;
  const { page = 1, pageSize = 10 } = body;

  const dataSource = [...declarationListDataSource].slice(
    ((page as number) - 1) * (pageSize as number),
    (page as number) * (pageSize as number),
  );

  const result = {
    data: dataSource,
    total: declarationListDataSource.length,
    pageSize,
    page: parseInt(`${page}`, 10) || 1,
  };

  return res.json(result);
}

function updateDeclaration(req: Request, res: Response, u: string, b: Request) {
  const body = (b && b.body) || req.body;
  const { id } = body;
  let newRule = {};
  declarationListDataSource = declarationListDataSource.map(item => {
    if (item.id === id) {
      newRule = { ...item, ...body };
      return { ...item, ...body };
    }
    return item;
  });
  res.json(newRule);
}

export default {
  [`POST ${AdminAPI.GROUP_LIST}`]: getGroupList,
  [`POST ${AdminAPI.GROUP_UPDATE}`]: updateGroup,
  [`POST ${AdminAPI.JOB_LIST}`]: getJobList,
  [`POST ${AdminAPI.JOB_UPDATE}`]: updateJob,
  [`POST ${AdminAPI.ANCHOR_GROUP_LIST}`]: getAnchorGroupList,
  [`POST ${AdminAPI.ADD_ANCHOR_GROUP}`]: addAnchorGroup,
  [`POST ${AdminAPI.PUBLISHER_LIST}`]: getPublisherList,
  [`POST ${AdminAPI.PUBLISHER_UPDATE}`]: updatePublisher,
  [`POST ${AdminAPI.ANCHOR_LIST}`]: getAnchorList,
  [`POST ${AdminAPI.DECLARATION_LIST}`]: getDeclarationList,
  [`POST ${AdminAPI.DECLARATION_UPDATE}`]: updateDeclaration,
};
