// index.js
// 获取应用实例
const app = getApp()
import {
  VueLikePage
} from '../../utils/page'

import { CDN_URL,API_BASE_URL,TYPE } from '../../config/index'
import {isPhoneX} from './../../utils/tools'

import Router from '../../utils/routes'
import Toast from '../../components/toast/toast';


VueLikePage([],{
  data:{
    cdn_url:CDN_URL,
    isBackCamera:false,
    current:'1',
    currentImg:'',
    isSelect: false,
    form:''
  },
  methods:{
    onLoad:function (options) {
      let {id} = options
      this.setData({
        current:id
      })
      let left = app.globalData.deviceInfo.windowWidth - app.globalData.top.right

      isPhoneX().then(res => {
        this.setData({
            isIphoneX: res,
            top:app.globalData.top,
            left
        })
      })
    },
    onShow:function () {
      // this.reTake()
      if (this.data.form == 'work') {
        this.setData({
          form:''
        })
        this.reTake()
      }
    },
    loadcompele(){
      this.setData({
        loadCompele: true
      })
    },
    back(){
      if (this.data.currentImg) {
        this.reTake()
      }
      else{
        wx.navigateBack()
      }
    },
    changPosition(){
      this.setData({
        isBackCamera:!this.data.isBackCamera
      })
    },
    reTake(){
      this.setData({
        currentImg:'',
        isSelect:false
      })
    },
    submit(){
      let toast = Toast.loading({
        message:'正在生成中，请耐心等候',
        selector: '#loading-toast'
      });
        
      wx.uploadFile({
        url: `${API_BASE_URL}/api/uploadFaceSwap/${TYPE[this.data.current]}`,
        filePath: this.data.currentImg,
        name: 'file',
        success: (res)=>{
          let data = JSON.parse(res.data)
          if (data.code==0) {
            this.setData({
              work:data.msg
            })

            Router.push({
              url: 'work',
              query: {
                vr_link:data.msg,
                id: this.data.current
              }
            })
          } 

          else{
            Toast.fail({
              message:this.data.isSelect?'无法识别人脸，请选取正脸照片':'无法识别人脸，请正对摄像头拍照',
              selector: '#fail-toast',
              callBack:()=>{
                if (this.data.isSelect) {
                  this.back()
                  wx.chooseImage({
                    sourceType:["album"],
                    count: 1,
                    sizeType: ['original', 'compressed'],
                    success:res =>{
                      this.setData({
                        currentImg: res.tempFilePaths[0],
                        isSelect:true
                      })
                    },
                    fail:error =>{
                      console.log(error);
                    }
                  })
                }
                else{
                  this.reTake()
                }
              }
            });
          }
        },
        fail:()=>{
          Toast.fail({
            message:this.data.isSelect?'无法识别人脸，请选取正脸照片':'无法识别人脸，请正对摄像头拍照',
            selector: '#fail-toast',
            callBack:()=>{
              if (this.data.isSelect) {
                this.back()
                wx.chooseImage({
                  sourceType:["album"],
                  count: 1,
                  sizeType: ['original', 'compressed'],
                  success:res =>{
                    this.setData({
                      currentImg: res.tempFilePaths[0],
                      isSelect:true
                    })
                  },
                  fail:error =>{
                    console.log(error);
                  }
                })
              }
              else{
                this.reTake()
              }
            }
          });
        },
        complete(){
          toast.hideLoading()
        }
      })
      
    },
    selectImage(){
      Toast.message({
        message:'请选取正脸照片',
        selector: '#tips-toast',
        callBack:()=>{
          wx.chooseImage({
            sourceType:["album"],
            count: 1,
            sizeType: ['original', 'compressed'],
            success:res =>{
              this.setData({
                currentImg: res.tempFilePaths[0],
                isSelect:true
              })
    
              console.log(this.data.currentImg,this.data.isSelect)
    
            },
            fail:error =>{
              console.log(error);
            }
          })
        }
      });
    },
    takePhoto() {
      const ctx = wx.createCameraContext()
      ctx.takePhoto({
        quality: 'high',
        success: (res) => {
          this.setData({
            currentImg: res.tempImagePath,
            isSelect:false
          })
        }
      })
    }
  }
})

