package com.tofly.ghrq_route.common.ui.activity;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.ui.BaseMapFileActivity;
import com.tofly.ghrq_route.common.ui.adapter.BaseFragmentAdapter;
import com.tofly.ghrq_route.common.ui.fragment.BuildAddressFragment;
import com.tofly.ghrq_route.common.ui.fragment.BuildInfoFragment;
import com.tofly.ghrq_route.common.ui.fragment.BuildReportFragment;
import com.tofly.ghrq_route.common.ui.view.MyAudioRecordButton;
import com.tofly.ghrq_route.module.buildManage.domain.BuildManageBean;
import com.tofly.ghrq_route.module.gps.GpsTool;
import com.tofly.latte_core.base.IPresenter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Amu on 2017/5/24.
 */

public class UiBuildManageDetail extends BaseMapFileActivity {
    @BindView(R.id.main_toolbar)
    Toolbar mainToolbar;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.layout_app_bar)
    AppBarLayout appBarLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.coordinator_layout)
    CoordinatorLayout coordinatorLayout;

    private BuildManageBean mBuildManage;

    private BuildAddressFragment mBuildAddressFragment = null;

    public Point getCurLocation(){
        Point point = null;
        GpsTool gpsTool = GpsTool.getInstance();
        Location location = gpsTool.getLocationInfo();
        if (location != null){
            point = new Point(location.getLongitude(), location.getLatitude());
        }
        return point;
    }

    public String getCurBDAddress(){
        String addr = null;
        if (mBuildAddressFragment != null)
            addr = mBuildAddressFragment.getLocAddr();
        return addr;
    }

    @Override
    public MapView getMapView() {
        MapView mapView = null;
        if (mBuildAddressFragment != null)
            mapView = mBuildAddressFragment.getMapView();
        return mapView;
    }

    @Override
    public void initView() {
        super.initView();
    }

    @Override
    public void initNet() {
        super.initNet();

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null){
            mBuildManage = (BuildManageBean) bundle.getSerializable("build_manage");
        }

        initTabLayout();

        initListener();
    }

    public void initListener() {
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    }

    private void initTabLayout(){
        final String[] tabTitle = getResources().getStringArray(R.array.build_detail_tab_title);
        List<Fragment> mFragments = new ArrayList<>();
        BuildInfoFragment buildInfoFragment = new BuildInfoFragment(mBuildManage,
                baseMapController.permissionHelper, baseMapController.photoHelper);
        mFragments.add(buildInfoFragment);
/*        mBuildAddressFragment = new BuildAddressFragment(mBuildManage, this);
        mFragments.add(mBuildAddressFragment);*/
        BuildReportFragment buildReportFragment = new BuildReportFragment(mBuildManage);
        mFragments.add(buildReportFragment);
        /*BuildHandleFragment buildHanldeFragment = new BuildHandleFragment(mBuildManage);
        mFragments.add(buildHanldeFragment);*/

        BaseFragmentAdapter fragmentAdapter =
                new BaseFragmentAdapter(getSupportFragmentManager(), mFragments, tabTitle);
        viewPager.setAdapter(fragmentAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    public void onBackPressed() {
        EventBus.getDefault().post(new BuildManageDetailEvent(BUILD_MANAGE_NEED_UPDATE));
        finish();
    }

    public void loadAllMap(MapView mapView) {
        baseMapController.loadMap(mapView);
    }

    public void showLayoutManager(MapView mapView) {
        baseMapController.showLayoutManager(mapView);
    }

    public void onTurnToBaiduRoute() {
        Point toPoint = new Point(mBuildManage.getX(), mBuildManage.getY());
        showNavigation(toPoint);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_build_manage_detail, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_enroll:{
                /*回报登记*/
                Intent intent = new Intent();
                intent.setClass(this, UiBuildReportEnrollFirst.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("build_manage", mBuildManage);
                intent.putExtras(bundle);
                startActivity(intent);
            }
            break;
            case R.id.action_apply:{
                /*监护申请*/
                Intent intent = new Intent();
                intent.setClass(this, UiBuildHandleApply.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("build_manage", mBuildManage);
                intent.putExtras(bundle);
                startActivity(intent);
            }
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected IPresenter createPresenter() {
        return null;
    }

    @Override
    public int setLayout() {
        return R.layout.ui_build_detail;
    }

    @Override
    public boolean enableLoadMap() {
        return false;
    }

    @Override
    public boolean enableMoveMapListener() {
        return false;
    }

    @Override
    public boolean enableArcgisLocating() {
        return false;
    }

    @Override
    public boolean enableAmapLocationListener() {
        return false;
    }

    @Override
    public boolean enableMapMoveToLocation() {
        return false;
    }

    @Override
    public TextView getTextAddrView() {
        return null;
    }

    @Override
    public Button getAddPhotoBtn() {
        return null;
    }

    @Override
    public LinearLayout getPhotoGallery() {
        return null;
    }

    @Override
    public MyAudioRecordButton getAudioRecordBtn() {
        return null;
    }

    public final static String BUILD_MANAGE_NEED_UPDATE= "buildManageNeedUpdate";
    public static class BuildManageDetailEvent {
        public String message;
        public BuildManageDetailEvent(String message) {
            this.message = message;
        }
    }


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