'use strict';

const Controller = require('egg').Controller;

class InstitutionController extends Controller {
  // 课程列表
  async getClassList() {
    const { ctx } = this;
    const queryParams = ctx.request.query;
    let ret = await ctx.service.course.v5.institution.getClassList(queryParams);
    ret = await ctx.helper.cipherByAES(ret);

    ctx.success(ret);
  }

  // 课程详情
  async getClassInfo() {
    const { ctx } = this;
    const class_id = ctx.params.class_id;
    if (!class_id) {
      ctx.failed('error class_id');
    }
    const ret = await ctx.service.course.v5.institution.getClassInfo(class_id);

    ctx.success(ret);
  }

  // 收藏课程列表
  async getCollectionClassList() {
    const { ctx } = this;
    const queryParams = ctx.request.query;
    const ret = await ctx.service.course.v5.institution.getCollectionClassList(queryParams);

    ctx.success(ret);
  }

  // 收藏课程
  async collectClass() {
    const { ctx } = this;
    const class_id = ctx.params.class_id;
    if (!class_id) {
      ctx.failed('error class_id');
    }
    const ret = await ctx.service.course.v5.institution.collectClass(class_id);

    ctx.success(ret);
  }

  // 取消收藏课程
  async delCollectClass() {
    const { ctx } = this;
    const class_id = ctx.params.class_id;
    if (!class_id) {
      ctx.failed('error class_id');
    }
    const ret = await ctx.service.course.v5.institution.delCollectClass(class_id);

    ctx.success(ret);
  }

  // 获取科普文章
  async getArticleByCategory() {
    const { ctx, service } = this;
    const queryParams = ctx.request.query;
    if (ctx.isEmpty(queryParams) || ctx.isEmpty(queryParams.cat_id)) {
      ctx.failed('cat_id is empty');
    }

    const ret = await service.course.v5.institution.getArticleByCategory(queryParams.cat_id);

    ctx.success(ret);
  }

  // 牛通社首页
  async getNiutongshe() {
    const { ctx, service } = this;
    const ret = await service.course.v5.institution.getNiutongshe();

    ctx.success(ret);
  }

  // 获取搜索联想
  async getSearchRecommend() {
    const { ctx, service } = this;
    const queryParams = ctx.request.query;
    if (ctx.isEmpty(queryParams) || ctx.isEmpty(queryParams.word)) {
      ctx.failed('word is empty');
    }

    const ret = await service.course.v5.institution.getSearchRecommend(queryParams);

    ctx.success(ret);
  }

  // 搜索
  async getSearch() {
    const { ctx, service } = this;
    const queryParams = ctx.request.query;
    if (ctx.isEmpty(queryParams) || ctx.isEmpty(queryParams.word)) {
      ctx.failed('word is empty');
    }

    const ret = await service.course.v5.institution.getSearch(queryParams);

    ctx.success(ret);
  }

  // 获取视频列表
  async getVideoList() {
    const { ctx, service } = this;
    const queryParams = ctx.request.query;
    if (ctx.isEmpty(queryParams) || ctx.isEmpty(queryParams.class_id)) {
      ctx.failed('class_id is empty');
    }

    const ret = await service.course.v5.institution.getVideoList(queryParams);

    ctx.success(ret);
  }

  // 获取视频详情
  async getVideoInfo() {
    const { ctx, service } = this;
    const params = ctx.params;
    if (ctx.isEmpty(params) || ctx.isEmpty(params.id)) {
      ctx.failed('id is empty');
    }

    const ret = await service.course.v5.institution.getVideoInfo(params.id);

    ctx.success(ret);
  }

  // 获取课程首页
  async getHomeClassList() {
    const { ctx, service } = this;
    const queryParams = ctx.request.query;
    if (ctx.isEmpty(queryParams) || ctx.isEmpty(queryParams.cat_id)) {
      ctx.failed('cat_id is empty');
    }

    const ret = await service.course.v5.institution.getHomeClassList(queryParams);

    ctx.success(ret);
  }
}

module.exports = InstitutionController;
