package com.zhilin.patrol.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhilin.patrol.R;
import com.zhilin.patrol.bean.LoginEntity;

import java.io.IOException;
import java.util.ArrayList;

/**
 * 所有activity的基类
 * Created by hanli on 2018/5/30.
 */
public class BaseActivity extends AppCompatActivity {

    /** 是否包含标题 */
    private boolean isContainNavi = true;
    /** 主页面 */
    private FrameLayout mContentView;
    /** 加载页面 */
    private RelativeLayout mLoadingView;
    /** 失败页面 */
    private RelativeLayout mFailView;
    /** 无数据页面 */
    private RelativeLayout mEmptyView;
    /** 标题 */
    private TextView mTitleTv;
    /** 右边按钮 */
    private TextView mRightTv;
    /** 左边返回 */
    private ImageView mBackIv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去标题
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(R.layout.activity_base);
        if (isContainNavi) {
            findViewById(R.id.rl_header).setVisibility(View.VISIBLE);
        }
        mContentView = findViewById(R.id.fl_content);
        mLoadingView = findViewById(R.id.rl_loading);
        mFailView = findViewById(R.id.rl_fail);
        mEmptyView = findViewById(R.id.rl_empty);
        mTitleTv = findViewById(R.id.tv_title);
        mRightTv = findViewById(R.id.tv_right);
        mBackIv = findViewById(R.id.iv_back);
        View.inflate(this, layoutResID, mContentView);
        mFailView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showLoadingView();
                request();
            }
        });
        mBackIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    /** 设置右边点击 */
    public void setRightClick(View.OnClickListener clickListener) {
        mRightTv.setOnClickListener(clickListener);
    }

    /**
     * 是否显示导航栏
     *
     * @param isContainNavi
     */
    public void isContainNavi(boolean isContainNavi) {
        this.isContainNavi = isContainNavi;
    }

    /**
     * 显示加载页面
     */
    public void showLoadingView() {
        mContentView.setVisibility(View.GONE);
        mLoadingView.setVisibility(View.VISIBLE);
        mFailView.setVisibility(View.GONE);
        mEmptyView.setVisibility(View.GONE);
    }

    /**
     * 显示内容
     */
    public void showContentView() {
        mContentView.setVisibility(View.VISIBLE);
        if (mLoadingView != null) {
            ((ViewGroup) mLoadingView.getParent()).removeView(mLoadingView);
            mLoadingView = null;
        }
        if (mFailView != null) {
            ((ViewGroup) mFailView.getParent()).removeView(mFailView);
            mFailView = null;
        }
    }

    /**
     * 显示无数据界面
     */
    public void showEmptyView() {
        mContentView.setVisibility(View.GONE);
        mEmptyView.setVisibility(View.VISIBLE);
    }

    /**
     * 显示请求失败界面
     */
    public void showFailView() {
        mContentView.setVisibility(View.GONE);
        mLoadingView.setVisibility(View.GONE);
        mEmptyView.setVisibility(View.GONE);
        mFailView.setVisibility(View.VISIBLE);
//        if (!TextUtils.isEmpty(errMsg)) {
//            mFailView.setText(errMsg);
//        }
//        if (resId != null) {
//            mErrorIv.setImageResource(resId);
//        }
    }

    public void setTitle(String headTitle) {
        mTitleTv.setText(headTitle);
    }

    public void setRightTv(String text) {
        if (TextUtils.isEmpty(text)) return;
        mRightTv.setVisibility(View.VISIBLE);
        mRightTv.setText(text);
    }

    /**
     * 请求方法
     */
    public void request(){

    }

    /**
     * 跳转页面
     *
     * @param cls 目的页面
     */
    public void toActivity(Class cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }

    /**
     * 跳转页面
     *
     * @param cls 目的页面
     */
    public void toActivity(Class cls, LoginEntity.InfoListBean.UserInfoBean userInfo,String mpCode,String maddress,int type){
        Intent intent = new Intent(this, cls);
        intent.putExtra("userInfo", userInfo);
        intent.putExtra("pCode",mpCode);
//        intent.putExtra("isAssign", 2);
        intent.putExtra("pbCode","null");
        intent.putExtra("address",maddress);
        startActivity(intent);
    }
    public void toActivityReUpload(Class cls, LoginEntity.InfoListBean.UserInfoBean userInfo,String planCode,String pbCode,String maddress){
        Intent intent = new Intent(this, cls);
        intent.putExtra("userInfo", userInfo);
        intent.putExtra("pCode",planCode);
        intent.putExtra("pbCode",pbCode);
        intent.putExtra("address",maddress);
        startActivity(intent);
    }
    public void toActivityRecify(Class cls, LoginEntity.InfoListBean.UserInfoBean userInfo, String mpCode, String maddress, String text, int mType, ArrayList imgList){
        Intent intent = new Intent(this, cls);
        intent.putExtra("userInfo", userInfo);
        intent.putExtra("pCode",mpCode);
        intent.putExtra("address",maddress);
        intent.putExtra("text",text);
        intent.putExtra("mType",mType);
        intent.putExtra("imgList",imgList);
        startActivity(intent);
    }

    /**
     * 跳转页面
     *
     * @param cls 目的页面
     */
    public void toActivityNum(Class cls, LoginEntity.InfoListBean.UserInfoBean userInfo,int num) {
        Intent intent = new Intent(this, cls);
        intent.putExtra("userInfo", userInfo);
        intent.putExtra("num",num);
        startActivity(intent);
    }

}
