package com.ifavine.pay.ui.base;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hitomi.tilibrary.transfer.TransferConfig;
import com.hitomi.tilibrary.transfer.Transferee;
import com.ifavine.pay.R;
import com.ifavine.pay.common.AppContext;
import com.ifavine.pay.interf.BaseViewInterface;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.yokeyword.swipebackfragment.SwipeBackActivity;


/**
 * Created by tree on 7/4/2016.
 */
//public class BaseActivity extends AppCompatActivity implements BaseViewInterface {
public class BaseActivity extends SwipeBackActivity implements BaseViewInterface {


    public final int TOOL_BAR_TYPE_NONE = -1;
    public final int TOOL_BAR_TYPE_GENERAL = 0;
    public final int TOOL_BAR_TYPE_SAVE = 1;
    public final int TOOL_BAR_TYPE_EDIT = 2;
    public final int TOOL_BAR_TYPE_SEARCH_LABEL = 4;
    public final int TOOL_BAR_TYPE_TITLE = 5;
    public final int TOOL_BAR_TYPE_DELETE = 6;
    public final int TOOL_BAR_TYPE_SAVE_CANCEL = 7;
    public final int TOOL_BAR_TYPE_SEARCH = 8;
    public final int TOOL_BAR_TYPE_NO_SAVE = 10;
    public final int TOOL_BAR_TYPE_ADD = 11;
    public final int TOOL_BAR_TYPE_CONFIRM = 12;
    public final int TOOL_BAR_TYPE_SEARCH_ADD = 13;
    public final int TOOL_BAR_TYPE_NO_SAVE_LEFTTITTLE = 14;


    @BindView(R.id.toolbar)
    protected Toolbar mToolbar;
    @BindView(R.id.toolbar_title)
    protected AppCompatTextView mTitle;
    @BindView(R.id.toolbar_left_title)
    protected AppCompatTextView mLeftTitle;
    @BindView(R.id.toolbar_right_title)
    protected AppCompatTextView mRightTitle;
    @BindView(R.id.toolbar_left_icon)
    protected AppCompatImageView mLeftIcon;
    @BindView(R.id.toolbar_right_icon)
    protected AppCompatImageView mRightIcon;
    @BindView(R.id.ll_search_box)
    protected LinearLayoutCompat mSearchBox;
    @BindView(R.id.et_search_box)
    protected AppCompatEditText mSreachBoxEditText;
    @BindView(R.id.iv_search_model)
    protected AppCompatImageView ivSearchModel;
    @BindView(R.id.tv_search_box)
    protected AppCompatTextView tvSearchBox;

