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

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatSpinner;
import androidx.appcompat.widget.Toolbar;

import com.gaoyuan.weixinrecord.utils.PermissionHelper;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.module.buildManage.controller.BuildManageController;
import com.tofly.ghrq_route.module.buildManage.domain.BuildManageBean;
import com.tofly.ghrq_route.module.trouble.domain.TroublePictureBean;
import com.tofly.ghrq_route.module.user.domain.UserBean;
import com.tofly.ghrq_route.common.util.image.PhotoHelper;

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

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Amu on 2017/6/1.
 */

public class UiBuildHandleApply extends BaseUi {
    @BindView(R.id.main_toolbar)
    Toolbar mainToolbar;
    @BindView(R.id.report_build_name)
    TextView reportBuildName;
    @BindView(R.id.report_build_addr)
    TextView reportBuildAddr;
    @BindView(R.id.report_build_company)
    TextView reportBuildCompany;
    @BindView(R.id.spinner_build_state)
    AppCompatSpinner spinnerBuildState;
    @BindView(R.id.edit_handle_note)
    EditText editHandleNote;
    @BindView(R.id.btn_add_photo)
    Button btnAddPhoto;
    @BindView(R.id.photo_gallery)
    LinearLayout photoGallery;
    @BindView(R.id.layout_addpic_area)
    LinearLayout layoutAddpicArea;
    @BindArray(R.array.handle_project_state)
    String[] handleStates;

    private static final String TAG = UiBuildHandleApply.class.getName();

    private UserBean mUser;

    private BuildManageController buildManageController;

    private BuildManageBean mBuildManageBean = null;

    private int mBuildState = -1;

    private int mStatePosition = 0 ;

    private List<TroublePictureBean> mPicturesUpload = new ArrayList<TroublePictureBean>();
    private int mUploadIndex;
    private PhotoHelper photoHelper;
    private PermissionHelper permissionHelper;

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

        setContentView(R.layout.ui_build_handle_apply);
        ButterKnife.bind(this);

//        initView();
//        initData();
//        initListener();
    }

