import {
  Provide,
  Body,
  Inject,
  Post,
  ALL,
  Logger,
  Get,
  Param,
  Files,
  Fields,
} from '@midwayjs/decorator';
import {
  CoolController,
  BaseController,
  CoolTag,
  CoolUrlTag,
  TagTypes,
} from '@cool-midway/core';
import { PayService } from '../../service/pay';
import { BaseSysParamService } from '../../../base/service/sys/param';
import { Context } from '@midwayjs/koa';
import { OrderService } from '../../service/order';
import { ChannelService } from '../../service/channel';
import { ILogger } from '@midwayjs/logger';
import { WithdrawService } from '../../service/withdraw';
import { KycService } from '../../service/kyc';
import { RepayService } from '../../service/repay';
import { WhiteIpMiddleware } from '../../middleware/whiteIp';
import { KycOpenService } from '../../service/openKyc';
import { PayRefundService } from '../../service/payRefund';

@Provide()
@CoolController()
@CoolUrlTag()
export class PayOpenController extends BaseController {
  @Inject()
  payService: PayService;

  @Inject()
  ctx: Context;

  @Inject()
  baseSysParamService: BaseSysParamService;

  @Inject()
  orderService: OrderService;

  @Inject()
  withdrawService: WithdrawService;

  @Inject()
  channelService: ChannelService;

  @Inject()
  kycService: KycService;
  
  @Inject()
  kycOpenService: KycOpenService;

  @Inject()
  repayService: RepayService;

  @Inject()
  payRefundService: PayRefundService;