    protected LayoutInflater mInflater;
    protected Context mContext;
    protected static Dialog loadingDialog;
    protected Transferee transferee;
    protected TransferConfig config;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AppContext.addActivity(this);
        if (getLayoutId() != 0) {
            setContentView(getLayoutId());
        }
        mContext = this;
        mInflater = getLayoutInflater();
        ButterKnife.bind(this);
        initView();
        initData();
        initListener();
    }

    public void setToolbar(int toolbarTypeCode, int titleResId) {
        switch (toolbarTypeCode) {
            case TOOL_BAR_TYPE_NONE:
                mToolbar.setVisibility(View.GONE);
                break;
            case TOOL_BAR_TYPE_GENERAL:
                setCenterTitle(titleResId);
                mLeftIcon.setVisibility(View.VISIBLE);
                mSreachBoxEditText.setVisibility(View.INVISIBLE);
                break;
            case TOOL_BAR_TYPE_SAVE:
                setCenterTitle(titleResId);
                mRightTitle.setVisibility(View.VISIBLE);
                mRightTitle.setText(getString(R.string.toolbar_save));
                mLeftTitle.setVisibility(View.INVISIBLE);
                mRightTitle.setVisibility(View.VISIBLE);
                mLeftIcon.setVisibility(View.VISIBLE);
                mSreachBoxEditText.setVisibility(View.INVISIBLE);
                break;
            case TOOL_BAR_TYPE_CONFIRM:
                setCenterTitle(titleResId);
                mRightTitle.setVisibility(View.VISIBLE);
                mRightTitle.setText(getString(R.string.toolbar_confirm));
                mRightTitle.setVisibility(View.VISIBLE);
                mLeftIcon.setVisibility(View.VISIBLE);
                mSreachBoxEditText.setVisibility(View.INVISIBLE);
                break;
            case TOOL_BAR_TYPE_ADD:
                setCenterTitle(titleResId);
                mRightTitle.setVisibility(View.VISIBLE);
                mRightTitle.setVisibility(View.GONE);
                mLeftIcon.setVisibility(View.VISIBLE);
                mRightIcon.setImageResource(R.mipmap.ic_launcher);
                mRightIcon.setVisibility(View.VISIBLE);
                mSreachBoxEditText.setVisibility(View.INVISIBLE);
                break;
            case TOOL_BAR_TYPE_EDIT:
                mTitle.setVisibility(View.VISIBLE);
                setCenterTitle(titleResId);
                mRightTitle.setText(getString(R.string.toolbar_edit));
                mRightTitle.setVisibility(View.VISIBLE);
                mSreachBoxEditText.setVisibility(View.INVISIBLE);
                break;
            case TOOL_BAR_TYPE_SEARCH_LABEL:
                mSearchBox.setVisibility(View.VISIBLE);
                ivSearchModel.setVisibility(View.VISIBLE);
                tvSearchBox.setVisibility(View.VISIBLE);
                mSreachBoxEditText.setVisibility(View.INVISIBLE);
                break;
            case TOOL_BAR_TYPE_TITLE:
                setCenterTitle(titleResId);
                mLeftIcon.setVisibility(View.GONE);
                mSreachBoxEditText.setVisibility(View.INVISIBLE);
                break;
            case TOOL_BAR_TYPE_DELETE:
                mLeftIcon.setVisibility(View.GONE);
                mTitle.setVisibility(View.GONE);
                mLeftTitle.setText(getString(R.string.toolbar_cancel));
                mRightTitle.setText(getString(R.string.toolbar_delete));
                mSreachBoxEditText.setVisibility(View.INVISIBLE);
                break;
            case TOOL_BAR_TYPE_SAVE_CANCEL:
                setCenterTitle(titleResId);
                mLeftIcon.setVisibility(View.GONE);
                mLeftTitle.setVisibility(View.VISIBLE);
                mLeftTitle.setText(R.string.toolbar_cancel);
                mRightTitle.setVisibility(View.VISIBLE);
                mRightTitle.setText(getString(R.string.toolbar_save));
                mSreachBoxEditText.setVisibility(View.INVISIBLE);
                break;
            case TOOL_BAR_TYPE_SEARCH:
                mSearchBox.setVisibility(View.VISIBLE);
                mSreachBoxEditText.setVisibility(View.VISIBLE);
                mLeftTitle.setVisibility(View.VISIBLE);
//                mRightTitle.setVisibility(View.GONE);
                mRightTitle.setVisibility(View.VISIBLE);
                mRightTitle.setText(R.string.toolbar_cancel);
                mSreachBoxEditText.setVisibility(View.VISIBLE);
                mLeftIcon.setVisibility(View.VISIBLE);
                ivSearchModel.setVisibility(View.GONE);
                mRightIcon.setVisibility(View.VISIBLE);
                mRightIcon.setImageResource(R.mipmap.ic_launcher);
                break;
            case TOOL_BAR_TYPE_SEARCH_ADD:
                mSearchBox.setVisibility(View.VISIBLE);
                mSreachBoxEditText.setVisibility(View.VISIBLE);
                mLeftTitle.setVisibility(View.VISIBLE);
                mRightTitle.setVisibility(View.GONE);
                mSreachBoxEditText.setVisibility(View.VISIBLE);
                mLeftIcon.setVisibility(View.VISIBLE);
                ivSearchModel.setVisibility(View.GONE);
                mRightIcon.setVisibility(View.VISIBLE);
                mRightIcon.setImageResource(R.mipmap.ic_launcher);
                break;
            case TOOL_BAR_TYPE_NO_SAVE:
                setCenterTitle(titleResId);
                mRightTitle.setVisibility(View.GONE);
                mSreachBoxEditText.setVisibility(View.INVISIBLE);
                break;
            case TOOL_BAR_TYPE_NO_SAVE_LEFTTITTLE:
                setCenterTitle(titleResId);
                mRightTitle.setVisibility(View.GONE);
                mSreachBoxEditText.setVisibility(View.INVISIBLE);
                // mLeftTitle.setVisibility(View.VISIBLE);
                // mLeftTitle.setText(getString(R.string.toolbar_return));
                break;
        }
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    };

    private void setCenterTitle(int titleResId) {
        if (titleResId > 0 && mTitle != null) {
            String title = getText(titleResId).toString().trim();
            mTitle.setText(title);
        }
    }


    public BaseActivity() {
        super();
    }


    @Override
    public void initView() {

    }


    @Override
    public void initData() {
        mLeftIcon.setOnClickListener(listener);
    }


    /**
     * 打开一个Activity 默认 不关闭当前activity
     */
    public void gotoActivity(Class<?> clz) {
        gotoActivity(clz, false, null);
    }

    public void gotoActivity(Class<?> clz, boolean isCloseCurrentActivity) {
        gotoActivity(clz, isCloseCurrentActivity, null);
    }

    public void gotoActivity(Class<?> clz, boolean isCloseCurrentActivity, Bundle ex) {
        Intent intent = new Intent(this, clz);
        if (ex != null) intent.putExtras(ex);
        startActivity(intent);
        if (isCloseCurrentActivity) {
            finish();
        }
    }

    public void gotoActivity(Intent intent, boolean isCloseCurrentActivity) {
        startActivity(intent);
        if (isCloseCurrentActivity) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppContext.removeActivity(this);

    }

    protected int getLayoutId() {
        return 0;
    }

    public void initListener() {

    }


    /**
     * 得到自定义的progressDialog
     *
     * @param context
     * @param msg
     * @return
     */
    public static Dialog showLoadingDialog(Context context, boolean cancelable, String msg) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.view_progressbar, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
        // main.xml中的ImageView

        TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
        tipTextView.setText(msg);// 设置加载信息

        loadingDialog = new Dialog(context, R.style.dialog_loading_dialog);// 创建自定义样式dialog

        loadingDialog.setCancelable(cancelable);
        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT));// 设置布局
        loadingDialog.show();
        return loadingDialog;
    }

    protected static void disMissDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

}
