package com.ez.ertc.meeting.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.Switch;

import com.ez.ertc.meeting.bean.ErtcBeautyFilterEnum;
import com.ez.ertc.meeting.ErtcEzmHelper;
import com.ez.ertc.meeting.R;
import com.ez.ertc.meeting.R2;
import com.ez.ertc.meeting.base.YsBaseActivity;
import com.ez.ertc.meeting.base.YsCallback;
import com.ez.ertc.meeting.bean.FaceBeauty;
import com.ez.ertc.meeting.constant.MtSets;
import com.ez.ertc.meeting.contract.BeautyConfigContract;
import com.ez.ertc.meeting.presenter.BeautyConfigPresenter;
import com.ez.ertc.meeting.widget.ErtcTextureView;
import com.ez.ertcengine.ERTCEngine;

import org.jetbrains.annotations.NotNull;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class BeautyConfigActivity extends YsBaseActivity implements BeautyConfigContract.View {

    @BindView(R2.id.vcsTextureView)
    ErtcTextureView vcsTextureView;
    @BindView(R2.id.viewBeautyConfig)
    View viewBeautyConfig;
    @BindView(R2.id.flContrast)
    FrameLayout flContrast;
    @BindView(R2.id.flBeautyConfig)
    View flBeautyConfig;
    @BindView(R2.id.swBeauty)
    Switch swBeauty;
    private static String KEY_BEAUTY_CONFIG = "KEY_BEAUTY_CONFIG";
    private int REQUEST_CODE_VIDEO = 400;
    private BeautyConfigContract.Presenter mPresenter;
    private BeautyConfigHolder beautyConfigHolder;

    public static void start(Context context) {
        Intent starter = new Intent(context, BeautyConfigActivity.class);
        context.startActivity(starter);
    }

    public static void startBeautyConfig(Context context) {
        Intent starter = new Intent(context, BeautyConfigActivity.class);
        starter.putExtra(KEY_BEAUTY_CONFIG,true);
        context.startActivity(starter);
    }

    @Override
    protected int provideLayoutId() {
        return R.layout.activity_beauty_config;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        flContrast.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        if(ErtcEzmHelper.ertcEngine != null) {
                            ErtcEzmHelper.ertcEngine.setBeautyEnable(false);
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        if(ErtcEzmHelper.ertcEngine != null) {
                            ErtcEzmHelper.ertcEngine.setBeautyEnable(true);
                        }
                        break;
                }
                return true;
            }
        });

    }

    @Override
    protected void initData() {
        new BeautyConfigPresenter(this);
        beautyConfigHolder = new BeautyConfigHolder(flBeautyConfig,mPresenter);
        beautyConfigHolder.onShow();
        swBeauty.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                viewBeautyConfig.setVisibility(isChecked ? View.VISIBLE : View.INVISIBLE);
                flContrast.setVisibility(isChecked ? View.VISIBLE : View.GONE);
                MtSets.setBeautyEnable(isChecked);
                if(ErtcEzmHelper.ertcEngine != null) {
                    ErtcEzmHelper.ertcEngine.setBeautyEnable(isChecked);
                }
            }
        });

        ErtcEzmHelper.getInstance().ertcEnterInitDebug(this)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new YsCallback<ERTCEngine>() {
                    @Override
                    public void onNext(@NotNull ERTCEngine engine) {
                        //打开摄像头
//                        ErtcEzmHelper.ertcEngine.enableLocalVideo(true);
                        //设置窗口
                        ErtcEzmHelper.ertcEngine.setLocalView(vcsTextureView.getVideoCanvasView());
                        vcsTextureView.isShowLoadingProgress = false;
                        vcsTextureView.hideProgress();
                        //设置美颜开关
                        swBeauty.setChecked(MtSets.isEnableBeauty());
                        viewBeautyConfig.setVisibility(swBeauty.isChecked() ? View.VISIBLE : View.INVISIBLE);
                        flContrast.setVisibility(swBeauty.isChecked() ? View.VISIBLE : View.GONE);
                    }
                });
    }


    @OnClick({R2.id.flNavBack,
            R2.id.ivSwitchCamera})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.flNavBack){
            onBackPressed();
        } else if (id == R.id.ivSwitchCamera){
            if (ErtcEzmHelper.ertcEngine != null) {
                ErtcEzmHelper.ertcEngine.switchCamera();
            }
        }
    }

    @Override
    public void updateBeautyKit(FaceBeauty faceBeauty) {
        if(ErtcEzmHelper.ertcEngine != null) {
            ErtcEzmHelper.ertcEngine.setBeautyConfig(faceBeauty.whitenessIntensity, faceBeauty.smoothnessIntensity, faceBeauty.ruddyIntensity);
        }
    }

    @Override
    public void updateBeautyFilter(ErtcBeautyFilterEnum filterType, float filterIntensity) {
        if(ErtcEzmHelper.ertcEngine != null) {
            ErtcEzmHelper.ertcEngine.setBeautyFilter(filterType.getValue(), filterIntensity);
        }
    }

    @Override
    public void setPresenter(BeautyConfigContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public Activity getActivity() {
        return this;
    }
}