import {
  fakeSubmitForm,
  updateReportInfo,
  getExamInfo,
  createReportCommon,
  createTestCase,
  getTestCaseList,
  getCaseBugList,
  getBugInfo,
  updateTestCaseList,
  getReportInfo,
  createCaseBug,
  getRecommendList,
  getPathInfo,
  getRecommendPath,
  goodReport,
  badReport,
  cancelGoodReport,
  cancelBadReport,
  recordAction,
  uploadImage,
  getIdByToken,
  forkReport,
  recordGoodReport,
  recordBadReport,
  bugSimilarity
} from './service';

const Model = {
  namespace: 'editReport',
  state: {
    current: 'info',
    osType: [],
    step: {
      payAccount: 'ant-design@alipay.com',
      receiverAccount: 'test@example.com',
      receiverName: 'Alex',
      amount: '500',
    },
    taskName: '',
    collaborative_type:'0',
    reportCommonInfo: {},
    testCaseList: [],
    caseBugList: [],
    categories: [],
    pathInfo: [],
    recommendPath: [],
    bugRecommendList: [],
    commonId:{
      // result:"success",
      // roleId:1,
      // caseId:"3714",
      // examId:"3714",
      // beginTime:"1621945823000",
      // endTime:"1624291200000",
      // userId:"99221",
      // case_take_id:"3714-3714",
    },
  },
  effects: {
    * submitStepForm({ payload }, { call, put }) {
      yield call(fakeSubmitForm, payload);
      yield put({
        type: 'saveStepFormData',
        payload,
      });
      yield put({
        type: 'saveCurrentStep',
        payload: 'detail',
      });
    },
    * getOsType({ payload }, { call, put }) {
      const { examId } = payload;
      const res = yield call(getExamInfo, examId);
      let osType = res?.result?.paper_type;
      osType = JSON.parse(osType)['subTitles'][2]?.value;
      yield put({
        type: 'saveOsType',
        payload: osType,
      });
    },
    //todo:根据id获取token
    * getIdByToken({ payload }, { call, put }) {
      const { token } = payload;
      let res = yield call(getIdByToken, token)
      res['case_take_id'] = res['caseId'] + '-' + res['examId'];
      yield put({
        type: 'saveCommonId',
        payload: res,
      });
      return res;
    },
    //todo:更新报告基础信息
    * updateReportCommonDetail({ payload }, { call, put }) {
      let { formData } = payload;
      yield call(updateReportInfo, formData);
      let res = yield call(getReportInfo,
        formData.get('case_take_id'), formData.get('worker_id'));
      yield put({
        type: 'saveReportCommonInfo',
        payload: res.result,
      });
    },
    //todo:获取三级菜单信息
    * getCategories({ payload }, { call, put }) {
      const { examId } = payload;
      let res = yield call(getExamInfo, examId);
      let categories = res?.result?.json;
      categories = JSON.parse(categories);
      yield put({
        type: 'saveCategories',
        payload: categories,
      });
    },
    //todo:获取报告用例列表
    * getTestCaseList({ payload }, { call, put }) {
      const { report_id } = payload;
      let res = yield call(getTestCaseList, report_id);
      yield put({
        type: 'saveTestCaseList',
        payload: res.result,
      });
      return res.result;
    },
    //todo:获取报告基础信息
    * getReportInfo({ payload }, { call, put }) {
      const { case_take_id, worker_id } = payload;
      let res = yield call(getReportInfo, case_take_id, worker_id);
      if (res.status === 200) {
        yield put({
          type: 'saveReportCommonInfo',
          payload: res.result,
        });
      }
      return res;
    },
    //todo:保存报告基础信息
    * saveReportCommonDetail({ payload }, { call, put }) {
      let {formData,case_take_id,worker_id} = payload;
      yield call(createReportCommon, formData);
      //res拿到报告id,组装生成report对象
      // let report = { id: res.id };
      // for (let key of payload.keys()) {
      //   report[key] = payload.get(key);
      // }
      yield put.resolve({
        type: 'getReportInfo',
        payload: { case_take_id,worker_id },
      });
      yield put({
        type: 'saveCurrentStep',
        payload: 'detail',
      });
    },
    //todo:创建新用例
    * createTestCase({ payload }, { call, put,select }) {
      let { formData, report_id } = payload;
      let res = yield call(createTestCase, formData);
      yield put.resolve({
        type: 'getTestCaseList',
        payload: { report_id },
      });
      const testCaseList = yield select(state => {
        return state.editReport.testCaseList;
      })
      return {res,testCaseList};
    },
    //todo:更新用例
    * updateTestCase({ payload }, { call, put }) {
      let { formData, report_id } = payload;
      let res = yield call(updateTestCaseList, formData);
      yield put({
        type: 'getTestCaseList',
        payload: { report_id },
      });
      return res;
    },
    //todo:获取bug列表
    * getCaseBugList({ payload }, { call, put }) {
      let bugIdList = yield call(getCaseBugList, payload);//bug id list
      yield put({ type: 'getBugInfo', payload: { bugIdList } });
    },
    //todo:根据bug id获取bug detail
    * getBugDetail({ payload }, { call, put }) {
      const { id } = payload;
      let bugInfo = yield call(getBugInfo, id);//bug id list
      return bugInfo;
    },
    //todo:根据bugIdList获取bug详情
    * getBugInfo({ payload }, { call, put }) {
      const { bugIdList } = payload;
      let newBugList = [];
      for (const item of bugIdList) {
        let newBugInfo = yield call(getBugInfo, item);
        newBugList = [...newBugList, newBugInfo];
      }
      yield put({
        type: 'saveCaseBugList',
        payload: newBugList,
      });
      return newBugList;
    },
    //todo:保存bug
    * createCaseBug({ payload }, { call, put }) {
      const { useCase, formData } = payload;
      let res = yield call(createCaseBug, formData);
      yield put({
        type: 'getCaseBugList',
        payload: useCase,
      });
      return res;
    },
    //todo:获取bug 路径
    * getPathInfo({ payload }, { call, put }) {
      const { case_take_id, report_id } = payload;
      let res = yield call(getPathInfo, case_take_id, report_id);
      yield put({
        type: 'savePathInfo',
        payload: res,
      });
    },
    //todo:获取bug 推荐路径
    * getBugRecommendPath({ payload }, { call, put }) {
      const { case_take_id, report_id } = payload;
      let res = yield call(getRecommendPath, case_take_id, report_id);
      yield put({
        type: 'saveRecommendPath',
        payload: res,
      });
      return res;
    },
    //todo:获取bug推荐列表
    * getBugRecommendList({ payload }, { call, put }) {
      const { case_take_id, report_id } = payload;
      let res = yield call(getRecommendList, case_take_id, report_id);
      yield put({
        type: 'saveBugRecommendList',
        payload: res,
      });
      return res;
    },
    //todo:点赞bug报告
    * goodForReport({ payload }, { call, put }) {
      const { id, report_id, user_id, action } = payload;
      let res = yield call(goodReport, id, report_id);
      if (res.status === 200) {
        yield call(recordAction, user_id, id, action);
      }
    },
    //todo:取消点赞bug报告
    * cancelGoodForReport({ payload }, { call, put }) {
      const { id, report_id } = payload;
      yield call(cancelGoodReport, id, report_id);
    },
    //todo:点踩bug报告
    * badForReport({ payload }, { call, put }) {
      const { id, report_id, user_id, action } = payload;
      let res = yield call(badReport, id, report_id);
      if(res.status === 200){
        yield call(recordAction, user_id, id, action);
      }
    },
    //todo:取消点踩bug报告
    * cancelBadForReport({ payload }, { call, put }) {
      const { id, report_id } = payload;
      yield call(cancelBadReport, id, report_id);
    },

    //todo:存储用户行为
    * recordAction({ payload }, { call, put }) {
      const { user_id, target_id, action } = payload;
      yield call(recordAction, user_id, target_id, action);
    },

    //todo:上传照片
    * uploadImg({ payload }, { call, put }) {
      const { formData } = payload;
      let res = yield call(uploadImage, formData);
      return res;
    },

    //todo:fork report
    * forkReport({ payload }, { call, put }) {
      const { page1, page2, page3, bug_category, severity, recurrent } = payload;
      yield call(forkReport, page1, page2, page3, bug_category, severity, recurrent);
    },
    //todo:记录点赞行为
    * recordGoodReport({ payload }, { call, put }) {
      const { user_id, target_id,action } = payload;
      yield call(recordGoodReport, user_id, target_id,action);
    },
    //todo:记录点踩行为
    * recordBadReport({ payload }, { call, put }) {
      const { user_id, target_id,action } = payload;
      yield call(recordBadReport, user_id, target_id,action);
    },
    //todo:bug相似度计算
    * bugSimilarity({ payload }, { call, put }) {
      const { case_take_id, type, content } = payload;
      const res = yield call(bugSimilarity, case_take_id, type, content);
      return res;
    },

  },
  reducers: {
    saveCurrentStep(state, { payload }) {
      return { ...state, current: payload };
    },
    saveStepFormData(state, { payload }) {
      return { ...state, step: { ...state.step, ...payload } };
    },
    saveOsType(state, { payload }) {
      return { ...state, osType: payload };
    },
    saveReportCommonInfo(state, { payload }) {
      return { ...state, reportCommonInfo: payload };
    },
    saveTestCaseList(state, { payload }) {
      return { ...state, testCaseList: payload };
    },
    saveCaseBugList(state, { payload }) {
      return { ...state, caseBugList: payload };
    },
    saveCategories(state, { payload }) {
      return { ...state, categories: payload };
    },
    saveBugRecommendList(state, { payload }) {
      return { ...state, bugRecommendList: payload };
    },
    savePathInfo(state, { payload }) {
      return { ...state, pathInfo: payload };
    },
    saveRecommendPath(state, { payload }) {
      return { ...state, recommendPath: payload };
    },
    saveCommonId(state, { payload }){
      return { ...state, commonId: payload };
    }
  },
};
export default Model;
