import { apiService } from './api.service';
import { message } from 'antd';
import { getImageUrlAbsolute } from "educoder";
import { calcChangeLineNum } from './utils/calc-change-line-num';
import { underscoreToCamelcase } from './utils/camelcase-convert';
import axios from 'axios';
import sha1 from 'sha1';

export const prService = {
  async getPRByIid(projectsId, owner, mergeId) {
    const res = await axios.get(`/v1/${owner}/${projectsId}/pulls/${mergeId}.json`);
    let data = underscoreToCamelcase(res.data);
    let newData = {
      ...data,
      checkSuites: [],
      iid: data.index,
      sourceBranch: data.head,
      sourceProjectId: projectsId,
      // state: "opened",
      // targetBranch: "master",
      // targetProjectId: 42422,
      diff: {
        baseCommitSha: data.baseCommitSha,
        commitsCount: data.commitNum,
        headCommitSha: data.headCommitSha,
        startCommitSha: data.mergeBase,
        // delLineNum: 379,
        // addLineNum: 753,
        // createdAt: "2021-05-20T14:27:46+0800",
        // filesCount: 22,
        // overflow: false,
        // startCommitSha: "2566c6dec7756e51f7f16267d9a2a63116ac015b",
        // updatedAt: "2021-05-20T14:27:46+0800",
      },

    };
    return newData;
  },

  async getDiffVersions(projectsId, owner, mergeId) {
    const res = await axios.get(`/v1/${owner}/${projectsId}/pulls/${mergeId}/versions.json`);
    return Array.isArray(res.data.versions) ? underscoreToCamelcase(res.data.versions) : [];
  },

  async getDiffs(
    projectId,
    owner,
    from,
    to,
  ) {
    const res = await axios.get(`/v1/${owner}/42422/compare.json`, { params: { from, to } })
    const changeLineNum = calcChangeLineNum(res.diffs);
    return {
      ...res,
      ...changeLineNum,
    };
  },

  async getDiffOverviews(projectsId, owner, mergeId, versionId) {
    const res = await axios.get(`/v1/${owner}/${projectsId}/pulls/${mergeId}/versions/${versionId}/diff.json`);
    let files = res.data.files;
    for (const [i, item] of files.entries()) {
      // item.aMode= "100644";
      // item.bMode= "0";
      // item.markAsRead= true;
      // item.tooLarge= false;
      // item.updatedAfterRead= false;

      item.compareDiffId = versionId;
      item.id = item.name;
      item.addLineNum = item.addition;
      item.binaryFile = item.is_bin;
      item.delLineNum = item.deletion;
      item.deletedFile = item.is_deleted;
      item.newFile = item.is_created;
      item.newPath = item.name;
      item.oldPath = item.oldname;
      item.renamedFile = item.is_renamed;
    }
    return Array.isArray(res.data.files) ? underscoreToCamelcase(res.data.files) : []
  },

  async getCommentPack(projectsId, owner, mergeId,params) {
    const res = await axios.get(`/v1/${owner}/${projectsId}/pulls/${mergeId}/journals.json`,{params});
    let journals=underscoreToCamelcase(res.data.journals);
    for(const item of journals){
      item.author=item.user;
      item.author.avatarUrl=item.user.imageUrl?getImageUrlAbsolute(item.user.imageUrl):'';
      item.author.username=item.user.name;
      item.type='Common';
      item.stDiff=item.diff;
      item.discussionId=item.parentId
      // item.lineType='old';
    }
    return journals;
  },

  async editPRComment(projectsId, owner, mergeId, noteId, data) {
    const res = await axios.patch(`/v1/${owner}/${projectsId}/pulls/${mergeId}/journals/${noteId}.json`, data);
    return res.data;
  },

  async getDiffById(
    projectsId, owner, mergeId, versionId,
    params = {}
  ) {
    const res = await axios.get(`/v1/${owner}/${projectsId}/pulls/${mergeId}/versions/${versionId}/diff.json`, { params });

    let data = res.data;
    data.compareDiffId = versionId;
    data.id = data.name;
    data.addLineNum = data.addition;
    data.binaryFile = data.is_bin;
    data.delLineNum = data.deletion;
    data.deletedFile = data.is_deleted;
    data.newFile = data.is_created;
    data.newPath = data.name;
    data.oldPath = data.oldname;
    data.renamedFile = data.is_renamed;
    return data;
  },

  async getFileReadMarks(projectsId, owner, mergeId,) {
    const res = await axios.get(`/${owner}/${projectsId}/pulls/${mergeId}/diffs/mark_files.json`);
    let data=[];
    for (const item of res.data.files) {
      if(item.mark_as_read){
        item.newPath = item.name;
        item.file_path_sha2 = item.file_path_sha;
        item.file_path_sha = sha1(item.newPath);
        data.push(item);
      }
    }
    return underscoreToCamelcase(data);
  },

  async markFileAsRead(projectsId, owner, mergeId, data) {
    const res = await axios.put(`/${owner}/${projectsId}/pulls/${mergeId}/diffs/mark_file_as_read.json`, data);
    return res.data;
  },
  async markFileAsUnread(projectsId, owner, mergeId, data) {
    const res = await axios.put(`/${owner}/${projectsId}/pulls/${mergeId}/diffs/mark_file_as_unread.json`, data);
    return res.data;
  },

  async addComment(projectsId, owner, mergeId, data) {
    const res = await axios.post(`/v1/${owner}/${projectsId}/pulls/${mergeId}/journals.json`, data);
    let commentData=underscoreToCamelcase(res.data);
    commentData.author=commentData.user;
    commentData.author.avatarUrl=commentData.user.imageUrl?getImageUrlAbsolute(commentData.user.imageUrl):'';
    commentData.author.username=commentData.user.name;
    commentData.type='Common';
    commentData.stDiff=commentData.diff;
    commentData.discussionId=commentData.parentId
    return commentData;
  },

  async deleteComment(projectsId, owner, mergeId,id){
    const res = await axios.delete(`/v1/${owner}/${projectsId}/pulls/${mergeId}/journals/${id}.json`);
    return res.data;
  },

  async createReview(projectsId, owner, mergeId, data) {
    const res = await axios.post(`/v1/${owner}/${projectsId}/pulls/${mergeId}/reviews.json`, data);
    console.log('createReview--data');
    if (res.data.id) {
      return res.data;
    } else {
      message.error(res.data && res.data.message);
    }
  },

  async getReviews(projectsId, owner, mergeId,) {
    const res = await axios.get(`/v1/${owner}/${projectsId}/pulls/${mergeId}/reviews.json`,);
    let reviews=underscoreToCamelcase(res.data.reviews);
    for(const item of reviews){
      item.author=item.reviewer;
      item.author.avatarUrl=getImageUrlAbsolute(item.reviewer.imageUrl);
      item.author.username=item.reviewer.name;
      item.author.webUrl='';
      item.body=item.content;
      item.pending=false;
      item.pullRequestId=mergeId;
    }
    return reviews;
  },

  async commitReview(projectsId, owner, mergeId, body) {
    return await apiService.put(
      `/api/v3/projects/42422/pull_requests/13055/reviews`,
      undefined,
      {
        body,
      }
    );
  },
};