  @Logger()
  logger: ILogger;

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/order', { summary: '代收下单' })
  public async order(@Body(ALL) payload: any) {
    return this.ok(await this.payService.order(payload));
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/query', { summary: '代收查询' })
  public async query(@Body(ALL) payload: any) {
    return this.ok(await this.payService.query(payload));
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/refund/query', { summary: '退款查询' })
  public async queryRefund(@Body(ALL) payload: any) {
    return this.ok(await this.payRefundService.queryRefund(payload));
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/refund', { summary: '退款' })
  public async refund(@Body(ALL) payload: any) {
    return this.ok(await this.payRefundService.refund(payload));
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/withdraw', { summary: '发起代付', middleware: [WhiteIpMiddleware] })
  public async withdraw(@Body(ALL) payload: any) {
    return this.ok(await this.repayService.withdraw(payload));
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/withdrawQuery', { summary: '代付查询', middleware: [WhiteIpMiddleware] })
  public async withdrawQuery(@Body(ALL) payload: any) {
    return this.ok(await this.repayService.queryWithdraw(payload));
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/queryBalance', { summary: '余额查询', middleware: [WhiteIpMiddleware] })
  public async queryBalance(@Body(ALL) payload: any) {
    return this.ok(await this.repayService.queryBalance(payload));
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/getBanks', { summary: '代付银行查询', middleware: [WhiteIpMiddleware] })
  public async getBanks(@Body(ALL) payload: any) {
    return this.ok(await this.repayService.getBanks(payload));
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/notifyTest', { summary: '回调通知' })
  public async notifyTest(@Body(ALL) payload: any) {
    this.logger.warn('收到测试回调数据', JSON.stringify(payload));
    console.log('收到测试回调数据', payload);
    this.ctx.body = 'success';
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/SunCard/notifyOrder', { summary: '交易订单回调通知' })
  public async notifySunCardOrderGet(@Body(ALL) payload: any) {
    const headers = this.ctx.headers;
    this.logger.warn('SunCard收到交易订单回调数据', JSON.stringify(payload), headers);
    await this.orderService.handleNotify('SUN_CARD', payload, headers);
    this.ctx.body = {
      "is_success": "true",
      "message": "success"
    };
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/hambit/bra/notifyOrder', { summary: '巴西交易订单回调通知' })
  public async hambitBraNotifyOrderGet(@Body(ALL) payload: any) {
    const headers = this.ctx.headers;
    this.logger.warn('hambit收到交易订单回调数据', JSON.stringify(payload), headers);
    await this.orderService.handleNotify('HB_BRA', payload, headers);
    this.ctx.body = {
      "code": "200",
      "success": "true"
    };
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/hambit/bra/notifyWithdraw', { summary: '巴西交易订单回调通知' })
  public async hambitBraNotifyWithdraw(@Body(ALL) payload: any) {
    const headers = this.ctx.headers;
    this.logger.warn('hambit收到代付订单回调数据', JSON.stringify(payload), headers);
    await this.withdrawService.handleWithdrawNotify('HB_BRA', payload, headers);
    this.ctx.body = {
      "code": "200",
      "success": "true"
    };
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/hambit/inr/notifyOrder', { summary: '印度交易订单回调通知' })
  public async hambitInrNotifyOrderGet(@Body(ALL) payload: any) {
    const headers = this.ctx.headers;
    this.logger.warn('hambit收到交易订单回调数据', JSON.stringify(payload), headers);
    await this.orderService.handleNotify('HB_INR', payload, headers);
    this.ctx.body = {
      "code": "200",
      "success": "true"
    };
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/hambit/inr/notifyWithdraw', { summary: '巴西交易订单回调通知' })
  public async hambitInrNotifyWithdraw(@Body(ALL) payload: any) {
    const headers = this.ctx.headers;
    this.logger.warn('hambit收到代付订单回调数据', JSON.stringify(payload), headers);
    await this.withdrawService.handleWithdrawNotify('HB_INR', payload, headers);
    this.ctx.body = {
      "code": "200",
      "success": "true"
    };
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/ach/notifyOrder', { summary: 'ACH订单回调通知' })
  public async achNotifyOrderGet(@Body(ALL) payload: any) {
    const headers = this.ctx.headers;
    this.logger.warn('ach收到交易订单回调数据', JSON.stringify(payload), headers);
    await this.orderService.handleNotify('ACH', payload, headers);
    this.ctx.body = 'success';
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/kyc/level', { summary: 'kyc level 查询' })
  public async kycLevel(@Body(ALL) payload: any) {
    return this.ok(await this.kycService.getLevel(payload.orderNo));
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Get('/kyc/countries', { summary: 'kyc countries 查询' })
  public async kycCountries() {
    return this.ok(await this.kycService.getCountries());
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/kyc/basic', { summary: 'kyc basic 保存' })
  public async kycBasic(@Body(ALL) payload: any) {
    return this.ok(await this.kycService.createBasic(payload));
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/kyc/advanced', { summary: 'kyc advanced 保存' })
  public async kycAdvanced(@Files() files, @Fields() fields) {
    return this.ok(await this.kycService.createAdvanced(fields, files));
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/kyc/premium', { summary: 'kyc basic 保存' })
  public async kycPremium(@Files() files, @Fields() fields) {
    return this.ok(await this.kycService.createPremium(fields, files));
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/kyc/toPay', { summary: 'kyc topay' })
  public async kycToPay(@Body(ALL) payload: any) {
    return this.ok(await this.kycService.toPay(payload));
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/kyc/toBaisc', { summary: 'kyc basic 保存' })
  public async submitBaisc(@Body(ALL) payload: any) {
    return this.ok(await this.kycOpenService.createOpenBasic(payload));
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/kyc/toAdvanced', { summary: 'kyc advanced 保存' })
  public async submitAdvanced(@Files() files, @Fields() fields) {
    return this.ok(await this.kycOpenService.createOpenAdvanced(fields, files));
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/kyc/toPremium', { summary: 'kyc basic 保存' })
  public async submitPremium(@Files() files, @Fields() fields) {
    return this.ok(await this.kycOpenService.createOpenPremium(fields, files));
  }

  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/kyc/getCustomer', { summary: 'kyc basic 保存' })
  public async getCustomer(@Body(ALL) payload: any) {
    return this.ok(await this.kycOpenService.getCustomer(payload));
  }
}
