package com.example.audiovisualrecord.ui.main;
import android.os.Environment;
import android.text.LoginFilter;
import android.util.Log;

import com.arthenica.mobileffmpeg.Config;
import com.arthenica.mobileffmpeg.FFmpeg;
import com.arthenica.mobileffmpeg.FFprobe;
import com.example.audiovisualrecord.R;
import com.example.audiovisualrecord.utils.api.ApiService;
import com.example.audiovisualrecord.utils.api.HttpMethods;
import com.example.audiovisualrecord.utils.api.apidata.CORequest.CORequest;
import com.example.audiovisualrecord.utils.api.apidata.CORequest.COResultList;
import com.example.audiovisualrecord.utils.api.apidata.searchtype.TYPERequest;
import com.example.audiovisualrecord.utils.api.apidata.searchtype.TYPEResultList;
import com.example.audiovisualrecord.utils.api.searcheqkd.EQKDRequest;
import com.example.audiovisualrecord.utils.api.searcheqkd.EQKDResultList;
import com.example.audiovisualrecord.utils.api.searchmntfct.MNTFCTRequest;
import com.example.audiovisualrecord.utils.api.searchmntfct.MNTFCTResultList;
import com.example.audiovisualrecord.utils.api.searchpmfct.PMFCTRequest;
import com.example.audiovisualrecord.utils.api.searchpmfct.PMFCTResultList;
import com.example.audiovisualrecord.utils.rxjava.SchedulerProviderImp;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;

import static com.arthenica.mobileffmpeg.Config.RETURN_CODE_CANCEL;
import static com.arthenica.mobileffmpeg.Config.RETURN_CODE_SUCCESS;

public class MainPresenter<V extends MainContract.View> implements MainContract.Presenter<V> {

//    private String KEY_SEARCH_CO = "6c66fcbd-6dfe-45a2-ad6b-cbcda09b25bd";
    private String KEY_SEARCH_CO = "3b0f7c72-5479-46c1-b26d-224748e64d7a";
    private String KEY_SEARCH_PZ = "78bc27f8-6c1b-4172-ba61-05798001671b";
    private String KEY_SEARCH_DP = "1c76477f-1fb0-49fb-a5bd-e75311bbe691";
    private String KEY_SEARCH_SHIFT = "3e161e49-30d5-44ce-bbc4-3bcff49f7c37";
    private String KEY_SEARCH_TYPE = "145ce81d-7bae-4848-a38a-e0e738385681";
    private String USER_ID = "N000158385";

    private V view;
    private String LOGIN_AUTHORIZED_ID = "acd9be92-46bf-4185-8721-5b60c67f0742";
    private ApiService api;
    SchedulerProviderImp schedulerProviderImp = new SchedulerProviderImp();
    private CompositeDisposable compositeDisposable;

    public MainPresenter(V view){
        this.view = view;
        api = HttpMethods.getInstance().getApi();
        compositeDisposable = new CompositeDisposable();
    }

    @Override
    public void onGetComData() {
        String url = view.getResourceString(R.string.api_on_getCO);
        final CORequest mCORequest = new CORequest(KEY_SEARCH_CO, USER_ID);
        compositeDisposable.add(api.getCO(url, mCORequest)
                .subscribeOn(schedulerProviderImp.io())
                .observeOn(schedulerProviderImp.ui())
                .subscribeWith(new DisposableObserver<COResultList>() {

                    @Override
                    public void onNext(COResultList mCOResultList) {
                        view.setCOData(mCOResultList.getcOResponseList());
                    }

                    @Override
                    public void onError(Throwable e) {
                       view.showDialogCaveatMessage(view.getResourceString(R.string.add_device_error));

                    }

                    @Override
                    public void onComplete() {

                    }
                })
        );
    }

    @Override
    public void onGetPZData() {
        String url = view.getResourceString(R.string.api_on_getMNTFCT);
        MNTFCTRequest mMNTFCTRequest = new MNTFCTRequest(KEY_SEARCH_PZ, USER_ID);
        compositeDisposable.add(api.getPZ(url, mMNTFCTRequest)
                .subscribeOn(schedulerProviderImp.io())
                .observeOn(schedulerProviderImp.ui())
                .subscribeWith(new DisposableObserver<MNTFCTResultList>() {

                    @Override
                    public void onNext(MNTFCTResultList mMNTFCTResultList) {
                        view.setPZData(mMNTFCTResultList.getmNTFCTResponse());
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.showDialogCaveatMessage(view.getResourceString(R.string.add_device_error));

                    }

                    @Override
                    public void onComplete() {

                    }
                })
        );
    }

