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.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.fxy.common.VersionUpgrades;
import com.elvishew.xlog.XLog;
import com.fxy.constant.SPCache;
import com.fxy.detection.DetectionConfirmGoodsMainActivity;
import com.fxy.detection.DetectionDestroyOutboundMainActivity;
import com.fxy.detection.DetectionPutInMainActivity;
import com.fxy.detection.DetectionPutawayMainActivity;
import com.fxy.detection.DetectionWorkMainActivity;
import com.fxy.helper.HkDataHelper;
import com.fxy.hk.HkOutboundMainActivity;
import com.fxy.hk.HkPickupStartMainActivity;
import com.fxy.hk.HkPutinMainActivity;
import com.fxy.hk.HkTrayLocationMainActivity;
import com.fxy.hk.HkTrayMainActivity;
import com.fxy.putIn.CheckUnpackingMainActivity;
import com.fxy.putIn.PutinPalletMainActivity;
import com.fxy.putIn.PutinPhotosMainActivity;
import com.fxy.putIn.TallyMainActivity;
import com.fxy.putOut.DeliveryGoodsMainActivity;
import com.fxy.tool.OrderToolMainActivity;
import com.fxy.workOrder.StartWorkOrderMainActivity;
import com.fxy.workOrder.WorkOrderOptMainActivity;
import com.fxy.baselibrary.base.BaseFragment;
import com.fxy.R;
import com.fxy.helper.ScanHelper;


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;


    @BindView(R.id.ll_menu_type)
    LinearLayout llMenuType;

    @BindView(R.id.ll_menu_type_hk)
    LinearLayout llMenuTypeHk;


    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);
        }


        SPCache spCache = SPCache.getInstance(getActivity());

        int business_type = spCache.getBusinessType();
        llMenuType.setVisibility((business_type==0 || business_type==1)?View.VISIBLE:View.GONE);
        llMenuTypeHk.setVisibility((business_type==0 || business_type==2)?View.VISIBLE:View.GONE);

        //从缓存读取客户端

        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();

        //拉取数据
        (new HkDataHelper(getApplicationContext())).getBaseInfo();



    }

    @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_hk_putin,
            R.id.ivt_handle_work_order,R.id.ivt_add_work_order,R.id.ivt_hk_tally,R.id.ivt_hk_outbound,
            R.id.ivt_hk_pallet_location,R.id.ivt_hk_pickup,
            R.id.ivt_detection_putin,R.id.ivt_detection_putaway,R.id.ivt_detection_destroy_outbound,R.id.ivt_detection_work})
    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);;
                intent2Page(getActivity(), CheckUnpackingMainActivity.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;
            case R.id.ivt_detection_putin:
                intent2Page(getActivity(), DetectionPutInMainActivity.class, false,bundle);
                break;
            case R.id.ivt_detection_putaway:
                intent2Page(getActivity(), DetectionPutawayMainActivity.class, false,bundle);
                break;
            case R.id.ivt_detection_destroy_outbound:
                intent2Page(getActivity(), DetectionDestroyOutboundMainActivity.class, false,bundle);
                break;

            case R.id.ivt_detection_work:
                intent2Page(getActivity(), DetectionConfirmGoodsMainActivity.class, false,bundle);
                break;


            case R.id.ivt_hk_putin:
                intent2Page(getActivity(), HkPutinMainActivity.class, false,bundle);
                break;
            case R.id.ivt_hk_tally:
                intent2Page(getActivity(), HkTrayMainActivity.class, false,bundle);;
                break;
            case R.id.ivt_hk_outbound:
                intent2Page(getActivity(), HkOutboundMainActivity.class, false,bundle);;
                break;

            case R.id.ivt_hk_pallet_location:
                intent2Page(getActivity(), HkTrayLocationMainActivity.class, false,bundle);;
                break;

            case R.id.ivt_hk_pickup:
                intent2Page(getActivity(), HkPickupStartMainActivity.class, false,bundle);;
                break;

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


        }
    }



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


}