//    private void initView(){
//
//    }
//
//    private void initData(){
//        mUser = BaseApp.getInstance().getLoginUser();
//        permissionHelper = new PermissionHelper(this);
//        Intent intent = getIntent();
//        Bundle bundle = intent.getExtras();
//        if (bundle != null){
//            BuildManageBean buildManageBean = (BuildManageBean) bundle.getSerializable("build_manage");
//            if (buildManageBean != null) {
//                mBuildManageBean = buildManageBean;
//
//                String buildname = buildManageBean.getSiteName();
//                reportBuildName.setText(buildname);
//                String buildaddr = buildManageBean.getConstructionAddress();
//                reportBuildAddr.setText(buildaddr);
//                String buildCompany = buildManageBean.getConstructionUnit();
//                reportBuildCompany.setText(buildCompany);
//
//                int buildstate = buildManageBean.getBuild_state() + 1;
//                if (buildstate > 0 && buildstate < handleStates.length) {
//                    mBuildState = buildstate;
//                    List<String> selectStates = new ArrayList<>();
//                    for (int i = buildstate ; i < handleStates.length; i++){
//                        selectStates.add(handleStates[i]);
//                    }
//                    ArrayAdapter<String> adapterStates = new ArrayAdapter<String>(this, R.layout.my_simple_spinner_item, selectStates);
//                    adapterStates.setDropDownViewResource(R.layout.danger_report_spinner_item);
//                    spinnerBuildState.setAdapter(adapterStates);
//                }
//            }
//        }
//    }
//
//    private void initListener(){
//
//    }
//
//    @OnItemSelected({R.id.spinner_build_state})
//    public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
//        switch (parent.getId()){
//            case R.id.spinner_build_state:{
//                mStatePosition = position;
//            }
//            break;
//        }
//    }
//
//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.menu_reported_detail, menu);
//        menu.findItem(R.id.action_clear).setVisible(false);
//        return super.onCreateOptionsMenu(menu);
//    }
//
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        switch (item.getItemId()){
//            case R.id.action_commit:{
//                if (checkValid()){
//                    showCommitAlert();
//                }
//            }
//            break;
//        }
//        return super.onOptionsItemSelected(item);
//    }
//
//    @OnClick({R.id.btn_add_photo})
//    public void onClick(View view){
//        switch (view.getId()){
//            case R.id.btn_add_photo:{
//                showAddPictureDialog();
//            }
//            break;
//        }
//    }
//
//    private void showAddPictureDialog(){
//        AlertDialog.Builder dialog = new AlertDialog.Builder(this).
//                setItems(new String[]{/*"从手机相册选择",*/ "拍照"},
//                        new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface arg0, int i) {
//                                /*if (i == 0) {
//                                    getPicFromSysAlbums();
//                                }*/
//                                if (i == 0) {
//                                    getPicFromSysCamera();
//                                }
//                            }
//                        });
//        dialog.setCancelable(true);
//        dialog.show();
//    }
//
//    @Override
//    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
//        permissionHelper.handleRequestPermissionsResult(requestCode, permissions, grantResults);
//    }
//
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (photoHelper != null){
//            photoHelper.handleActivityResult(requestCode, resultCode, data);
//        }
//    }
//
//    private WaterMarkParams getWaterMarkParams(){
//        WaterMarkParams params = null;
//        if (mBuildManageBean != null){
//            params = new WaterMarkParams();
//            double lon = mBuildManageBean.getX();
//            double lat = mBuildManageBean.getY();
//            BigDecimal decimal = new BigDecimal(lon);
//            lon = decimal.setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
//            decimal = new BigDecimal(lat);
//            lat = decimal.setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
//            params.mLongitude = Double.toString(lon);
//            params.mLatitude = Double.toString(lat);
//            params.mAddress = mBuildManageBean.getConstructionAddress();
//            UserBean userBean = BaseApp.getInstance().getLoginUser();
//            if (userBean != null){
//                params.mUser = userBean.getUsername();
//                params.mCompany = userBean.getCompany_name();
//            }
//            String time = DateUtils.getDate(new Date().getTime());
//            params.mReportTime = time;
//        }
//        return params;
//    }
//
//    private void getPicFromSysAlbums(){
//        if (photoHelper == null)
//            photoHelper = new PhotoHelper(this, permissionHelper);
//        photoHelper.requestImagePhoto(true, new PhotoHelper.GetWatermarkParams() {
//            @Override
//            public WaterMarkParams onGetWatermarkParams() {
//                return getWaterMarkParams();
//            }
//        }, new PhotoHelper.PhotoListener() {
//            @Override
//
//            public void getPhoto(boolean success, String errStr, TroublePictureBean pictureBean) {
//                if (success) {
//                    if (pictureBean != null) {
//                        mPicturesUpload.add(pictureBean);//本地缓存
//                        View view = getPhotoViewFromPath(pictureBean);
//                        photoGallery.addView(view);
//                    }
//                } else {
//                    toast(errStr);
//                }
//            }
//        });
//    }
//
//    private void getPicFromSysCamera(){
//        if (photoHelper == null)
//            photoHelper = new PhotoHelper(this, permissionHelper);
//        photoHelper.requestCameraPhoto(true, new PhotoHelper.GetWatermarkParams() {
//            @Override
//            public WaterMarkParams onGetWatermarkParams() {
//                return getWaterMarkParams();
//            }
//        }, new PhotoHelper.PhotoListener() {
//            @Override
//            public void getPhoto(boolean success, String errStr, TroublePictureBean pictureBean) {
//                if (success) {
//                    if (pictureBean != null) {
//                        mPicturesUpload.add(pictureBean);//本地缓存
//                        View view = getPhotoViewFromPath(pictureBean);
//                        photoGallery.addView(view);
//                    }
//                } else {
//                    toast(errStr);
//                }
//            }
//        });
//    }
//
//    private View getPhotoViewFromPath(TroublePictureBean pictureBean){
//        View rootView = photoHelper.getPhotoViewFromPath(pictureBean,
//                new PhotoHelper.PhotoClickListener() {
//                    @Override
//                    public void onRemovePhoto(View view, TroublePictureBean picture) {
//                        photoGallery.removeView(view);
//                        mPicturesUpload.remove(picture);
//                    }
//                });
//        return rootView;
//    }
//
//    private void showCommitAlert(){
//        String title = "提交申请";
//        String content = "是否提交当前监护申请?";
//        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//        builder.setTitle(title);
//        builder.setMessage(content);
//        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                uploadBuildHandle();
//            }
//        });
//        builder.setNegativeButton(android.R.string.cancel, null);
//        builder.show();
//    }
//
//    private boolean checkValid(){
//        boolean isPictureValid = false;
//
//        if (mPicturesUpload != null && mPicturesUpload.size() > 0) {
//            isPictureValid = true;
//        }
//
//        if (!isPictureValid) {
//            toast("请选择图片");
//            return false;
//        }
//        return true;
//    }
//
//    private void uploadBuildHandle(){
//        if (mBuildManageBean != null){
//            BuildHandleBean handleBean = getUploadHandle();
//            String strUploading = getString(R.string.build_handle_uploading);
//            ProgressManager.getInstance().showProgress(this, null, strUploading, false);
//            if (buildManageController == null)
//                buildManageController = new BuildManageController(this);
//            buildManageController.uploadBuildHandle(mUser.getToken(), handleBean, new BuildManageCallback(){
//                @Override
//                public void onUploadBuildHandle(boolean isSuccess, int errorCode, String errMsg) {
//                    super.onUploadBuildHandle(isSuccess, errorCode, errMsg);
//                    ProgressManager.getInstance().cancelProgress();
//                    if (isSuccess){
//                        EventBus.getDefault().post(new BuildHandleFragment.BuildHandleEvent(BUILD_HANDLE_UPLOADED));
//                        showUploadFinishedDialog();
//                    }else {
//                        toast(errMsg);
//                    }
//                }
//            });
//        }
//    }
//
//    private BuildHandleBean getUploadHandle(){
//        BuildHandleBean handleBean = new BuildHandleBean();
//        handleBean.setBuild_id(mBuildManageBean.getId());
//
//        String handleNote = editHandleNote.getText().toString();
//        handleBean.setHandle_note(handleNote);
//
//        int oldBuildState = mBuildManageBean.getBuild_state();
//        int newBuildState = mBuildState + mStatePosition;
//        if (newBuildState > oldBuildState && newBuildState < handleStates.length){
//            handleBean.setProject_state(mBuildState);
//        }
//
//        String files = "";
//        if (mPicturesUpload != null && mPicturesUpload.size() > 0){
//            List<String> photos = new ArrayList<>();
//            for (TroublePictureBean troublePictureBean : mPicturesUpload){
//                photos.add(troublePictureBean.getFilepath());
//            }
//            files = StringUtil.listToString(photos, ',');
//        }
//        handleBean.setFile(files);
//
//        return handleBean;
//    }
//
//    private void showUploadFinishedDialog(){
//        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//        builder.setMessage(R.string.build_report_upload_ok);
//        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                UiBuildHandleApply.this.finish();
//            }
//        });
//        builder.show();
//    }
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//    }
}
