package com.sensetime.demo.ui;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;


import com.sensetime.demo.SenseTimeApp;
import com.sensetime.demo.util.MaterialDialogUtils;
import com.sensetime.mtc.MtcCode;

import java.lang.ref.WeakReference;

import kr.co.icontrols.ilp300_700.R;
import kr.co.icontrols.wallpadsupport.WpadActivity;


/**
 * Created by LaoTie on 2020/4/25.
 */

public abstract class BaseActivity extends AppCompatActivity {
    private String toast_permission;
    private OnPermissionHaveListener onPermissionHaveListener;


    private boolean isOnStop = false;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("jglee", "jglee - onCreate");
//        MClient.getInstance().setConnectCallback(mConnectCallback);

        //initView();

        //addActivity();
        //initData();
    }

    private void addActivity() {
        SenseTimeApp senseTimeApp = (SenseTimeApp) getApplication();
        senseTimeApp.pushTask(new WeakReference<>(this));
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        Log.d("jglee", "jglee - onResume");
        super.onResume();
        isOnStop = false;
    }

    @Override
    protected void onStop() {
        Log.d("jglee", "jglee - onStop");
        super.onStop();
        isOnStop = true;
    }

    private Context mContext;

    public Context getContext() {
        return this.mContext;
    }

    protected boolean setChangeScreen(int nScreenId) {
        return ((WpadActivity)((WpadActivity)this.getContext())).ChangeScreen(nScreenId);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    protected abstract void initView();

    public void showToast(String msg) {
        if (isFinishing()) {
            return;
        }
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    }

    public void showToast(String msg, int rotate) {
        if (isFinishing()) {
            return;
        }
        Toast toast = Toast.makeText(getApplicationContext(), "This is toast!", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);

        //toast.setMargin(1000,1000);
        ImageView imageView=new ImageView(getApplicationContext());
        imageView.setImageResource(R.mipmap.popup_success);
        toast.setView(imageView);
        //위치 지정
        //toast.setGravity(Gravity.CENTER,50,50);

        //View toastView = toast.getView();
        //toastView.setRotation(rotate);


        toast.show();
    }

    public void showToast_fire() {
        if (isFinishing()) {
            return;
        }
        Toast toast = Toast.makeText(getApplicationContext(), "This is toast!", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);

        //toast.setMargin(1000,1000);
        ImageView imageView=new ImageView(getApplicationContext());
        imageView.setImageResource(R.drawable.caution_emergency);
        toast.setView(imageView);
        //위치 지정
        //toast.setGravity(Gravity.CENTER,50,50);

        //View toastView = toast.getView();
        //toastView.setRotation(rotate);


        toast.show();
    }

    public void showToast_rotate(String msg) {
        if (isFinishing()) {
            return;
        }
        Toast toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);

        //toast.setMargin(1000,1000);

        //위치 지정
        //toast.setGravity(Gravity.CENTER,50,50);

        View toastView = toast.getView();
        toastView.setRotation(270);


        toast.show();
        //Toast.makeText(getApplicationContext(), getResources().getString(string_id), Toast.LENGTH_SHORT).show();
    }

    public void showToast(int string_id) {
        if (isFinishing()) {
            return;
        }
        Toast.makeText(getApplicationContext(), getResources().getString(string_id), Toast.LENGTH_SHORT).show();
    }

    public void showBasicDialogNoCancel(String title, String content) {
        if (isFinishing()) {
            return;
        }
        MaterialDialogUtils.showBasicDialogNoCancel(this, title, content).show();

    }

    public void showBasicDialogNoCancel(int titleId, String content) {
        if (isFinishing()) {
            return;
        }
        MaterialDialogUtils.showBasicDialogNoCancel(this, getString(titleId), content).show();

    }

    protected abstract void initData();

    public String getMtcErrorMsg(MtcCode mtcCode) {
        if (isFinishing()) {
            return "";
        }
        Context context = getApplicationContext();
        String errorMsg = context.getString(R.string.notice_failure);
        switch (mtcCode) {
            case SUCCESS:
                errorMsg = context.getString(R.string.notice_success);
                break;
            case ERR_NOT_INIT:
                errorMsg = context.getString(R.string.notice_init_failure);
                break;
            case ERR_ALREADY_INIT:
                errorMsg = context.getString(R.string.repeat_init);
                break;
            case ERR_CANNOT_OPEN:
                errorMsg = context.getString(R.string.cannot_open_serial);
                break;
            case ERR_CANNOT_FCNTL:
                errorMsg = context.getString(R.string.failed_setting_block);
                break;
            case ERR_CANNOT_SET_ATTR:
                errorMsg = context.getString(R.string.serial_port_setup_failed);
                break;
            case ERR_INVALID_FD:
                errorMsg = context.getString(R.string.fd_is_invalid);
                break;
            case ERR_WRITE_RETRY_FAILED:
                errorMsg = context.getString(R.string.multiple_write_to_serial_port_failures);
                break;
            case ERR_INVALID_DATA:
                errorMsg = context.getString(R.string.invalid_request_data);
                break;
            case ERR_INVALID_LENGTH:
                errorMsg = context.getString(R.string.invalid_request_data);
                break;
            case ERR_INVALID_ALLOC_MEM:
                errorMsg = context.getString(R.string.malloc_failure);
                break;
            case ERR_INVALID_PROTOCOL:
                errorMsg = context.getString(R.string.invalidate_protocol);
                break;
            case ERR_RECEIVER_INVALID_STAT:
                errorMsg = context.getString(R.string.wrong_receiving_state);
                break;
            case ERR_TIME_OUT:
                errorMsg = context.getString(R.string.time_out);
                break;
            case ERR_PROCESS_FAILED:
                errorMsg = context.getString(R.string.notice_failure);
                break;
            case ERR_NO_IMAGE:
                errorMsg = context.getString(R.string.no_the_pictures_in_the_android_devices);
                break;

            case ERR_NO_IMAGE_ID:
                errorMsg = context.getString(R.string.define_failure_reason_id_empty);
                break;
            case ERR_IMAGE_ID_LENGTH_TOO_LONG:
                errorMsg = context.getString(R.string.setting_notice_del_user_failure_reason_too_lonng_id);
                break;
            case ERR_IMAGE_IS_EMPTY:
                errorMsg = context.getString(R.string.no_the_pictures_in_the_android_devices);
                break;
            case ERR_IMAGE_TOO_BIG:
                errorMsg = context.getString(R.string.image_too_big);
                break;
            case ERR_IMAGE_ID_IRREGULAR:
                errorMsg = context.getString(R.string.reo_error_code_15);
                break;


            case AI_ERROR_CODE_1:
                errorMsg = context.getString(R.string.reo_error_code_1);
                break;
            case AI_ERROR_CODE_2:
                errorMsg = context.getString(R.string.reo_error_code_2);
                break;
            case AI_ERROR_CODE_3:
                errorMsg = context.getString(R.string.reo_error_code_3);
                break;
            case AI_ERROR_CODE_4:
                errorMsg = context.getString(R.string.reo_error_code_4);
                break;
            case AI_ERROR_CODE_5:
                errorMsg = context.getString(R.string.reo_error_code_5);
                break;
            case AI_ERROR_CODE_6:
                errorMsg = context.getString(R.string.reo_error_code_6);
                break;
            case AI_ERROR_CODE_7:
                errorMsg = context.getString(R.string.reo_error_code_7);
                break;
            case AI_ERROR_CODE_8:
                errorMsg = context.getString(R.string.reo_error_code_8);
                break;
            case AI_ERROR_CODE_9:
                errorMsg = context.getString(R.string.reo_error_code_9);
                break;
            case AI_ERROR_CODE_10:
                errorMsg = context.getString(R.string.reo_error_code_10);
                break;
            case AI_ERROR_CODE_11:
                errorMsg = context.getString(R.string.reo_error_code_11);
                break;
            case AI_ERROR_CODE_12:
                errorMsg = context.getString(R.string.reo_error_code_12);
                break;
            case AI_ERROR_CODE_13:
                errorMsg = context.getString(R.string.reo_error_code_13);
                break;

            case AI_ERROR_CODE_15:
                errorMsg = context.getString(R.string.reo_error_code_15);
                break;
            case AI_ERROR_CODE_16:
                errorMsg = context.getString(R.string.reo_error_code_16);
                break;
            case AI_ERROR_CODE_17:
                errorMsg = context.getString(R.string.reo_error_code_17);
                break;
            case AI_ERROR_CODE_18:
                errorMsg = context.getString(R.string.reo_error_code_18);
                break;
            case AI_ERROR_CODE_19:
                errorMsg = context.getString(R.string.reo_error_code_19);
                break;
            case AI_ERROR_CODE_20:
                errorMsg = context.getString(R.string.reo_error_code_20);
                break;

            case ENGINE_ERR_CODE_160:
                errorMsg = context.getString(R.string.AI_service_has_not_completed_initialization);
                break;

            case PARAM_ERROR:
                errorMsg = getString(R.string.param_error);
                break;
            case UNKNOWN_ERROR:
                errorMsg = context.getString(R.string.unknow_error);
                break;
        }
        return errorMsg;
    }

    public boolean permissionGranted(String permissions[], int requestCode) {
        for (String pm : permissions) {
            Log.d("jglee", "jglee --------------------- Usb permissionGranted");
            int permissionCode = ContextCompat.checkSelfPermission(this, pm);
            if (permissionCode != PackageManager.PERMISSION_GRANTED) {
                try {
                    ActivityCompat.requestPermissions(this, new String[]{pm}, requestCode);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return false;
            }
        }
        return true;
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        for (int i = 0; i < permissions.length; i++) {
            if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {

                showToast(toast_permission);
                return;
            }
        }
        if (onPermissionHaveListener != null) {
            onPermissionHaveListener.onPermissionHave();
        }


    }

    public interface OnPermissionHaveListener {
        public void onPermissionHave();
    }
}
