package com.fxy.main.fragment;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.fxy.common.VersionUpgrades;
import com.elvishew.xlog.XLog;
import com.fxy.putIn.CheckGoodsMainActivity;
import com.fxy.putIn.PutinPalletMainActivity;
import com.fxy.putIn.PutinPhotosMainActivity;
import com.fxy.putIn.SortationMainActivity;
import com.fxy.putIn.TallyMainActivity;
import com.fxy.putIn.TrayMainActivity;
import com.fxy.putOut.DeliveryGoodsMainActivity;
import com.fxy.putOut.QualityInspectionMainActivity;
import com.fxy.tool.OrderToolMainActivity;
import com.fxy.workOrder.StartWorkOrderMainActivity;
import com.fxy.workOrder.WorkOrderOptMainActivity;
import com.google.zxing.Result;
import com.fxy.baselibrary.base.BaseFragment;
import com.fxy.baselibrary.interfaces.OnRxScanerListener;
import com.fxy.baselibrary.language.other.CommSharedUtil;
import com.fxy.baselibrary.qrcode.ActivityScanerCode;
import com.fxy.baselibrary.tddialog.TDialog;
import com.fxy.baselibrary.tddialog.base.BindViewHolder;
import com.fxy.baselibrary.tddialog.base.TBaseAdapter;
import com.fxy.baselibrary.tddialog.list.TListDialog;
import com.fxy.R;
import com.fxy.constant.BaseConfig;
import com.fxy.helper.ScanHelper;


import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * 仓配业务主界面
 *
 * @author James.Jiang
 */
public class HomePagerFragment extends BaseFragment implements View.OnClickListener {

    /**
     * 搜索框
     */
    @BindView(R.id.search_view)
    LinearLayout searchView;



    Unbinder bind;
    public static final String TYPE = "type";

    @Nullable
    public static HomePagerFragment newInstance(@Nullable String steRes) {
        Bundle bundle = new Bundle();
        bundle.putString(TYPE, steRes);
        HomePagerFragment fragment = new HomePagerFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_homepager;
    }

    @Override
    protected void initView(View rootView) {
        bind = ButterKnife.bind(this, rootView);
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }

        //从缓存读取客户端

        try {
            //检查文件读写 权限
            if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(getActivity(), new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            }
        }catch (Exception e){
            XLog.e("获取读写权限出错："+e.getMessage());
        }


        //初始化扫码设置
        new ScanHelper(getActivity());
        //记录一下设备信息
        XLog.e("记录设备信息：\n厂商："+ Build.MANUFACTURER+"\n型号："+Build.MODEL+"\nandroid："+Build.VERSION.RELEASE+"");
        new Thread() {
            @Override
            public void run() {
                (new VersionUpgrades(getContext())).checkUpgrades();
            }
        }.start();

    }

    @Override
    protected void getBundleExtras(Bundle bundle) {

    }

    @Override
    protected void initLogic() {

    }


    /**
     * 绑定事件
     * @param v
     */
    @OnClick({R.id.search_view, R.id.ivt_tally,R.id.ivt_cargo_storage,R.id.ivt_putin_photos,
            R.id.ivt_order_tool,R.id.ivt_check,R.id.ivt_putin_pallet,
            R.id.ivt_handle_work_order,R.id.ivt_add_work_order})
    public void onClick(View v) {
        Bundle bundle = new Bundle();
        switch (v.getId()) {
            case R.id.ivt_tally:
                bundle.putString("action","receive");
                intent2Page(getActivity(), TallyMainActivity.class, false,bundle);;
                break;
            case R.id.ivt_cargo_storage:
                bundle.putString("action","storage");
                intent2Page(getActivity(), DeliveryGoodsMainActivity.class, false,bundle);;
                break;
            case R.id.ivt_order_tool:
                intent2Page(getActivity(), OrderToolMainActivity.class, false,bundle);;
                break;
            case R.id.ivt_check:
                intent2Page(getActivity(), CheckGoodsMainActivity.class, false,bundle);;
                break;
            case R.id.ivt_handle_work_order:
                intent2Page(getActivity(), StartWorkOrderMainActivity.class, false,bundle);
                break;
            case R.id.ivt_add_work_order:
                bundle.putString("action","add");
                intent2Page(getActivity(), WorkOrderOptMainActivity.class, false,bundle);
                break;
            case R.id.ivt_putin_photos:
                intent2Page(getActivity(), PutinPhotosMainActivity.class, false,bundle);
                break;
            case R.id.ivt_putin_pallet:
                intent2Page(getActivity(), PutinPalletMainActivity.class, false,bundle);
                break;


            default:
                showToast("还没有配置点击事件");
                break;


        }
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }


}
