import inputData from '../data/input';

import verification from '$utils/verification';

import {LibDataComponents} from '$utils/verification/const';


export default <LibMixins>{

    data(){
        return {
            inputData:inputData(),

            components: LibDataComponents
        }
    },

    inject:['setResultData','setStep','close'],

    methods:{

        submitVerify(callback){

            let useInputData = [];

            for (let key in this.inputData) {
                if (this.inputData.hasOwnProperty(key)) {
                    useInputData.push(...this.inputData[key]);
                }
            }

            return verification.verificationPromise(useInputData,true).then((resultData)=>{
                resultData.sound_duration = this.$refs.sound && this.$refs.sound.countdown || 3;
                return callback(this.setResultData(resultData));
            });

        },

        submit(obj){


            return this.uploadImageAll(obj.data).then((resultData)=>{

                this.$request({
                    url:'player/apply_play_game',
                    data: resultData,
                    token:true,
                    message:true,
                    failMessage:true
                }).then((data)=>{
                    if (data.isSuccess) {

                        this.$store.commit('setGameItemStatus',{
                            gid: resultData.gid,
                            status:1
                        });

                        this.$store.dispatch('updateGameStatusPromise');

                        return this.close();
                    } else {
                        return obj.none();
                    }
                }).catch(obj.fail);

            }).catch(obj.fail);

        },

        // 执行图片上传
        uploadImageAll(resultData){

            return new Promise((resolve, reject)=>{

                let upload = [];
                for (let key in resultData) {
                    if (resultData.hasOwnProperty(key)) {
                        let item = resultData[key];
                        // 如果为文件触发
                        if (item instanceof File || item instanceof Blob) {
                            upload.push(this.uploadFile(resultData,key,item));
                        }
                    }
                }

                if (upload.length > 0) {
                    Promise.all(upload).then(()=>{
                        return resolve(resultData);
                    }).catch(reject);
                } else {
                    return resolve(resultData);
                }

            });

        },

        uploadFile(resultData,key,file){

            return new Promise<void>( (resolve, reject)=>{

                // @ts-ignore
                this.$request.uploadFile({
                    url:'hxupload/img_upload',
                    data:{
                        file:file
                    },
                    token:true
                }).then((data)=>{

                    if (data.isSuccess)  {
                        resultData[key] = data.data;
                        resolve();
                    } else {
                        reject();
                    }

                }).catch(reject);

            });

        }

    }

}