    @Override
    public void onGetDPData(String CO, String PZ) {
        final PMFCTRequest mPMFCTRequest = new PMFCTRequest(KEY_SEARCH_DP, USER_ID, CO, PZ);
        String url = view.getResourceString(R.string.api_on_getDP);
        compositeDisposable.add(api.getDP(url, mPMFCTRequest)
                .subscribeOn(schedulerProviderImp.io())
                .observeOn(schedulerProviderImp.ui())
                .subscribeWith(new DisposableObserver<PMFCTResultList>() {

                    @Override
                    public void onNext(PMFCTResultList mPMFCTResultList) {
                        if (mPMFCTResultList.getmPMFCTResponseList().size() < 1) {
                            view.showDialogCaveatMessage(view.getResourceString(R.string.get_pmfct_error_no_data));
                        }else{
                            view.setDPNMData(mPMFCTResultList.getmPMFCTResponseList());
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        view.showDialogCaveatMessage(view.getResourceString(R.string.add_device_error));

                    }

                    @Override
                    public void onComplete() {

                    }
                })
        );
    }

    @Override
    public void onGetSHIFTData(String CO, String PMFCT) {
        EQKDRequest mEQKDRequest = new EQKDRequest(KEY_SEARCH_SHIFT, USER_ID, CO, PMFCT);
        String url = view.getResourceString(R.string.api_on_getSHIFT);
        compositeDisposable.add(api.getEQKD(url, mEQKDRequest)
                .subscribeOn(schedulerProviderImp.io())
                .observeOn(schedulerProviderImp.ui())
                .subscribeWith(new DisposableObserver<EQKDResultList>() {

                    @Override
                    public void onNext(EQKDResultList mEQKDResultList) {
                        if (mEQKDResultList.getmEQKDResponseList().size() < 1) {
                            view.showDialogCaveatMessage(view.getResourceString(R.string.get_shift_error_no_data));
                        }else{
                            view.setEQKDData(mEQKDResultList.getmEQKDResponseList());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.showDialogCaveatMessage(view.getResourceString(R.string.add_device_error));

                    }

                    @Override
                    public void onComplete() {

                    }
                })
        );
    }

    @Override
    public void onGetTYPEData(String CO, String PMFCT) {
        final TYPERequest mTypeRequest = new TYPERequest(KEY_SEARCH_TYPE,USER_ID,CO, PMFCT);
        String url = view.getResourceString(R.string.api_on_getTYPE);
        compositeDisposable.add(api.getTYPE(url,mTypeRequest)
                .subscribeOn(schedulerProviderImp.io())
                .observeOn(schedulerProviderImp.ui())
                .subscribeWith(new DisposableObserver<TYPEResultList>() {
                    @Override
                    public void onNext(TYPEResultList mTYPEResultList) {
                        if (mTYPEResultList.getmTYPEResponseList().size() < 1) {
                            view.showDialogCaveatMessage(view.getResourceString(R.string.get_type_error_no_data));
                        }else{
                            view.setTYPEData(mTYPEResultList.getmTYPEResponseList());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.showDialogCaveatMessage(view.getResourceString(R.string.add_device_error));
                    }

                    @Override
                    public void onComplete() {

                    }
                })
        );
    }

    @Override
    public void segVideo(String path,String name) {
        File video = new File(path);
        int sizeLimit = 50 * 1024 * 1024;
        Log.e("size", video.length() + "");
//                        Log.e("path2", );
        int duration = (int) (FFprobe.getMediaInformation(path).getDuration() / 1000); //影片秒數
        int startTime = 0;
        int segTime = 30;   //分割秒數
        int loop = 0;
        if (duration % segTime != 0) {
            loop = duration / segTime + 1;
        } else {
            loop = duration / segTime;
        }
        if (video.length() > sizeLimit) {
            for (int i = 0; i < loop; i++) {
                int rc = FFmpeg.execute("-ss " + startTime + " -i " + path + " -t " + segTime + " " + Environment.getExternalStorageDirectory().toString() +  "/seg_" +i +name + ".mp4");

                if (rc == RETURN_CODE_SUCCESS) {
                    Log.i(Config.TAG, "Command execution completed successfully.");
                } else if (rc == RETURN_CODE_CANCEL) {
                    Log.i(Config.TAG, "Command execution cancelled by user.");
                } else {
                    Log.i(Config.TAG, String.format("Command execution failed with rc=%d and the output below.", rc));
                    Config.printLastCommandOutput(Log.INFO);
                }
                startTime += segTime;
            }

        } else {
            //沒超過
        }
    }




//    @Override
//    public void onGetDisposableToken(String DeviceId) {
//        String authorizedId ="fec40e7e-48c2-4226-81ca-5044b72a8e1f";
//        String url = getView().getResourceString(R.string.api_on_DisposableToken);
//        DisposableTokenRequest mDisposableTokenRequest=new DisposableTokenRequest(authorizedId,mLoginPreferencesProvider.getToken(),DeviceId);
//        getCompositeDisposable().add(getApiService().onDisposableToken(url, mDisposableTokenRequest)
//                .subscribeOn(getSchedulerProvider().io())
//                .observeOn(getSchedulerProvider().ui())
//                .subscribeWith(new DisposableObserver<DisposableTokenResponse>() {
//                    @Override
//                    public void onNext(DisposableTokenResponse disposableTokenResponse) {
//                        Log.e("wwwww","getmResult:"+disposableTokenResponse.getmResult());
//                        Log.e("wwwww","getmErrMsg:"+disposableTokenResponse.getmErrMsg());
//                        Log.e("wwwww","getmDisposableToken:"+disposableTokenResponse.getmDisposableToken());
//
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//
//                    }
//
//                    @Override
//                    public void onComplete() {
//
//                    }
//                })
//        );
//    }

}
