package com.personal.xiri.only.design;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.Snackbar;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.personal.xiri.only.R;
import com.personal.xiri.only.appmanager.AppSkinManager;
import com.personal.xiri.only.appmanager.AppStyleManager;
import com.personal.xiri.only.common.DownloadOnlyCallBack;
import com.personal.xiri.only.common.FilePathManager;
import com.personal.xiri.only.common.ImageLoaderManager;
import com.personal.xiri.only.common.album.PhotoPickerActivity;
import com.personal.xiri.only.data.StyleConfigureConstants;
import com.personal.xiri.only.data.StyleConstants;
import com.personal.xiri.only.data.model.ConfigItem;
import com.personal.xiri.only.data.model.ImagePath;
import com.personal.xiri.only.data.model.ImageStyle;
import com.personal.xiri.only.data.model.Skin;
import com.personal.xiri.only.data.model.SkinData;
import com.personal.xiri.only.events.DatabaseEvent;
import com.personal.xiri.only.events.Event;
import com.personal.xiri.only.utils.ConvertUtils;
import com.personal.xiri.only.utils.DensityUtils;
import com.personal.xiri.only.utils.EncryptUtils;
import com.personal.xiri.only.utils.FileUtils;
import com.personal.xiri.only.utils.JSONParser;
import com.personal.xiri.only.utils.OkioFileUtil;
import com.personal.xiri.only.utils.TLog;
import com.personal.xiri.only.widgets.ToastMaker;
import com.yalantis.ucrop.UCrop;
import com.yalantis.ucrop.UCropActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.io.FileOutputStream;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import static com.personal.xiri.only.common.album.util.PhotoPicker.EXTRA_SELECTED_ENABLED;
import static com.personal.xiri.only.common.album.util.PhotoPicker.EXTRA_SHOW_BOTTOM;
import static com.personal.xiri.only.common.album.util.PhotoPicker.EXTRA_SHOW_GIF;
import static com.personal.xiri.only.data.StyleConstants.BG_CHAT_BOTTOM_BAR;
import static com.personal.xiri.only.data.StyleConstants.BG_CHAT_CONTENT;
import static com.personal.xiri.only.data.StyleConstants.BG_CHAT_CONTENT_BOTTOM;
import static com.personal.xiri.only.data.StyleConstants.BG_CHAT_CONTENT_RIGHT;
import static com.personal.xiri.only.data.StyleConstants.BG_CHAT_DRAWER_BOTTOM_RIGHT;
import static com.personal.xiri.only.data.StyleConstants.BG_CHAT_DRAWER_RIGHT;
import static com.personal.xiri.only.data.StyleConstants.BG_CHAT_DRAWER_SCRIM_RIGHT;
import static com.personal.xiri.only.data.StyleConstants.BG_CHAT_DRAWER_TOP_RIGHT;
import static com.personal.xiri.only.data.StyleConstants.BG_CHAT_EDITTEXT;
import static com.personal.xiri.only.data.StyleConstants.BG_TYPE_COLOR;
import static com.personal.xiri.only.data.StyleConstants.COLOR_STRINGS;
import static com.personal.xiri.only.data.StyleConstants.DEF_DRAWER_BG_COLOR_RIGHT;
import static com.personal.xiri.only.data.StyleConstants.DEF_SVG_IMAGE_CHECKED;
import static com.personal.xiri.only.data.StyleConstants.HEAD_IDS;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_BOTTOM_CAMERA;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_BOTTOM_EMOJI;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_BOTTOM_GAME;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_BOTTOM_MORE;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_BOTTOM_PHOTO;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_BOTTOM_VIDEO;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_BOTTOM_VOICE;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_CHAT_DRAWER_CALL;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_CHAT_DRAWER_FILE;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_CHAT_DRAWER_GRADE;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_CHAT_DRAWER_PHOTO;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_CHAT_DRAWER_SKIN;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_CHAT_DRAWER_VIDEO;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_CONTENT_AVATAR_RIGHT;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_EDIT_SEND;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_STYLE_ID_OVER;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_TINT_MODE_IMG;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_TINT_MODE_MUTIPLY;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_TINT_MODE_SRC_IN;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_TYPE_DEFAULT;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_TYPE_INTERNET;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_TYPE_LOCAL;
import static com.personal.xiri.only.data.StyleConstants.IMAGE_TYPE_TEXT;
import static com.personal.xiri.only.data.StyleConstants.STYLE_IMG_STATUS_CHANGE;
import static com.personal.xiri.only.data.StyleConstants.STYLE_IMG_STATUS_NORMAL;

/**
 * Created by gaojianyang on 17/8/27.
 */
public class StyleMainConfigActivity extends BaseSyleConfigActivity implements View.OnClickListener {
    private final static String TAG = "StyleMainConfigActivity";
//    protected SparseArray<ImageStyle> clonedImageStyles;
//    protected SparseArray<ImageStyle> originalImageStyles;
//    protected ArrayList<ImageStyle> clonedImageStylesList;
//
//    //main ui
//    protected LinearLayout llContent;
//    protected RecyclerView mainRv;
//    protected SimpleChatAdapter simpleAdapter;
//    protected ArrayList<IMMessage> messages = new ArrayList<>();
//    protected CheckableImageView svgVideo;
//    protected CheckableImageView svgVoice;
//    protected CheckableImageView svgPhoto;
//    protected CheckableImageView svgCamera;
//    protected CheckableImageView svgEmoji;
//    protected CheckableImageView svgGame;
//    protected CheckableImageView svgMore;
//    protected ImageView ivSend;
//    protected ImageView chatBg;
//    protected ViewGroup editTextBg;
//    protected ViewGroup chatBottomBg;
//    protected ViewGroup bottomBarBg;


    //    protected RecyclerView.Adapter adapter;

    //drawerUi
//    private LinearLayout llLeftPercent, llRightPercent, llTop, llBottom;
//    private RelativeLayout redrawerAll;
//    private StyleTextView item1, item2, item3, item4, item5, item6;
//    private ImageView ivHead;
//    private float drawerMinPercent;
//    private int drawerMinProgress;

    //    //    private OverScrollView overScrollVi/ew;
    //common
//    protected TextView tvBack;
//    protected TextView tvTittle;
//    protected TextView tvMenu1;
//    protected TextView tvMenu2;
//    protected NumberProgressBar pbProgress;
//    protected LinearLayout layoutSave;
//    protected TextView tvSave;
//    protected NoHorizontalScrollerViewPager viewPager;
    //    protected CheckTextView tvMainUi;
//    protected CheckTextView tvDrawerUi;
    private StyleConfigMainAdapter mainAdapter;
    private ConfigPagerRvAdapter drawerAdapter;
    private LinearLayout loadingDialog;
    private LinearLayoutManager mainLayoutManager;
    private LinearLayoutManager drawerLayoutManager;

    //    protected ChoiceView hideCv;
//    private SeekBar sbColor, sbColorSat, sbColorVal, sbColorAlpha;
    private ConfigItem configItem1, configItem2, configItem3, configItem4, configItem5;
    private ConfigItem drawerConfigItem1, drawerConfigItem2, drawerConfigItem3, drawerConfigItem4, drawerConfigItem5;

    private ArrayList<ConfigItem> mainItems;
    private ArrayList<ConfigItem> drawerItems;
//    private LinearLayout llColorConfig, llInputSvg, llInputText;
//    private RelativeLayout llInputPath;
//    private ImageView ivPagerBack;
//    private EditText etHexValue, etInputPath, etInputText, etInputSvg;
//    private TextView tvPathSure;
//    private ColorPickerPanelView colorPanel;
//    private ImageView ivDelete;
//    private RecyclerView rvPager;
//    private ArrayList<View> viewList;

    private ImageView loadingIv;
    private TextView loadingTv;
    private Animation rotate;

    private int currentStyleId;
    private ArrayList<ImagePath> pathsContainer;

    //    private boolean isCheckedColor = false;
    private int colorType;//0 normal
    private boolean isCheckedImg = false;

    private final Handler mHandler = new CloseDialogHandler(this);
    private int currentPageType;//0 path  1 text
    //    protected String currentStyleDirName = "default";
    private int lastDrawerStyleid = 0;
    private int lastMainStyleid = 0;
//    private SkinData skinData;
//    private Skin originalSkin;

    private static final int COLOR_TYPE_NORMAL = 0;
    private static final int COLOR_TYPE_CHECK = 1;


//    private String identifier;


//    @Override
//    protected void setUpContentView() {
//        setContentView(R.layout.activity_style_drawer_config);
//    }

//    @Override
//    protected void setUpView() {
//        currentStyleId = BG_CHAT_CONTENT;
////        llContent = (LinearLayout) findViewById(R.id.chat_drawer_all);
////        initMainView();
////        initDrawerView();
//
//        //common view
////        tvBack = (TextView) findViewById(R.id.back_tv);
////        tvTittle = (TextView) findViewById(R.id.title_txt);
////        tvMenu1 = (TextView) findViewById(R.id.title_right_one);
////        tvMenu2 = (TextView) findViewById(R.id.title_right_two);
////        loadingDialog = (LinearLayout) findViewById(R.id.loading_dialog_all);
////        loadingIv = (ImageView) findViewById(R.id.dialog_content_iv);
////        loadingTv = (TextView) findViewById(R.id.dialog_content_tv);
////        layoutSave = (LinearLayout) findViewById(R.id.layout_save);
////        pbProgress = (NumberProgressBar) findViewById(R.id.save_progressbar);
////        tvSave = (TextView) findViewById(R.id.save_txt);
//////        hideCv = (ChoiceView) findViewById(R.id.tv_hide);
////        tvMenu2.setText(R.string.style_visible_gone);
//////        hideCv.setText(R.string.style_visible_gone);
//////        hideCv.setChecked(false);
////        tvTittle.setText(StyleConstants.MODULE_STRINGS_MAIN[0]);
////        tvMenu1.setOnClickListener(this);
////        tvMenu2.setOnClickListener(this);
//
//        viewList = new ArrayList<View>();
//        LayoutInflater inflater = getLayoutInflater();
//        View view1 = inflater.inflate(R.layout.adapter_config_pager, viewPager, false);
//        View view2 = inflater.inflate(R.layout.adapter_config_pager2, null);
//
//        rvPager = (RecyclerView) view1.findViewById(R.id.adapter_config_pager_rv);
////        sbDrawerWidth = (SeekBar) view1.findViewById(R.id.drawer_width_sb);
////        llDrawerWidth = (LinearLayout) view1.findViewById(R.id.ll_drawer_width);
////        sbDrawerWidth.setVisibility(View.GONE);
////        llDrawerWidth.setVisibility(View.GONE);
//        tabLayout = (TabLayout) view1.findViewById(R.id.tabLayout);
//        tabLayout.addTab(tabLayout.newTab().setText(R.string.skin_main_ui));
//        tabLayout.addTab(tabLayout.newTab().setText(R.string.skin_drawer_ui));
//        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//            @Override
//            public void onTabSelected(TabLayout.Tab tab) {
//                if (tab.getPosition() == 0) {
//                    if (!isDrawerConfig) return;
//                    isDrawerConfig = false;
//                    lastDrawerStyleid = currentStyleId;
////                    tvMainUi.setChecked(true);
////                    tvDrawerUi.setChecked(false);
//                    setCurrentMainUiOrDrawerUi();
////                    runBackAnimation(llLeftPercent, llRightPercent);
//
//                } else {
//                    if (isDrawerConfig) return;
//                    isDrawerConfig = true;
//                    lastMainStyleid = currentStyleId;
////                    tvMainUi.setChecked(false);
////                    tvDrawerUi.setChecked(true);
//                    setCurrentMainUiOrDrawerUi();
////                    runEnterAnimation(llLeftPercent, llRightPercent);
//                }
//            }
//
//            @Override
//            public void onTabUnselected(TabLayout.Tab tab) {
//
//            }
//
//            @Override
//            public void onTabReselected(TabLayout.Tab tab) {
//
//            }
//        });
////        tvMainUi = (CheckTextView) view1.findViewById(R.id.adapter_config_pager_main_tv);
////        tvDrawerUi = (CheckTextView) view1.findViewById(R.id.adapter_config_pager_drawer_tv);
//
//        llColorConfig = (LinearLayout) view2.findViewById(R.id.ll_color_config);
//        llInputText = (LinearLayout) view2.findViewById(R.id.ll_input_text);
//        llInputSvg = (LinearLayout) view2.findViewById(R.id.ll_input_svg);
//        llInputPath = (RelativeLayout) view2.findViewById(R.id.ll_input_path);
//        ivPagerBack = (ImageView) view2.findViewById(R.id.iv_pager_back);
//        etHexValue = (EditText) view2.findViewById(R.id.et_hex_val);
//        etInputPath = (EditText) view2.findViewById(R.id.et_inputpath);
//        etInputText = (EditText) view2.findViewById(R.id.et_inputtext);
//        etInputSvg = (EditText) view2.findViewById(R.id.et_inputsvg);
//        sbColor = (SeekBar) view2.findViewById(R.id.adapter_pager_item_sb);
//        sbColorSat = (SeekBar) view2.findViewById(R.id.adapter_pager_item_sb_sat);
//        sbColorVal = (SeekBar) view2.findViewById(R.id.adapter_pager_item_sb_val);
//        sbColorAlpha = (SeekBar) view2.findViewById(R.id.adapter_pager_item_sb_alpha);
//        if (AppStyleManager.tf != null) {
//            ((TextView) view2.findViewById(R.id.tv_h)).setTypeface(AppStyleManager.tf);
//            ((TextView) view2.findViewById(R.id.tv_s)).setTypeface(AppStyleManager.tf);
//            ((TextView) view2.findViewById(R.id.tv_v)).setTypeface(AppStyleManager.tf);
//            ((TextView) view2.findViewById(R.id.tv_alpha)).setTypeface(AppStyleManager.tf);
//            ((TextView) view2.findViewById(R.id.tv_text)).setTypeface(AppStyleManager.tf);
//            ((TextView) view2.findViewById(R.id.tv_svg)).setTypeface(AppStyleManager.tf);
//        }
//        colorPanel = (ColorPickerPanelView) view2.findViewById(R.id.new_color_panel);
//        ivDelete = (ImageView) view2.findViewById(R.id.iv_et_delete);
//        ivDelete.setVisibility(View.INVISIBLE);
//        sbColor.setProgressDrawable(new SBGradientDrawable());
//        sbColorSat.setProgressDrawable(new SBSatDrawable());
//        sbColorVal.setProgressDrawable(new SbValDrawable());
//        etInputPath.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                hideSoftInputKeyBoard(v);
//                loadImageFromAddress(etInputPath.getText().toString());
//                return true;
//            }
//        });
//        etInputSvg.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                hideSoftInputKeyBoard(v);
//                if (getCurrentStyle() != null) {
//                    getCurrentStyle().setImageType(IMAGE_TYPE_SVG);
//                    getCurrentStyle().setSvgPath(etInputSvg.getText().toString());
//                    changeUiByConfigStyleId(getCurrentStyle());
//                }
//                return true;
//            }
//        });
//        etInputText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                hideSoftInputKeyBoard(v);
//                if (getCurrentStyle() != null) {
//                    if (!isDrawerConfig) getCurrentStyle().setImageType(IMAGE_TYPE_TEXT);
//                    getCurrentStyle().setTextString(etInputText.getText().toString());
//                    changeUiByConfigStyleId(getCurrentStyle());
//                }
//                return true;
//            }
//        });
//        etInputPath.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                if (s != null && !TextUtils.isEmpty(s.toString())) {
//                    ivDelete.setVisibility(View.VISIBLE);
//                } else {
//                    ivDelete.setVisibility(View.INVISIBLE);
//                }
//
//            }
//        });
//        etHexValue.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//
//                if (etHexValue.getText() != null && !TextUtils.isEmpty(etHexValue.getText().toString()) && etHexValue.getText().toString().length() == 8) {
//                    StringBuilder sbColor = new StringBuilder(etHexValue.getText().toString());
//                    sbColor.insert(0, "#");
//                    clonedImageStyles.get(currentStyleId).setMainColor(sbColor.toString());
//                    int color = ConvertUtils.HextoColor(sbColor.toString());
//                    colorPanel.setColor(color);
//                    postColorAndUpdateUi(color);
//                } else {
//                    ToastMaker.showShortToast(R.string.color_eight);
//                }
//                return true;
//            }
//        });
//        tvPathSure = (TextView) view2.findViewById(R.id.tv_input_sure);
////        tvMainUi.setOrition(true);
////        tvMainUi.setChecked(true);
////        tvDrawerUi.setOrition(false);
////        tvDrawerUi.setChecked(false);
////        tvMainUi.setOnClickListener(this);
////        tvDrawerUi.setOnClickListener(this);
//        tvPathSure.setOnClickListener(this);
//        ivPagerBack.setOnClickListener(this);
//        ivDelete.setOnClickListener(this);
//        viewList.add(view1);
//        viewList.add(view2);
//
//
//    }

//    @Override
//    protected void setUpData(Bundle savedInstanceState) {
//        EventBus.getDefault().register(this);
////        currentStyleDirName = getIntent().getStringExtra(StyleConfigureConstants.CONFIGURE_DIR_NAME);
//        identifier = (String) SharedPreferencesHelper.getInstance().getData("identifier", Constant.IDENTIFIER_TEST);
//
////        if (TextUtils.isEmpty(currentStyleDirName)) {
////            currentStyleDirName = "default";
////        }
//        initMainmainItemsData();
//        mainLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
//        drawerLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
//        mainAdapter = new StyleConfigMainAdapter(mainItems, this);
//        rvPager.setLayoutManager(mainLayoutManager);
//        rvPager.setAdapter(mainAdapter);
//        PagerAdapter pagerAdapter = new AdapterViewpager(viewList);
//        viewPager.setAdapter(pagerAdapter);
//        screenWidth = DensityUtils.getDisplayWidth(this);
//        screenHeight = DensityUtils.getDisplayHeight(this);
//        initClonedStyles();
//        initMainStyleConfigure();
//        initDrawerStyleConfigure();
//        setContentParams();
//        initColorSbProgress();
//        initFakeData();
//        simpleAdapter = new SimpleChatAdapter(messages, this, clonedImageStyles, getSkinDirName(skinData));
//        LinearLayoutManager simpleLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
//        mainRv.setLayoutManager(simpleLayoutManager);
//        mainRv.setAdapter(simpleAdapter);
//    }

    @Override
    protected void initExtraData() {
        currentStyleId = BG_CHAT_CONTENT;
        initMainmainItemsData();

        mainLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        drawerLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mainAdapter = new StyleConfigMainAdapter(mainItems, this);
        rvPager.setLayoutManager(mainLayoutManager);
        rvPager.setAdapter(mainAdapter);
        PagerAdapter pagerAdapter = new AdapterViewpager(viewList);
        viewPager.setAdapter(pagerAdapter);
//        initColorSbProgress();
    }

    @Override
    protected void onMainTabSelected() {
//        if (!isDrawerConfig) return;
//        isDrawerConfig = false;
        lastDrawerStyleid = currentStyleId;
        setCurrentMainUiOrDrawerUi();
    }

    @Override
    protected void onDrawerTabSelected() {
//        if (isDrawerConfig) return;
//        isDrawerConfig = true;
        lastMainStyleid = currentStyleId;
        setCurrentMainUiOrDrawerUi();
    }

    @Override
    protected void onInputPathEditorAction() {
        loadImageFromAddress(etInputPath.getText().toString());
    }


    @Override
    protected void onInputTextEditorAction() {
        if (getCurrentStyle() != null) {
            if (!isDrawerConfig) getCurrentStyle().setImageType(IMAGE_TYPE_TEXT);
            getCurrentStyle().setTextString(etInputText.getText().toString());
            changeUiByConfigStyleId(getCurrentStyle());
        }
    }

    @Override
    protected void onEtHexValueEditorAction() {
        if (etHexValue.getText() != null && !TextUtils.isEmpty(etHexValue.getText().toString()) && etHexValue.getText().toString().length() == 8) {
            StringBuilder sbColor = new StringBuilder(etHexValue.getText().toString());
            sbColor.insert(0, "#");
            int color = ConvertUtils.HextoColor(sbColor.toString());
            setColorChanged(color, colorType);
            colorPanel.setColor(color);
            postColorAndUpdateUi(color);
        } else {
            ToastMaker.showShortToast(R.string.color_eight);
        }
    }


//
//    private void initMainView() {
//        mainRv = (RecyclerView) findViewById(R.id.main_rv);
//        svgMore = (CheckableImageView) findViewById(R.id.more);
//        svgGame = (CheckableImageView) findViewById(R.id.game);
//        svgEmoji = (CheckableImageView) findViewById(R.id.emoji);
//        svgCamera = (CheckableImageView) findViewById(R.id.camera);
//        svgPhoto = (CheckableImageView) findViewById(R.id.photo);
//        svgVoice = (CheckableImageView) findViewById(R.id.voice);
//        svgVideo = (CheckableImageView) findViewById(R.id.video);
//        ivSend = (ImageView) findViewById(R.id.chat_send);
//        chatBg = (ImageView) findViewById(R.id.style_configure_img_bg);
//        editTextBg = (ViewGroup) findViewById(R.id.style_configure_edit_bg);
//        chatBottomBg = (ViewGroup) findViewById(R.id.style_configure_chat_bottom);
//        bottomBarBg = (ViewGroup) findViewById(R.id.bottom_bar_bg);
//        viewPager = (NoHorizontalScrollerViewPager) findViewById(R.id.chat_drawer_bottom_vp);
//
//    }
//
//    private void initDrawerView() {
//        redrawerAll = (RelativeLayout) findViewById(R.id.drawer_all_rv);
//        llLeftPercent = (LinearLayout) findViewById(R.id.chat_drawer_leftparams);
//        llRightPercent = (LinearLayout) findViewById(R.id.chat_drawer_rightparams);
//        llTop = (LinearLayout) findViewById(R.id.chat_drawer_top);
//        llBottom = (LinearLayout) findViewById(R.id.chat_drawer_bottom);
//        viewPager = (NoHorizontalScrollerViewPager) findViewById(R.id.chat_drawer_bottom_vp);
//
//        ivHead = (ImageView) findViewById(R.id.chat_drawer_head);
//        item1 = (StyleTextView) findViewById(R.id.chat_drawer_item1);
//        item2 = (StyleTextView) findViewById(R.id.chat_drawer_item2);
//        item3 = (StyleTextView) findViewById(R.id.chat_drawer_item3);
//        item4 = (StyleTextView) findViewById(R.id.chat_drawer_item4);
//        item5 = (StyleTextView) findViewById(R.id.chat_drawer_item5);
//        item6 = (StyleTextView) findViewById(R.id.chat_drawer_item6);
////        overScrollView= (OverScrollView) findViewById(R.id.drawer_overscrollview);
////        overScrollView.setVisibility(View.INVISIBLE);
////        llLeftPercent.setVisibility(View.INVISIBLE);
////        llRightPercent.setVisibility(View.INVISIBLE);
//        redrawerAll.setVisibility(View.INVISIBLE);
//
//    }

    //    protected RecyclerView.Adapter getConfigureAdapter() {
//        return new StyleConfigMainAdapter(mainItems, this);
//    }
//    private void initClonedStyles() {
//        clonedImageStyles = new SparseArray<>();
//        clonedImageStylesList = new ArrayList<>();
//        originalImageStyles = new SparseArray<>();
//        if (AppSkinManager.getEditSkinData() != null) {
//            this.skinData = AppSkinManager.getEditSkinData();
//            this.originalSkin = AppSkinManager.getEditSkin();
//            if (skinData != null && skinData.getSkinStyle() != null && skinData.getSkinStyle().size() > 0) {
//                ArrayList<ImageStyle> originalStyles = AppSkinManager.getEditSkinData().getSkinStyle();
//                for (int i = 0; i < originalStyles.size(); i++) {
//                    originalImageStyles.put(originalStyles.get(i).getId(), originalStyles.get(i));
//
//                    ImageStyle cloneStyle = originalStyles.get(i).clone();
//                    clonedImageStyles.put(originalStyles.get(i).getId(), cloneStyle);
//                    clonedImageStylesList.add(cloneStyle);
//                }
//            }
//        } else {
//            TLog.d(TAG, "empty skin finish");
//            finish();
//        }
//
//    }


//    private void initMainStyleConfigure() {
//        setStateBarColor(this, Color.BLACK);
//        AppStyleManager.setBottomSvgConfigureDependOnStyle(svgCamera, clonedImageStyles.get(IMAGE_BOTTOM_CAMERA), getSkinDirName(skinData));
//        AppStyleManager.setBottomSvgConfigureDependOnStyle(svgMore, clonedImageStyles.get(IMAGE_BOTTOM_MORE), getSkinDirName(skinData));
//        AppStyleManager.setBottomSvgConfigureDependOnStyle(svgGame, clonedImageStyles.get(IMAGE_BOTTOM_GAME), getSkinDirName(skinData));
//        AppStyleManager.setBottomSvgConfigureDependOnStyle(svgVideo, clonedImageStyles.get(IMAGE_BOTTOM_VIDEO), getSkinDirName(skinData));
//        AppStyleManager.setBottomSvgConfigureDependOnStyle(svgEmoji, clonedImageStyles.get(IMAGE_BOTTOM_EMOJI), getSkinDirName(skinData));
//        AppStyleManager.setBottomSvgConfigureDependOnStyle(svgPhoto, clonedImageStyles.get(IMAGE_BOTTOM_PHOTO), getSkinDirName(skinData));
//        AppStyleManager.setBottomSvgConfigureDependOnStyle(svgVoice, clonedImageStyles.get(IMAGE_BOTTOM_VOICE), getSkinDirName(skinData));
//        AppStyleManager.setImageViewConfigureDependOnStyle(ivSend, clonedImageStyles.get(IMAGE_EDIT_SEND), getSkinDirName(skinData));
//        AppStyleManager.setChatContentBgConfigureDependOnStyle(chatBg, clonedImageStyles.get(BG_CHAT_CONTENT), getSkinDirName(skinData));
//        AppStyleManager.setChatBottomBgConfigureDependOnStyle(chatBottomBg, clonedImageStyles.get(BG_CHAT_CONTENT_BOTTOM), getSkinDirName(skinData));
//        AppStyleManager.setChatEditTextBgConfigureDependOnStyle(editTextBg, clonedImageStyles.get(BG_CHAT_EDITTEXT));
//        AppStyleManager.setBottomBarBgConfigureDependOnStyle(bottomBarBg, clonedImageStyles.get(BG_CHAT_BOTTOM_BAR), getSkinDirName(skinData));
//
//    }
//
//    private void initDrawerStyleConfigure() {
//        AppStyleManager.setChatBottomBgConfigureDependOnStyle(llLeftPercent, clonedImageStyles.get(BG_CHAT_DRAWER_RIGHT), getSkinDirName(skinData));
//        AppStyleManager.setChatBottomBgConfigureDependOnStyle(llTop, clonedImageStyles.get(BG_CHAT_DRAWER_TOP_RIGHT), getSkinDirName(skinData));
//        AppStyleManager.setChatBottomBgConfigureDependOnStyle(llBottom, clonedImageStyles.get(BG_CHAT_DRAWER_BOTTOM_RIGHT), getSkinDirName(skinData));
//        AppStyleManager.setChatScrimBgConfigureDependOnStyle(llRightPercent, clonedImageStyles.get(BG_CHAT_DRAWER_SCRIM_RIGHT));
//        AppStyleManager.setDrawertextCongigureDependOnStyle(item1, clonedImageStyles.get(IMAGE_CHAT_DRAWER_VIDEO), getSkinDirName(skinData));
//        AppStyleManager.setDrawertextCongigureDependOnStyle(item2, clonedImageStyles.get(IMAGE_CHAT_DRAWER_CALL), getSkinDirName(skinData));
//        AppStyleManager.setDrawertextCongigureDependOnStyle(item3, clonedImageStyles.get(IMAGE_CHAT_DRAWER_SKIN), getSkinDirName(skinData));
//        AppStyleManager.setDrawertextCongigureDependOnStyle(item4, clonedImageStyles.get(IMAGE_CHAT_DRAWER_GRADE), getSkinDirName(skinData));
//        AppStyleManager.setDrawertextCongigureDependOnStyle(item5, clonedImageStyles.get(IMAGE_CHAT_DRAWER_FILE), getSkinDirName(skinData));
//        AppStyleManager.setDrawertextCongigureDependOnStyle(item6, clonedImageStyles.get(IMAGE_CHAT_DRAWER_PHOTO), getSkinDirName(skinData));
//        AppStyleManager.setChatDrawerAvatarConfigureDependOnStyle(ivHead, clonedImageStyles.get(IMAGE_CONTENT_AVATAR_RIGHT), getSkinDirName(skinData));
//        Glide.with(this).load(R.drawable.head_right).priority(Priority.HIGH).diskCacheStrategy(DiskCacheStrategy.NONE).bitmapTransform(new CropCircleTransformation(this)).into(ivHead);
//        float leftPercent = clonedImageStyles.get(BG_CHAT_DRAWER_RIGHT).getWidth();
//        drawerMinPercent = DensityUtils.dip2px(this, 100) * 1.0f / screenWidth;
//        drawerMinProgress = Math.round(drawerMinPercent * 100);
//        drawerMinPercent = drawerMinProgress * 1.0f / 100;
//        Log.d(TAG, "initDrawerStyleConfigure() returned: drawerMinPercent " + drawerMinPercent);
//        if (leftPercent < drawerMinPercent)
//            leftPercent = drawerMinPercent;
//        llLeftPercent.getLayoutParams().width = (int) (screenWidth * leftPercent);
//        llRightPercent.getLayoutParams().width = screenWidth - (int) (screenWidth * leftPercent);
//
//    }

    private void postColorAndUpdateUi(int color) {
        sbColorVal.setOnSeekBarChangeListener(null);
        sbColorSat.setOnSeekBarChangeListener(null);
        sbColor.setOnSeekBarChangeListener(null);
        sbColorAlpha.setOnSeekBarChangeListener(null);
        Color.RGBToHSV(Color.red(color), Color.green(color), Color.blue(color), HSV);
        currentColor = color;
        alpha = Color.alpha(color);
        sbColorAlpha.setProgress(255 - alpha);
        sbColor.setProgress(359 - (int) HSV[0]);
        sbColorSat.setProgress((int) (HSV[1] * 100));
        sbColorVal.setProgress((int) (HSV[2] * 100));
        changeUiByConfigStyleId(clonedImageStyles.get(currentStyleId));
        sbColorVal.setOnSeekBarChangeListener(this);
        sbColorSat.setOnSeekBarChangeListener(this);
        sbColor.setOnSeekBarChangeListener(this);
        sbColorAlpha.setOnSeekBarChangeListener(this);
    }

//    private void initFakeData() {
//        String[] fakeArray = getResources().getStringArray(R.array.fake_messages);
//        for (int i = 1; i <= fakeArray.length; i++) {
//            IMMessage message = new IMMessage(System.currentTimeMillis(), i % 2 == 0 ? true : false, 0, "", "");
//            message.setContent(fakeArray[i - 1]);
//            messages.add(message);
//        }
//    }

    private void initColorSbProgress() {
        sbColor.setMax(359);
        sbColorSat.setMax(100);
        sbColorVal.setMax(100);
        sbColorVal.setProgress(50);
        sbColorSat.setProgress(50);
        sbColorAlpha.setMax(254);
        sbColorAlpha.setProgress(0);
        sbColorVal.setOnSeekBarChangeListener(this);
        sbColorSat.setOnSeekBarChangeListener(this);
        sbColor.setOnSeekBarChangeListener(this);
        sbColorAlpha.setOnSeekBarChangeListener(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onConfigChanged(Event.StyleDrawerEvent event) {
        if (!isDrawerConfig) {
            setMainConfigChanged(event);
        } else {
            setDrawerConfigChanged(event);
        }
    }

    private void setMainConfigChanged(Event.StyleDrawerEvent event) {
        final int position = event.getPosition();
        ImageStyle currentStyle = clonedImageStyles.get(currentStyleId);
        if (currentStyle == null) return;
        switch (event.getStatementId()) {
            case R.string.style_main_module:
                setConfigItemAndStyleIdByPosition(position);
                break;
            case R.string.style_main_bg_type:
                if (currentStyleId == BG_CHAT_CONTENT || currentStyleId == BG_CHAT_CONTENT_BOTTOM || currentStyleId == BG_CHAT_BOTTOM_BAR) {
                    setNoramlBgTypeChanged(currentStyle, position);
                    setNormalmainItems(currentStyle, getPositionFromCurrentStyleid(currentStyleId));
                } else if (currentStyleId == BG_CHAT_CONTENT_RIGHT) {
                    setNoramlBgTypeChanged(currentStyle, position);
                    setTextBgmainItems(currentStyle, getPositionFromCurrentStyleid(currentStyleId));
                } else if (currentStyleId == IMAGE_CONTENT_AVATAR_RIGHT) {
                    setNoramlBgTypeChanged(currentStyle, position);
                    setAvatarmainItems(currentStyle, getPositionFromCurrentStyleid(currentStyleId));
                }
                changeUiByConfigStyleId(currentStyle);
                break;
            case R.string.style_main_avatar_shape:
                if (currentStyleId == IMAGE_CONTENT_AVATAR_RIGHT) {
                    currentStyle.setShapeType(position);
                    setAvatarmainItems(currentStyle, getPositionFromCurrentStyleid(currentStyleId));
                }
                changeUiByConfigStyleId(currentStyle);
                break;
//            case R.string.style_main_svg_path:
//                break;
//            case R.string.style_main_text_content:
//                break;
            case R.string.style_main_bg_color:
//                isCheckedColor = false;
                if (position == 0) {
                    setDefaultColorByStyleId();
                } else if (position < COLOR_STRINGS.length - 2) {
                    setColorChanged(getResources().getColor(COLOR_STRINGS[position]), COLOR_TYPE_NORMAL);
                } else if (position == COLOR_STRINGS.length - 2) {
                    int currentColor = -1;
                    if (!TextUtils.isEmpty(currentStyle.getMainColor()))
                        currentColor = ConvertUtils.HextoColor(currentStyle.getMainColor());
                    gotoColorConfig(currentColor, COLOR_TYPE_NORMAL);
                }
                setConfigItemById(currentStyleId, getPositionFromCurrentStyleid(currentStyleId));
                break;
            case R.string.style_main_check_color:
//                isCheckedColor = true;
                if (position == 0) {
                    setDefaultCheckedColorByStyleId();
                } else if (position < COLOR_STRINGS.length - 2) {
                    setColorChanged(getResources().getColor(COLOR_STRINGS[position]), COLOR_TYPE_CHECK);
                } else if (position == COLOR_STRINGS.length - 2) {
                    int currentColor = -1;
                    if (!TextUtils.isEmpty(currentStyle.getCheckedColor())) {
                        currentColor = ConvertUtils.HextoColor(currentStyle.getCheckedColor());
                    }
                    gotoColorConfig(currentColor, COLOR_TYPE_CHECK);
                }
                setConfigItemById(currentStyleId, getPositionFromCurrentStyleid(currentStyleId));
                break;
            case R.string.style_main_icon_type:
                setIconTypeChanged(currentStyle, position);
                setCheckIconmainItems(currentStyle, getPositionFromCurrentStyleid(currentStyleId));
                break;
            case R.string.style_main_text_content:
                gotoIconTextInput(currentStyle.getTextString());
                break;
            case R.string.style_main_check_type:
                setIconCheckTypeChanged(currentStyle, position);
                setCheckIconmainItems(currentStyle, getPositionFromCurrentStyleid(currentStyleId));
                break;
            case R.string.style_chat_bg_shape:
                currentStyle.setShapeType(position);
                setTextBgmainItems(currentStyle, getPositionFromCurrentStyleid(currentStyleId));
                changeUiByConfigStyleId(currentStyle);
                break;
            case R.string.style_main_bg_path:
                isCheckedImg = false;
                goDifferentUiByImageType(currentStyle.getImageType(), currentStyle.getTextString());
//                if (!TextUtils.isEmpty(currentStyle.getImageName())) {
//                    if (currentStyleId == BG_CHAT_CONTENT_RIGHT) {
//                        startToNinePatchactivityUpdate(clonedImageStyles.get(currentStyleId));
//                    } else {
//                        final String pathName = getCurrentSkinPath();
//                        File outputFile = new File(FilePathManager.getSkinCurrentPath(this, pathName, currentStyleDirName));
//                        final Uri outputUri = Uri.fromFile(outputFile);
//                        startToCropActivity(getImgaeUriByNameAndCurrentDir(currentStyle.getImageName()), outputUri, pathName);
//                    }
//                }
                break;
            case R.string.style_main_check_path:
                isCheckedImg = true;
                goDifferentUiByImageType(currentStyle.getImageType(), null);
//                if (!TextUtils.isEmpty(currentStyle.getCheckImageName())) {
//                    final String pathName = getCurrentSkinPath();
//                    File outputFile = new File(FilePathManager.getSkinCurrentPath(this, pathName, currentStyleDirName));
//                    final Uri outputUri = Uri.fromFile(outputFile);
//                    startToCropActivity(getImgaeUriByNameAndCurrentDir(currentStyle.getCheckImageName()), outputUri, pathName);
//
//                }
                break;
//            case R.string.style_model_avatar:
//                if (position < HEAD_IDS.length) {
//                    currentStyle.setImageId(position);
//                    setAvatarmainItems(currentStyle, getPositionFromCurrentStyleid(currentStyleId));
//                    changeUiByConfigStyleId(currentStyle);
//                }
//                break;

            default:
                break;

        }
        mainAdapter.notifyDataSetChanged();
    }

    private void goDifferentUiByImageType(int imageType, String text) {
        switch (imageType) {
            case IMAGE_TYPE_LOCAL:
                startToPhotoActivity();
                break;
            case IMAGE_TYPE_INTERNET:
                gotoInternetInputPath();
                break;
            case IMAGE_TYPE_TEXT:
                gotoIconTextInput(text);
                break;
        }
    }

    private void setDrawerConfigChanged(Event.StyleDrawerEvent event) {
        final int position = event.getPosition();
        ImageStyle currentStyle = clonedImageStyles.get(currentStyleId);
        Log.d(TAG, "onConfigChanged() drawer returned: event.getStringId()" + event.getPosition());
        switch (event.getStatementId()) {
            case R.string.style_drawer_module:
                Log.d(TAG, "onConfigChanged() drawer returned: position" + position);
                setDrawerConfigItemAndStyleIdByPosition(position);
                break;
            case R.string.style_drawer_bg_type:
                isCheckedImg = false;
                if (position == 0) {
                    currentStyle.setImageType(IMAGE_TYPE_DEFAULT);
                    changeUiByConfigStyleId(currentStyle);
                } else if (position == 1) {
                    currentStyle.setImageType(IMAGE_TYPE_LOCAL);
                    if (!TextUtils.isEmpty(currentStyle.getImageUrl())) {
                        changeUiByConfigStyleId(currentStyle);
                    } else {
                        startToPhotoActivity();
                    }
                } else if (position == 2) {
                    currentStyle.setImageType(IMAGE_TYPE_INTERNET);
                    if (!TextUtils.isEmpty(currentStyle.getImageUrl())) {
                        changeUiByConfigStyleId(currentStyle);
                    } else {
                        gotoInternetInputPath();
                    }
                }
//                if (currentStyleId == IMAGE_CONTENT_AVATAR_RIGHT) {
//                    setAvatardrawerItems(currentStyle, getDrawerPositionFromCurrentStyleid(currentStyleId));
//                } else {
                setDrawerConfigItemById(currentStyleId, getDrawerPositionFromCurrentStyleid(currentStyleId));
//                }
                break;
            case R.string.style_drawer_bg_color:
//                isCheckedColor = false;
//                if (currentStyleId == BG_CHAT_DRAWER_SCRIM_RIGHT) {
//                    drawerItems.get(1).setPosition(position);
//                } else {
//                    drawerItems.get(2).setPosition(position);
//                }
                if (position == 0) {
                    setDefaultColorByStyleId();
                } else if (position < COLOR_STRINGS.length - 2) {
                    setColorChanged(getResources().getColor(COLOR_STRINGS[position]), COLOR_TYPE_NORMAL);
                } else if (position == COLOR_STRINGS.length - 2) {
                    int currentColor = -1;
                    if (!TextUtils.isEmpty(currentStyle.getMainColor()))
                        currentColor = ConvertUtils.HextoColor(currentStyle.getMainColor());
                    gotoColorConfig(currentColor, COLOR_TYPE_NORMAL);
                }
                setDrawerConfigItemById(currentStyleId, getDrawerPositionFromCurrentStyleid(currentStyleId));
                break;
            case R.string.style_drawer_bg_path:
                isCheckedImg = false;
                goDifferentUiByImageType(currentStyle.getImageType(), null);
                break;
            case R.string.style_drawer_img_text:
                gotoIconTextInput(currentStyle.getTextString());
                break;

            default:
                break;


        }
        drawerAdapter.notifyDataSetChanged();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onImageChanged(Event.ImageChangeEvent event) {
        Uri inputUri = event.getOrginalUri();

        String outputPath = event.getOutputPath();
        int styleId = event.getStyleid();
        Log.d(TAG, "onImageChanged() returned: inputUri" + inputUri.toString());
        if (inputUri != null && outputPath != null) {
            ImageStyle changeStyle = clonedImageStyles.get(styleId);
            changeStyle.setImageType(IMAGE_TYPE_LOCAL);
            if (isCheckedImg) {
//                changeStyle.setCheckImageName(outputName);
                changeStyle.setCheckUrl(outputPath);
                changeStyle.setCheckImgStatus(StyleConstants.STYLE_IMG_STATUS_CHANGE);
//                configItem5.setTextString(outputUri.toString());
//
                Log.d(TAG, "onImageChanged() returned:setCheckUrl " + outputPath);
            } else {
//                changeStyle.setImageName(outputName);
                changeStyle.setImageUrl(outputPath);
                changeStyle.setImgStatus(STYLE_IMG_STATUS_CHANGE);
//                if (!isDrawerConfig)
//                    setConfigItemById(currentStyleId,getPositionFromCurrentStyleid(styleId));
////                    configItem3.setTextString(outputUri.toString());
//                else {
//
////                    drawerConfigItem3.setTextString(outputUri.toString());
//                }
            }
            changeStyle.setMainColor("");
            changeUiByConfigStyleId(changeStyle);
            if (!isDrawerConfig) {
                setConfigItemById(currentStyleId, getPositionFromCurrentStyleid(styleId));
                mainAdapter.notifyDataSetChanged();
            } else {
                setDrawerConfigItemById(currentStyleId, getDrawerPositionFromCurrentStyleid(styleId));
                drawerAdapter.notifyDataSetChanged();
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onProgressChange(Event.SeekBarChangeEvent event) {
        int stringId = event.getStringId();
        int position = event.getPosition();
        ImageStyle currentStyle = clonedImageStyles.get(currentStyleId);
        if (currentStyle == null) return;

        switch (stringId) {
            case R.string.style_main_round_angel:
                currentStyle.setRadius(position);
                setConfigItemById(currentStyleId, getPositionFromCurrentStyleid(currentStyleId));
                changeUiByConfigStyleId(currentStyle);
                break;
            case R.string.style_drawer_width:
                int progress;
                if (position < drawerMinProgress) {
//                    position=drawerMinProgress;
                    progress = drawerMinProgress;
                } else {
                    progress = position;
                }
                currentStyle.setWidth(progress * 1.0f / 100);
                llLeftPercent.getLayoutParams().width = (int) (screenWidth * progress * 1.0f / 100);
                llRightPercent.getLayoutParams().width = screenWidth - (int) (screenWidth * progress * 1.0f / 100);
                llLeftPercent.requestLayout();
                setDrawerConfigItemById(currentStyleId, getDrawerPositionFromCurrentStyleid(currentStyleId));
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onChatBgChanged(Event.ChatImageBgChangeEvent event) {
        currentStyleId = BG_CHAT_CONTENT_RIGHT;
        ImageStyle currentStyle = clonedImageStyles.get(currentStyleId);
        currentStyle.setPaddingLeft(event.getValueShowLeft());
        currentStyle.setPaddingRight(event.getValueShowRight());
        currentStyle.setPaddingTop(event.getValueShowTop());
        currentStyle.setPaddingBottom(event.getValueShowBottom());
        Log.d(TAG, "onChatBgChanged() returned:event.getValueShowLeft() " + event.getValueShowLeft());
        Log.d(TAG, "onChatBgChanged() returned:event.event.getValueShowRight()() " + event.getValueShowRight());
        Log.d(TAG, "onChatBgChanged() returned:event.event.getValueShowTop()() " + event.getValueShowTop());
        Log.d(TAG, "onChatBgChanged() returned:event.event.getValueShowBottom()() " + event.getValueShowBottom());
        currentStyle.setStretchLeft(event.getValueStretchLeft());
        currentStyle.setStretchRight(event.getValueStretchRight());
        currentStyle.setStretchTop(event.getValueStretchTop());
        currentStyle.setStretchBottom(event.getValueStretchBottom());
        currentStyle.setWidth(event.getWidth());
        currentStyle.setHeight(event.getHeight());
        final String outputPath = FilePathManager.getSkinImgPath(this, getCurrentImgName(), getSkinDirName(skinData));
        File outputFile = new File(outputPath);
        Uri outputUri = Uri.fromFile(outputFile);
        Uri inputUri = Uri.fromFile(new File(event.getLocalPath()));
        EventBus.getDefault().post(new Event.ImageJustCopyEvent(currentStyleId, inputUri, outputUri, outputPath));


    }

    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void onCopyImage(Event.ImageJustCopyEvent event) {
        Uri inputUri = event.getOrginalUri();
        Uri outputUri = event.getCopyUri();
        int styleId = event.getStyleid();
        Log.d(TAG, "onImageChanged() returned: inputUri" + inputUri.toString());
        if (inputUri != null && outputUri != null) {
            saveOriginalPathToSkin(styleId, inputUri, outputUri);
//            changeUiByConfigStyleId(styleId);
//            adapter.notifyDataSetChanged();
        }
    }

    private void setDrawerConfigItemAndStyleIdByPosition(int position) {
        switch (position) {
            case 0:
                currentStyleId = BG_CHAT_DRAWER_RIGHT;
                break;
            case 1:
                currentStyleId = IMAGE_CHAT_DRAWER_VIDEO;
                break;
            case 2:
                currentStyleId = IMAGE_CHAT_DRAWER_CALL;
                break;
            case 3:
                currentStyleId = IMAGE_CHAT_DRAWER_SKIN;
                break;
            case 4:
                currentStyleId = IMAGE_CHAT_DRAWER_GRADE;
                break;
            case 5:
                currentStyleId = IMAGE_CHAT_DRAWER_FILE;
                break;
            case 6:
                currentStyleId = IMAGE_CHAT_DRAWER_PHOTO;
                break;
            case 7:
                currentStyleId = BG_CHAT_DRAWER_SCRIM_RIGHT;
                break;
            case 8:
                currentStyleId = BG_CHAT_DRAWER_TOP_RIGHT;
                break;
            case 9:
                currentStyleId = BG_CHAT_DRAWER_BOTTOM_RIGHT;
                break;
            default:
                currentStyleId = BG_CHAT_DRAWER_RIGHT;
                break;
        }
        if (position < StyleConstants.MODULE_STRINGS.length - 1)
            tvTittle.setText(StyleConstants.MODULE_STRINGS[position]);
        setCurrentStyleVisbleStatus();
        setDrawerConfigItemById(currentStyleId, position);
    }

    protected void setDrawerConfigItemById(int currentStyleId, int position) {
        ImageStyle imageStyle = clonedImageStyles.get(currentStyleId);
//            needBaseAlpha = false;
//            drawerConfigItem2.setStringId(R.string.style_drawer_bg_type);
        switch (currentStyleId) {
//            case IMAGE_CONTENT_AVATAR_RIGHT:
//                setAvatardrawerItems(imageStyle, position);
//                break;
            case BG_CHAT_DRAWER_SCRIM_RIGHT:
                setScrimdrawerItems(imageStyle, position);
                break;
            case BG_CHAT_DRAWER_RIGHT:
                setChatDrawerBgItems(imageStyle, position);
                break;
            case BG_CHAT_DRAWER_TOP_RIGHT:
            case BG_CHAT_DRAWER_BOTTOM_RIGHT:
            case IMAGE_CHAT_DRAWER_FILE:
            case IMAGE_CHAT_DRAWER_PHOTO:
            case IMAGE_CHAT_DRAWER_SKIN:
            case IMAGE_CHAT_DRAWER_GRADE:
            case IMAGE_CHAT_DRAWER_VIDEO:
            case IMAGE_CHAT_DRAWER_CALL:
                setNormaldrawerItems(imageStyle, position);
                break;

        }
//        setItemViewIsOut();
    }

    private void setChatDrawerBgItems(ImageStyle imageStyle, int position) {
        drawerItems.clear();
        drawerConfigItem1.setStringId(R.string.style_drawer_module);
        drawerConfigItem1.setPosition(position);
        drawerConfigItem2.setStringId(R.string.style_drawer_bg_type);
        int imgType = imageStyle.getImageType();
        drawerConfigItem2.setPosition(imgType);
        switch (imgType) {
            case IMAGE_TYPE_DEFAULT:
                drawerConfigItem3.setStringId(R.string.style_drawer_bg_color);
                drawerConfigItem3.setPosition(getColorPosition(imageStyle.getMainColor()));
                break;
            case IMAGE_TYPE_LOCAL:
            case IMAGE_TYPE_INTERNET:
                drawerConfigItem3.setStringId(R.string.style_drawer_bg_path);
                drawerConfigItem3.setTextString(imageStyle.getImageUrl());
                break;
        }
        drawerConfigItem4.setStringId(R.string.style_drawer_width);
        drawerConfigItem4.setPosition(Math.round(imageStyle.getWidth() * 100));
        adddrawerItems(4);
    }

    private void setNormaldrawerItems(ImageStyle imageStyle, int position) {
        drawerItems.clear();
        drawerConfigItem1.setStringId(R.string.style_drawer_module);
        drawerConfigItem1.setPosition(position);
        drawerConfigItem2.setStringId(R.string.style_drawer_bg_type);
        int imgType = imageStyle.getImageType();
        drawerConfigItem2.setPosition(imgType);
        switch (imgType) {
            case IMAGE_TYPE_DEFAULT:
                drawerConfigItem3.setStringId(R.string.style_drawer_bg_color);
                drawerConfigItem3.setPosition(getColorPosition(imageStyle.getMainColor()));
                break;
            case IMAGE_TYPE_LOCAL:
            case IMAGE_TYPE_INTERNET:
                drawerConfigItem3.setStringId(R.string.style_drawer_bg_path);
                drawerConfigItem3.setTextString(imageStyle.getImageUrl());
                break;
        }
        drawerConfigItem4.setStringId(R.string.style_drawer_img_text);
        drawerConfigItem4.setTextString(imageStyle.getTextString());
        adddrawerItems(4);
    }

//    private void setAvatardrawerItems(ImageStyle imageStyle, int position) {
//        drawerItems.clear();
//        drawerConfigItem1.setStringId(R.string.style_drawer_module);
//        drawerConfigItem1.setPosition(position);
//        drawerConfigItem2.setStringId(R.string.style_drawer_bg_type);
//        int imgType = imageStyle.getImageType();
//        drawerConfigItem2.setPosition(imgType);
//        switch (imgType) {
//            case IMAGE_TYPE_DEFAULT:
//                drawerConfigItem3.setStringId(R.string.style_model_avatar);
//                drawerConfigItem3.setPosition(imageStyle.getImageId());
//                break;
//            case IMAGE_TYPE_LOCAL:
//            case IMAGE_TYPE_INTERNET:
//                drawerConfigItem3.setStringId(R.string.style_drawer_bg_path);
//                drawerConfigItem3.setTextString(imageStyle.getImageUrl());
//                break;
//        }
//        adddrawerItems(3);
//    }

    private void setScrimdrawerItems(ImageStyle imageStyle, int position) {
        drawerItems.clear();
        drawerConfigItem1.setStringId(R.string.style_drawer_module);
        drawerConfigItem1.setPosition(position);
        drawerConfigItem2.setStringId(R.string.style_drawer_bg_color);
        drawerConfigItem2.setPosition(getColorPosition(imageStyle.getMainColor()));
        adddrawerItems(2);
    }

    private void setDefaultColorByStyleId() {
        ImageStyle currentStyle = clonedImageStyles.get(currentStyleId);
        currentStyle.setImageType(BG_TYPE_COLOR);
//        if (currentStyleId == BG_CHAT_DRAWER_RIGHT) {
//            currentStyle.setMainColor(DEF_DRAWER_BG_COLOR_RIGHT);
//        } else {
        currentStyle.setMainColor("");
//        }
        changeUiByConfigStyleId(currentStyle, true);

    }

    private void setDefaultCheckedColorByStyleId() {
        ImageStyle currentStyle = clonedImageStyles.get(currentStyleId);
        currentStyle.setCheckedColor("");
        changeUiByConfigStyleId(currentStyle, true);
    }

//    private void setColorChangByIsCheked(int color) {
//        if (isCheckedColor) setCheckedColorChanged(color);
//        else setColorChanged(color);
//    }

    protected void setColorChanged(int color, int type) {
        ImageStyle currentStyle = clonedImageStyles.get(currentStyleId);
        if (currentStyle == null) return;
        switch (type) {
            case COLOR_TYPE_NORMAL:
                currentStyle.setImageType(BG_TYPE_COLOR);
                currentStyle.setMainColor(ConvertUtils.ColorToHex(color));
                break;
            case COLOR_TYPE_CHECK:
                currentStyle.setCheckedColor(ConvertUtils.ColorToHex(color));
                break;
        }

        changeUiByConfigStyleId(currentStyle, true);
    }

    private void setCheckedColorChanged(int color) {
        ImageStyle currentStyle = clonedImageStyles.get(currentStyleId);
        if (currentStyle == null) return;
        currentStyle.setCheckedColor(ConvertUtils.ColorToHex(color));
        changeUiByConfigStyleId(currentStyle, true);
    }


    protected void setNoramlBgTypeChanged(ImageStyle currentStyle, int position) {
        if (position == 0) {
            currentStyle.setImageType(IMAGE_TYPE_DEFAULT);
            changeUiByConfigStyleId(currentStyle);
        } else if (position == 1) {
            currentStyle.setImageType(IMAGE_TYPE_LOCAL);
            if (!TextUtils.isEmpty(currentStyle.getImageUrl())) {
                changeUiByConfigStyleId(currentStyle);
            }
            startToPhotoActivity();
        } else if (position == 2) {
            currentStyle.setImageType(IMAGE_TYPE_INTERNET);
            if (!TextUtils.isEmpty(currentStyle.getImageUrl())) {
                changeUiByConfigStyleId(currentStyle);
            }
            gotoInternetInputPath();
        }
        isCheckedImg = false;
    }

    protected void setIconTypeChanged(ImageStyle currentStyle, int position) {
        if (position == 1) {
            currentStyle.setImageType(IMAGE_TYPE_LOCAL);
            if (!TextUtils.isEmpty(currentStyle.getImageUrl())) {
                changeUiByConfigStyleId(currentStyle);
            } else {
                startToPhotoActivity();
            }
        } else if (position == 2) {
            currentStyle.setImageType(IMAGE_TYPE_INTERNET);
            if (!TextUtils.isEmpty(currentStyle.getImageUrl())) {
                changeUiByConfigStyleId(currentStyle);
            } else {
                gotoInternetInputPath();
            }
        } else if (position == 3) {
            currentStyle.setImageType(IMAGE_TYPE_TEXT);
            if (!TextUtils.isEmpty(currentStyle.getTextString())) {
                changeUiByConfigStyleId(currentStyle);
            } else {
                gotoIconTextInput(currentStyle.getTextString());
            }
        } else {
            currentStyle.setImageType(IMAGE_TYPE_DEFAULT);
            changeUiByConfigStyleId(currentStyle);
        }
        isCheckedImg = false;
    }

    protected void setIconCheckTypeChanged(ImageStyle currentStyle, int position) {
        if (position == 1) {
            currentStyle.setTintMode(IMAGE_TINT_MODE_MUTIPLY);
            changeUiByConfigStyleId(currentStyle);
        } else if (position == 2) {
            currentStyle.setTintMode(IMAGE_TINT_MODE_IMG);
            if (currentStyle.getImageType() == IMAGE_TYPE_LOCAL) {
                startToPhotoActivity();
            } else if (currentStyle.getImageType() == IMAGE_TYPE_INTERNET)
                gotoInternetInputPath();
        } else {
            currentStyle.setTintMode(IMAGE_TINT_MODE_SRC_IN);
            changeUiByConfigStyleId(currentStyle);
        }
        isCheckedImg = true;
    }

//    protected void setContentParams() {
//        int contentHeight = screenHeight - DensityUtils.dip2px(this, 250);
//
////        int contentWidth = contentHeight * screenWidth / screenHeight;
//        scaleX = scaleY = contentHeight * 1.0f / screenHeight;
//        Log.d(TAG, "setContentParams() returned: scaleX " + scaleX);
//        llContent.setPivotX(screenWidth * 1.0f / 2);
//        llContent.setPivotY(screenHeight * 1.0f / 2);
//        llContent.setScaleX(scaleX);
//        llContent.setScaleY(scaleY);
//        float extraHeight = (screenHeight - contentHeight) / 2.0f - DensityUtils.dip2px(this, 50);
//        float extraBottomHeight = (screenHeight - contentHeight) / 2.0f + extraHeight;
//        float translationY = extraHeight - (extraBottomHeight - DensityUtils.dip2px(this, 190)) / 2.0f;
//        llContent.setTranslationY(-translationY);
////
//
////        float translationX = (screenWidth - contentWidth) / 2.0f;
////        llContent.getLayoutParams().height = contentHeight;
////        llContent.getLayoutParams().width = contentWidth;
////        llContent.setTranslationX(translationX);
//    }

    @Override
    protected void setUpExtraUi() {
        loadingDialog = (LinearLayout) findViewById(R.id.loading_dialog_all);
        loadingIv = (ImageView) findViewById(R.id.dialog_content_iv);
        loadingTv = (TextView) findViewById(R.id.dialog_content_tv);
    }

    protected void initMainmainItemsData() {
        //TODO 根据背景的Style来决定初始数据
        if (mainItems == null || mainItems.size() == 0) {
            mainItems = new ArrayList<>();
            configItem1 = new ConfigItem(R.string.style_main_module, 0);
            configItem2 = new ConfigItem(R.string.style_main_bg_type, 0);
            configItem3 = new ConfigItem(R.string.style_main_bg_color, 0);
            configItem4 = new ConfigItem(R.string.style_main_check_color, 0);
            configItem5 = new ConfigItem(R.string.style_main_check_path, 0);
//            mainItems.add(configItem1);
//            mainItems.add(configItem2);
//            mainItems.add(configItem3);
            setConfigItemById(currentStyleId, getPositionFromCurrentStyleid(currentStyleId));
        }

    }

    protected void initDrawerItemsData() {
        //TODO 根据背景的Style来决定初始数据
        if (drawerItems == null || drawerItems.size() == 0) {
            drawerItems = new ArrayList<>();
            drawerConfigItem1 = new ConfigItem(R.string.style_drawer_module, 0);
            drawerConfigItem2 = new ConfigItem(R.string.style_drawer_bg_type, 0);
            drawerConfigItem3 = new ConfigItem(R.string.style_drawer_bg_color, 0);
            float leftPercent = clonedImageStyles.get(BG_CHAT_DRAWER_RIGHT).getWidth();
            if (leftPercent < drawerMinPercent)
                leftPercent = drawerMinPercent;
            drawerConfigItem4 = new ConfigItem(R.string.style_drawer_width, Math.round(leftPercent * 100));
            Log.d(TAG, "initDrawerItemsData() returned: drawerConfigItem4" + drawerConfigItem4.getPosition());
            drawerConfigItem5 = new ConfigItem(R.string.style_drawer_bg_color, 0);
//            drawerItems.add(drawerConfigItem1);
//            drawerItems.add(drawerConfigItem2);
//            drawerItems.add(drawerConfigItem3);
//            drawerItems.add(drawerConfigItem4);
            setDrawerConfigItemById(currentStyleId, getDrawerPositionFromCurrentStyleid(currentStyleId));
            drawerAdapter = new ConfigPagerRvAdapter(drawerItems, this);

        }
    }

    private ImageStyle getCurrentStyle() {
        return clonedImageStyles.get(currentStyleId);
    }

    //    <string name="style_main_bg">背景</string>
//    <string name="style_main_chat_bubble">聊天气泡</string>
//    <string name="style_main_avatar">头像框</string>
//    <string name="style_main_item_1">语音</string>
//    <string name="style_main_item_2">表情</string>
//    <string name="style_main_item_3">相机</string>
//    <string name="style_main_item_4">相册</string>
//    <string name="style_main_item_5">录像</string>
//    <string name="style_main_item_6">游戏</string>
//    <string name="style_main_item_7">更多</string>
//    <string name="style_main_item_send">发送</string>
//    <string name="style_main_item_edit">输入框</string>
//    <string name="style_main_item_bottom_bg">底部背景</string>
    private void setConfigItemAndStyleIdByPosition(int position) {
        switch (position) {
            case 0:
                currentStyleId = BG_CHAT_CONTENT;
                break;
            case 1:
                currentStyleId = IMAGE_CONTENT_AVATAR_RIGHT;
                break;
            case 2:
                currentStyleId = BG_CHAT_CONTENT_RIGHT;
                break;
            case 3:
                currentStyleId = IMAGE_BOTTOM_VOICE;
                break;
            case 4:
                currentStyleId = IMAGE_BOTTOM_EMOJI;
                break;
            case 5:
                currentStyleId = IMAGE_BOTTOM_CAMERA;
                break;
            case 6:
                currentStyleId = IMAGE_BOTTOM_PHOTO;
                break;
            case 7:
                currentStyleId = IMAGE_BOTTOM_VIDEO;
                break;
            case 8:
                currentStyleId = IMAGE_BOTTOM_GAME;
                break;
            case 9:
                currentStyleId = IMAGE_BOTTOM_MORE;
                break;
            case 10:
                currentStyleId = IMAGE_EDIT_SEND;
                break;
            case 11:
                currentStyleId = BG_CHAT_EDITTEXT;
                break;
            case 12:
                currentStyleId = BG_CHAT_CONTENT_BOTTOM;
                break;
            case 13:
                currentStyleId = BG_CHAT_BOTTOM_BAR;
                break;
            default:
                currentStyleId = BG_CHAT_CONTENT;
                break;
        }
        if (position < StyleConstants.MODULE_STRINGS_MAIN.length - 1)
            tvTittle.setText(StyleConstants.MODULE_STRINGS_MAIN[position]);
        setCurrentStyleVisbleStatus();
        setConfigItemById(currentStyleId, position);
    }

    protected int getPositionFromCurrentStyleid(int styleId) {
        int position = 0;
        switch (styleId) {
            case BG_CHAT_CONTENT:
                position = 0;
                break;
            case IMAGE_CONTENT_AVATAR_RIGHT:
                position = 1;
                break;
            case BG_CHAT_CONTENT_RIGHT:
                position = 2;
                break;
            case IMAGE_BOTTOM_VOICE:
                position = 3;
                break;
            case IMAGE_BOTTOM_EMOJI:
                position = 4;
                break;
            case IMAGE_BOTTOM_CAMERA:
                position = 5;
                break;
            case IMAGE_BOTTOM_PHOTO:
                position = 6;
                break;
            case IMAGE_BOTTOM_VIDEO:
                position = 7;
                break;
            case IMAGE_BOTTOM_GAME:
                position = 8;
                break;
            case IMAGE_BOTTOM_MORE:
                position = 9;
                break;
            case IMAGE_EDIT_SEND:
                position = 10;
                break;
            case BG_CHAT_EDITTEXT:
                position = 11;
                break;
            case BG_CHAT_CONTENT_BOTTOM:
                position = 12;
                break;
            case BG_CHAT_BOTTOM_BAR:
                position = 13;
                break;
            default:
                position = 0;
                break;
        }
        return position;
    }

    protected int getDrawerPositionFromCurrentStyleid(int styleId) {

        int position = 0;
        switch (styleId) {
            case BG_CHAT_DRAWER_RIGHT:
                position = 0;
                break;
            case IMAGE_CHAT_DRAWER_VIDEO:
                position = 1;
                break;
            case IMAGE_CHAT_DRAWER_CALL:
                position = 2;
                break;
            case IMAGE_CHAT_DRAWER_SKIN:
                position = 3;
                break;
            case IMAGE_CHAT_DRAWER_GRADE:
                position = 4;
                break;
            case IMAGE_CHAT_DRAWER_FILE:
                position = 5;
                break;
            case IMAGE_CHAT_DRAWER_PHOTO:
                position = 6;
                break;
            case BG_CHAT_DRAWER_SCRIM_RIGHT:
                position = 7;
                break;
            case BG_CHAT_DRAWER_TOP_RIGHT:
                position = 8;
                break;
            case BG_CHAT_DRAWER_BOTTOM_RIGHT:
                position = 9;
                break;
            default:
                position = 0;
                break;
        }
        return position;
    }

    protected void setConfigItemById(int currentStyleId, int position) {
        ImageStyle imageStyle = clonedImageStyles.get(currentStyleId);
        if (imageStyle == null) return;
        switch (currentStyleId) {
            case BG_CHAT_CONTENT:
            case BG_CHAT_CONTENT_BOTTOM:
            case BG_CHAT_BOTTOM_BAR:
                setNormalmainItems(imageStyle, position);
                break;
            case IMAGE_BOTTOM_VOICE:
            case IMAGE_BOTTOM_EMOJI:
            case IMAGE_BOTTOM_CAMERA:
            case IMAGE_BOTTOM_PHOTO:
            case IMAGE_BOTTOM_VIDEO:
            case IMAGE_BOTTOM_GAME:
            case IMAGE_BOTTOM_MORE:
            case IMAGE_EDIT_SEND:
                setCheckIconmainItems(imageStyle, position);
                break;
            case BG_CHAT_EDITTEXT:
                setEditTextmainItems(imageStyle, position);
                break;
            case IMAGE_CONTENT_AVATAR_RIGHT:
                setAvatarmainItems(imageStyle, position);
                break;
            case BG_CHAT_CONTENT_RIGHT:
                Log.d(TAG, "setConfigItemById() returned: position " + position);
                setTextBgmainItems(imageStyle, position);
                break;
            default:
                break;
        }

    }

    private void setEditTextmainItems(ImageStyle imageStyle, int position) {
        mainItems.clear();
        configItem1.setStringId(R.string.style_main_module);
        configItem1.setPosition(position);
        configItem2.setStringId(R.string.style_main_bg_color);
        configItem2.setPosition(getColorPosition(imageStyle.getMainColor()));
        configItem3.setStringId(R.string.style_main_round_angel);
        configItem3.setPosition(imageStyle.getRadius());
        addmainItems(3);
    }

    private void setNormalmainItems(ImageStyle imageStyle, int position) {
        mainItems.clear();
        configItem1.setStringId(R.string.style_main_module);
        configItem1.setPosition(position);
        int imgType = imageStyle.getImageType();
        configItem2.setStringId(R.string.style_main_bg_type);
        configItem2.setPosition(imgType);
        switch (imgType) {
            case IMAGE_TYPE_DEFAULT:
                configItem3.setStringId(R.string.style_main_bg_color);
                Log.d(TAG, "setNormalmainItems() returned: imageStyle.getMainColor()" + imageStyle.getMainColor());
                Log.d(TAG, "setNormalmainItems() returned: position" + getColorPosition(imageStyle.getMainColor()));
                configItem3.setPosition(getColorPosition(imageStyle.getMainColor()));
                break;
            case IMAGE_TYPE_LOCAL:
                configItem3.setStringId(R.string.style_main_bg_path);
                if (TextUtils.isEmpty(imageStyle.getImageUrl())) {
                    configItem3.setTextString("");
                } else {
                    configItem3.setTextString(imageStyle.getImageUrl());
                }
                break;
            case IMAGE_TYPE_INTERNET:
                configItem3.setStringId(R.string.style_main_bg_path);
                configItem3.setTextString(imageStyle.getImageUrl());
                break;
        }
        addmainItems(3);
    }

    private void setAvatarmainItems(ImageStyle imageStyle, int position) {
        mainItems.clear();
        configItem1.setStringId(R.string.style_main_module);
        configItem1.setPosition(position);
        int shapeType = imageStyle.getShapeType();
        int imgType = imageStyle.getImageType();
//        configItem2.setStringId(R.string.style_main_bg_type);
//        configItem2.setPosition(imgType);
//        switch (imgType) {
//            case IMAGE_TYPE_DEFAULT:
//                configItem3.setStringId(R.string.style_model_avatar);
//                configItem3.setPosition(imageStyle.getImageId());
//                break;
//            case IMAGE_TYPE_LOCAL:
//            case IMAGE_TYPE_INTERNET:
//                configItem3.setStringId(R.string.style_main_bg_path);
//                configItem3.setTextString(imageStyle.getImageUrl());
//                break;
//        }
        configItem2.setStringId(R.string.style_main_avatar_shape);
        switch (shapeType) {
            case 0:
                configItem2.setPosition(0);
                addmainItems(2);
                break;
            case 1:
                configItem2.setPosition(1);
                configItem3.setStringId(R.string.style_main_round_angel);
                Log.d(TAG, "setAvatarmainItems() returned:imageStyle.getRadius() " + imageStyle.getRadius());
                configItem3.setPosition(imageStyle.getRadius());
                addmainItems(3);
                break;
        }


    }

    private void setTextBgmainItems(ImageStyle imageStyle, int position) {
        Log.d(TAG, "setTextBgmainItems() returned:position " + position);
        mainItems.clear();
        configItem1.setStringId(R.string.style_main_module);
        configItem1.setPosition(position);
        int imgType = imageStyle.getImageType();
        configItem2.setStringId(R.string.style_main_bg_type);
        if (imgType == 0) {
            int shapeType = imageStyle.getShapeType();
            configItem2.setPosition(0);
            configItem3.setStringId(R.string.style_main_bg_color);
            configItem3.setPosition(getColorPosition(imageStyle.getMainColor()));
            configItem4.setStringId(R.string.style_chat_bg_shape);
            configItem4.setPosition(shapeType);
            configItem5.setStringId(R.string.style_main_round_angel);
            Log.d(TAG, "setTextBgmainItems() returned:imageStyle.getRadius() " + imageStyle.getRadius());
            configItem5.setPosition(imageStyle.getRadius());
            addmainItems(5);
        } else if (imgType == 2) {
            configItem2.setPosition(2);
            configItem3.setStringId(R.string.style_main_bg_path);
            addmainItems(3);
        } else {
            configItem2.setPosition(1);
            configItem3.setStringId(R.string.style_main_bg_path);
            addmainItems(3);
        }
    }

    protected void setCheckIconmainItems(ImageStyle imageStyle, int position) {
        mainItems.clear();
        configItem1.setStringId(R.string.style_main_module);
        configItem1.setPosition(position);
        int imgType = imageStyle.getImageType();
        configItem2.setStringId(R.string.style_main_icon_type);
        configItem2.setPosition(imgType);
        boolean isAdded = false;
        switch (imgType) {
            case IMAGE_TYPE_DEFAULT:
                configItem3.setStringId(R.string.style_main_bg_color);
                configItem3.setPosition(getColorPosition(imageStyle.getMainColor()));
                configItem4.setStringId(R.string.style_main_check_color);
                configItem4.setPosition(getCheckIconColorPosition(imageStyle.getCheckedColor()));
                break;
            case IMAGE_TYPE_LOCAL:
                configItem3.setStringId(R.string.style_main_bg_path);
                if (TextUtils.isEmpty(imageStyle.getImageUrl())) {
                    configItem3.setTextString("");
                } else {
                    configItem3.setTextString(imageStyle.getImageUrl());
                }
                configItem4.setStringId(R.string.style_main_check_type);
                if (imageStyle.getTintMode() == 2) {
                    configItem4.setPosition(2);
                    configItem5.setStringId(R.string.style_main_check_path);
                    if (TextUtils.isEmpty(imageStyle.getCheckUrl()))
                        configItem5.setTextString("");
                    else
                        configItem5.setTextString(imageStyle.getCheckUrl());
                    addmainItems(5);
                    isAdded = true;
                    break;
                } else if (imageStyle.getTintMode() == 1) {
                    configItem4.setPosition(1);
                } else {
                    configItem4.setPosition(0);
                }
                configItem5.setStringId(R.string.style_main_check_color);
                configItem5.setPosition(getCheckIconColorPosition(imageStyle.getCheckedColor()));
                addmainItems(5);
                isAdded = true;
                break;
            case IMAGE_TYPE_INTERNET:
                configItem3.setStringId(R.string.style_main_bg_path);
                configItem3.setTextString(imageStyle.getImageUrl());
                configItem4.setStringId(R.string.style_main_check_type);
                if (imageStyle.getTintMode() == 2) {
                    configItem4.setPosition(2);
                    configItem5.setStringId(R.string.style_main_check_path);
                    configItem5.setTextString(imageStyle.getCheckUrl());
                    addmainItems(5);
                    isAdded = true;
                    break;
                } else if (imageStyle.getTintMode() == 1) {
                    configItem4.setPosition(1);
                } else {
                    configItem4.setPosition(0);
                }
                configItem5.setStringId(R.string.style_main_check_color);
                configItem5.setPosition(getCheckIconColorPosition(imageStyle.getCheckedColor()));
                addmainItems(5);
                isAdded = true;
                break;
            case IMAGE_TYPE_TEXT:
                configItem3.setStringId(R.string.style_main_text_content);
                configItem3.setTextString(imageStyle.getTextString());
                configItem4.setStringId(R.string.style_main_check_color);
                configItem4.setPosition(getCheckIconColorPosition(imageStyle.getCheckedColor()));
                break;
//            case IMAGE_TYPE_SVG:
//                configItem3.setStringId(R.string.style_main_svg_path);
//                configItem3.setTextString(imageStyle.getSvgPath());
//                configItem4.setStringId(R.string.style_main_check_color);
//                configItem4.setPosition(getCheckIconColorPosition(imageStyle.getCheckedColor()));
//                break;
        }
        if (!isAdded) addmainItems(4);
    }

    protected void addmainItems(int count) {
        switch (count) {
            case 2:
                mainItems.add(configItem1);
                mainItems.add(configItem2);
                break;
            case 3:
                addmainItems(2);
                mainItems.add(configItem3);
                break;
            case 4:
                addmainItems(3);
                mainItems.add(configItem4);
                break;
            case 5:
                addmainItems(4);
                mainItems.add(configItem5);
                break;
        }
    }

    protected void adddrawerItems(int count) {
        switch (count) {
            case 2:
                drawerItems.add(drawerConfigItem1);
                drawerItems.add(drawerConfigItem2);
                break;
            case 3:
                adddrawerItems(2);
                drawerItems.add(drawerConfigItem3);
                break;
            case 4:
                adddrawerItems(3);
                drawerItems.add(drawerConfigItem4);
                break;
            case 5:
                adddrawerItems(4);
                drawerItems.add(drawerConfigItem5);
                break;
        }
    }

    protected void changeUiByConfigStyleId(ImageStyle currentStyle) {
        changeUiByConfigStyleId(currentStyle, false);
    }

    protected void changeUiByConfigStyleId(ImageStyle currentStyle, boolean justColor) {
        if (currentStyle == null) return;
        int styleId = currentStyle.getId();
        switch (styleId) {
            case BG_CHAT_CONTENT:
                AppStyleManager.setChatContentBgConfigureDependOnStyle(chatBg, currentStyle, skinData.getThemeId());
                break;
            case BG_CHAT_CONTENT_BOTTOM:
                AppStyleManager.setChatBottomBgConfigureDependOnStyle(chatBottomBg, currentStyle, skinData.getThemeId());
                break;
            case BG_CHAT_BOTTOM_BAR:
                AppStyleManager.setBottomBarBgConfigureDependOnStyle(bottomBarBg, currentStyle, skinData.getThemeId());
                break;
            case BG_CHAT_EDITTEXT:
                if (justColor)
                    AppStyleManager.changeEditTextBgConfigureDependOnStyle(editTextBg, currentStyle, skinData.getThemeId());
                else
                    AppStyleManager.setChatEditTextBgConfigureDependOnStyle(editTextBg, currentStyle, skinData.getThemeId());
                break;
            case BG_CHAT_CONTENT_RIGHT:
                simpleAdapter.notifyDataSetChanged();
                break;
//            case IMAGE_CONTENT_AVATAR_RIGHT:
//                simpleAdapter.notifyDataSetChanged();
////                AppStyleManager.setChatAvatarConfigureDependOnStyle(chatHead, currentStyle);
//                break;
            case IMAGE_BOTTOM_VOICE:
                if (justColor)
                    AppStyleManager.changeImageViewColorDependOnStyle(svgVoice, currentStyle, skinData.getThemeId());
                else
                    AppStyleManager.setBottomSvgConfigureDependOnStyle(svgVoice, currentStyle, skinData.getThemeId());
                break;
            case IMAGE_BOTTOM_EMOJI:
                if (justColor)
                    AppStyleManager.changeImageViewColorDependOnStyle(svgEmoji, currentStyle, skinData.getThemeId());
                else
                    AppStyleManager.setBottomSvgConfigureDependOnStyle(svgEmoji, currentStyle, skinData.getThemeId());
                break;
            case IMAGE_BOTTOM_CAMERA:
                if (justColor)
                    AppStyleManager.changeImageViewColorDependOnStyle(svgCamera, currentStyle, skinData.getThemeId());
                else
                    AppStyleManager.setBottomSvgConfigureDependOnStyle(svgCamera, currentStyle, skinData.getThemeId());
                break;
            case IMAGE_BOTTOM_PHOTO:
                if (justColor)
                    AppStyleManager.changeImageViewColorDependOnStyle(svgPhoto, currentStyle, skinData.getThemeId());
                else
                    AppStyleManager.setBottomSvgConfigureDependOnStyle(svgPhoto, currentStyle, skinData.getThemeId());
                break;
            case IMAGE_BOTTOM_GAME:
                if (justColor)
                    AppStyleManager.changeImageViewColorDependOnStyle(svgGame, currentStyle, skinData.getThemeId());
                else
                    AppStyleManager.setBottomSvgConfigureDependOnStyle(svgGame, currentStyle, skinData.getThemeId());
                break;
            case IMAGE_BOTTOM_MORE:
                if (justColor)
                    AppStyleManager.changeImageViewColorDependOnStyle(svgMore, currentStyle, skinData.getThemeId());
                else
                    AppStyleManager.setBottomSvgConfigureDependOnStyle(svgMore, currentStyle, skinData.getThemeId());
                break;
            case IMAGE_BOTTOM_VIDEO:
                if (justColor)
                    AppStyleManager.changeImageViewColorDependOnStyle(svgVideo, currentStyle, skinData.getThemeId());
                else
                    AppStyleManager.setBottomSvgConfigureDependOnStyle(svgVideo, currentStyle, skinData.getThemeId());
                break;
            case IMAGE_EDIT_SEND:
                if (justColor)
                    AppStyleManager.changeImageViewColorDependOnStyle(ivSend, currentStyle, skinData.getThemeId());
                else
                    AppStyleManager.setImageViewConfigureDependOnStyle(ivSend, currentStyle, skinData.getThemeId());
                break;
            case BG_CHAT_DRAWER_RIGHT:
                AppStyleManager.setChatBottomBgConfigureDependOnStyle(llLeftPercent, currentStyle, skinData.getThemeId());
                break;
            case IMAGE_CONTENT_AVATAR_RIGHT:
//                AppStyleManager.setChatDrawerAvatarConfigureDependOnStyle(ivHead, currentStyle, getSkinDirName(skinData));
                simpleAdapter.notifyDataSetChanged();
                break;
            case IMAGE_CHAT_DRAWER_FILE:
                AppStyleManager.setDrawertextCongigureDependOnStyle(item5, currentStyle, skinData.getThemeId());
                break;
            case IMAGE_CHAT_DRAWER_PHOTO:
                AppStyleManager.setDrawertextCongigureDependOnStyle(item6, currentStyle, skinData.getThemeId());
                break;
            case IMAGE_CHAT_DRAWER_SKIN:
                AppStyleManager.setDrawertextCongigureDependOnStyle(item3, currentStyle, skinData.getThemeId());
                break;
            case IMAGE_CHAT_DRAWER_GRADE:
                AppStyleManager.setDrawertextCongigureDependOnStyle(item4, currentStyle, skinData.getThemeId());
                break;
            case IMAGE_CHAT_DRAWER_VIDEO:
                AppStyleManager.setDrawertextCongigureDependOnStyle(item1, currentStyle, skinData.getThemeId());
                break;
            case IMAGE_CHAT_DRAWER_CALL:
                AppStyleManager.setDrawertextCongigureDependOnStyle(item2, currentStyle, skinData.getThemeId());
                break;
            case BG_CHAT_DRAWER_SCRIM_RIGHT:
                AppStyleManager.setChatScrimBgConfigureDependOnStyle(llRightPercent, currentStyle, skinData.getThemeId());
                break;
            case BG_CHAT_DRAWER_TOP_RIGHT:
                AppStyleManager.setChatBottomBgConfigureDependOnStyle(llTop, currentStyle, skinData.getThemeId());
                break;
            case BG_CHAT_DRAWER_BOTTOM_RIGHT:
                AppStyleManager.setChatBottomBgConfigureDependOnStyle(llBottom, currentStyle, skinData.getThemeId());
                break;

        }
    }

//    @Override
//    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//        switch (seekBar.getId()) {
//            case R.id.adapter_pager_item_sb:
//                Log.d(TAG, "onProgressChanged() returned: adapter_pager_item_sb " + progress);
//                HSV[0] = 359 - progress;
//                currentColor = Color.HSVToColor(HSV);
//                currentColor = Color.argb(alpha, Color.red(currentColor), Color.green(currentColor), Color.blue(currentColor));
//                etHexValue.setText(ConvertUtils.ColorToHexSix(currentColor));
//                colorPanel.setColor(currentColor);
//                setColorChanged(currentColor, colorType);
//
//                break;
//            case R.id.adapter_pager_item_sb_sat:
//                Log.d(TAG, "onProgressChanged() returned: adapter_pager_item_sb_opacity " + progress);
//                HSV[1] = progress * 1.0f / 100;
//                currentColor = Color.HSVToColor(HSV);
//                currentColor = Color.argb(alpha, Color.red(currentColor), Color.green(currentColor), Color.blue(currentColor));
//                etHexValue.setText(ConvertUtils.ColorToHexSix(currentColor));
//                colorPanel.setColor(currentColor);
//                setColorChanged(currentColor, colorType);
//
////                HSV[0]=progress;
////                HSV[1]=1.0f;
////                HSV[2]=1.0f;
////                currentColor=Color.HSVToColor(HSV);
////                etHexValue.setText(ConvertUtils.ColorToHexSix(currentColor));
////                colorPanel.setColor(currentColor);
//                break;
//            case R.id.adapter_pager_item_sb_val:
//                Log.d(TAG, "onProgressChanged() returned: adapter_pager_item_sb_opacity " + progress);
//                HSV[2] = progress * 1.0f / 100;
//                currentColor = Color.HSVToColor(HSV);
//                currentColor = Color.argb(alpha, Color.red(currentColor), Color.green(currentColor), Color.blue(currentColor));
//                etHexValue.setText(ConvertUtils.ColorToHexSix(currentColor));
//                setColorChanged(currentColor, colorType);
//
////                HSV[0]=progress;
////                HSV[1]=1.0f;
////                HSV[2]=1.0f;
////                currentColor=Color.HSVToColor(HSV);
////                etHexValue.setText(ConvertUtils.ColorToHexSix(currentColor));
////                colorPanel.setColor(currentColor);
//
//                break;
//            case R.id.adapter_pager_item_sb_alpha:
//                alpha = 255 - progress;
//                Log.d(TAG, "onProgressChanged() returned: alpha" + alpha);
//                currentColor = Color.argb(alpha, Color.red(currentColor), Color.green(currentColor), Color.blue(currentColor));
//                etHexValue.setText(ConvertUtils.ColorToHexSix(currentColor));
//                colorPanel.setColor(currentColor);
//                setColorChanged(currentColor, colorType);
//                break;
//        }
//    }

    @Override
    protected void onColorChanged(int color) {
        setColorChanged(color, colorType);
    }

    protected void setSbCurrentColor(int color) {
        sbColorVal.setOnSeekBarChangeListener(null);
        sbColorSat.setOnSeekBarChangeListener(null);
        sbColor.setOnSeekBarChangeListener(null);
        sbColorAlpha.setOnSeekBarChangeListener(null);
        currentColor = color;
        etHexValue.setText(ConvertUtils.ColorToHexSix(color));
        colorPanel.setColor(color);
        Color.RGBToHSV(Color.red(color), Color.green(color), Color.blue(color), HSV);
        alpha = Color.alpha(color);
        sbColorAlpha.setProgress(255 - alpha);
        Log.d(TAG, "setSbCurrentColor() returned: Color.alpha(color)" + Color.alpha(color));
        sbColor.setProgress(359 - (int) HSV[0]);
        sbColorSat.setProgress((int) (HSV[1] * 100));
        sbColorVal.setProgress((int) (HSV[2] * 100));
        sbColorVal.setOnSeekBarChangeListener(this);
        sbColorSat.setOnSeekBarChangeListener(this);
        sbColor.setOnSeekBarChangeListener(this);
        sbColorAlpha.setOnSeekBarChangeListener(this);

    }


    protected void sendShowLoadingDialog() {
        mHandler.sendEmptyMessageDelayed(1, 500);
    }

    protected void sendCancelLoadingDialog() {
        mHandler.sendEmptyMessage(4);
    }

    protected void showLoadingDialog() {
        loadingDialog.setVisibility(View.VISIBLE);
        if (rotate == null) {
            rotate = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
        }
        loadingIv.setImageResource(R.drawable.loading_in);
        loadingIv.startAnimation(rotate);
        loadingTv.setText(R.string.dialog_loading);
    }

    protected void showLoadingFailDialog() {
        loadingDialog.setVisibility(View.VISIBLE);
        loadingIv.clearAnimation();
        loadingIv.setImageResource(R.drawable.loading_fail);
        loadingTv.setText(R.string.dialog_fail);
        sendCancelLoadingDialog();
        mHandler.sendEmptyMessageDelayed(3, 1000);

    }

    protected void showLoadingSuccessDialog() {
        loadingDialog.setVisibility(View.VISIBLE);
        loadingIv.clearAnimation();
        loadingIv.setImageResource(R.drawable.succ);
        loadingTv.setText(R.string.dialog_success);
        sendCancelLoadingDialog();
        mHandler.sendEmptyMessageDelayed(2, 500);
    }

    public void hideDialog() {
        loadingDialog.setVisibility(View.GONE);
    }

    protected void loadImageFromAddress(String loadPath) {
        if (loadPath == null) return;
        final String path = loadPath.replaceAll(" ", "");
        TLog.d("path", path);
        if (TextUtils.isEmpty(path)) {
            ToastMaker.showLongToast(R.string.img_load_right_address);
            return;
        }
//        final Uri originalUri = Uri.parse(path);
        final String outputPath = FilePathManager.getSkinImgPath(this, getCurrentImgName(), getSkinDirName(skinData));
        final File outputFile = new File(outputPath);
        final Uri outputUri = Uri.fromFile(outputFile);
        Uri localUri;
        boolean isExist = false;
        String existLocalPath = null;

        if (pathsContainer != null && pathsContainer.size() > 0) {
            for (ImagePath existPath : pathsContainer) {
                if (existPath.getOrginalPath().equals(path)) {
                    isExist = true;
                    existLocalPath = existPath.getLocalPath();
                    break;
                }
            }
        }
        if (isExist) {
            localUri = Uri.parse(existLocalPath);
            if (currentStyleId == BG_CHAT_CONTENT_RIGHT) {
                startToNinePatchactivity(existLocalPath);
            } else {
                startToCropActivity(localUri, outputUri, outputPath);
            }
        } else {
            sendShowLoadingDialog();
            final String tempFilePath = FilePathManager.getPathTempInter(this) + File.separator + getCurrentImgName();
            File tempFile = new File(tempFilePath);
            ImageLoaderManager.loadIconAndSaveInLocal(this, path, tempFile, new DownloadOnlyCallBack() {
                @Override
                public void onDownLoadSuccess(File tempFile) {
                    showLoadingSuccessDialog();
                    if (pathsContainer == null) {
                        pathsContainer = new ArrayList<>();
                    }
                    pathsContainer.add(new ImagePath(path, tempFile.getAbsolutePath()));
                    if (currentStyleId == BG_CHAT_CONTENT_RIGHT) {
                        startToNinePatchactivity(tempFilePath);
                    } else {
                        startToCropActivity(Uri.fromFile(tempFile), outputUri, outputPath);
                    }
                    enableButtonClick();
                }

                @Override
                public void onFailure() {
                    Log.d(TAG, "onFailure() returned: " + "onFailure");
                    showLoadingFailDialog();
                    enableButtonClick();
                }

                @Override
                public void onDownLoading() {
                    mHandler.sendEmptyMessage(8);
                }
            });
        }
    }

    private void saveOriginalPathToSkin(int styleId, final Uri inputUri, final Uri outputUri) {
        if (FileUtils.copyFile(new File(inputUri.getPath()), new File(outputUri.getPath()))) {
            Message msg = Message.obtain();
            msg.arg1 = styleId;
            msg.what = 5;
            ImageStyle changeStyle = clonedImageStyles.get(styleId);
            changeStyle.setImageType(IMAGE_TYPE_LOCAL);
            if (isCheckedImg) {
                changeStyle.setCheckUrl(outputUri.toString());
            } else {
                changeStyle.setImageUrl(outputUri.toString());
            }
            configItem3.setTextString(outputUri.toString());
            mHandler.sendMessage(msg);
        } else {
            Message msg = Message.obtain();
            msg.arg1 = styleId;
            msg.what = 6;
            mHandler.sendMessage(msg);
        }
    }

    protected void startToCropActivity(Uri inputUri, Uri outputUri, String outputPath) {
        Intent intent = new Intent(this, UCropActivity.class);
        Bundle bundle = new Bundle();
        bundle.putFloat(UCrop.EXTRA_ASPECT_RATIO_X, getAspectByCurrentId());
        bundle.putFloat(UCrop.EXTRA_ASPECT_RATIO_Y, 1.0f);
        bundle.putParcelable(UCrop.EXTRA_OUTPUT_URI, outputUri);
        bundle.putParcelable(UCrop.EXTRA_INPUT_URI, inputUri);
        bundle.putString(UCrop.EXTRA_OUTPUT_PATH, outputPath);
        intent.putExtras(bundle);
        startActivityForResult(intent, 1);
    }

    private void startToNinePatchactivity(String path) {
        Intent intent = new Intent(this, NinePatchActivity.class);
        intent.putExtra(StyleConfigureConstants.CONFIGURE_STYLE_ID, currentStyleId);
        intent.putExtra(StyleConfigureConstants.CONFIGURE_LOCAL_PATH, path);
        startActivity(intent);
    }

    private void startToNinePatchactivityUpdate(ImageStyle updateImageStyle) {
        Intent intent = new Intent(this, NinePatchActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean(StyleConfigureConstants.CONFIGURE_UPDATE, true);
        bundle.putInt(StyleConfigureConstants.CONFIGURE_PRE_SHOW_B, updateImageStyle.getPaddingBottom());
        bundle.putInt(StyleConfigureConstants.CONFIGURE_PRE_SHOW_L, updateImageStyle.getPaddingLeft());
        bundle.putInt(StyleConfigureConstants.CONFIGURE_PRE_SHOW_T, updateImageStyle.getPaddingTop());
        bundle.putInt(StyleConfigureConstants.CONFIGURE_PRE_SHOW_R, updateImageStyle.getPaddingRight());
        bundle.putInt(StyleConfigureConstants.CONFIGURE_PRE_STRETCH_B, updateImageStyle.getStretchBottom());
        bundle.putInt(StyleConfigureConstants.CONFIGURE_PRE_STRETCH_L, updateImageStyle.getStretchLeft());
        bundle.putInt(StyleConfigureConstants.CONFIGURE_PRE_STRETCH_T, updateImageStyle.getStretchTop());
        bundle.putInt(StyleConfigureConstants.CONFIGURE_PRE_STRETCH_R, updateImageStyle.getStretchRight());
        bundle.putInt(StyleConfigureConstants.CONFIGURE_STYLE_ID, currentStyleId);
        bundle.putString(StyleConfigureConstants.CONFIGURE_LOCAL_PATH, updateImageStyle.getImageUrl());
        Log.d(TAG, "startToNinePatchactivityUpdate() returned:updateImageStyle.getLocalUrl() " + updateImageStyle.getImageUrl());
        intent.putExtras(bundle);
        startActivity(intent);
    }

// TODO: 17/11/2  记得在每个界面处理back健

    protected void startToPhotoActivity() {
        Bundle bundle = new Bundle();
        bundle.putBoolean(EXTRA_SHOW_GIF, true);
        bundle.putBoolean(EXTRA_SELECTED_ENABLED, false);
        bundle.putBoolean(EXTRA_SHOW_BOTTOM, false);
        bundle.putBoolean(StyleConfigureConstants.CONFIGURE_CROP, true);
        bundle.putFloat(StyleConfigureConstants.CONFIGURE_ASPECT, getAspectByCurrentId());
        bundle.putString(StyleConfigureConstants.CONFIGURE_OUTPUTPATH, FilePathManager.getSkinImgPath(this, getCurrentImgName(), getSkinDirName(skinData)));
//        bundle.putString(StyleConfigureConstants.CONFIGURE_STYLEDIRNAME, getSkinDirName(skinData));
        bundle.putInt(StyleConfigureConstants.CONFIGURE_STYLE_ID, currentStyleId);
        readyGo(PhotoPickerActivity.class, bundle);
        overridePendingTransition(R.anim.window_out, R.anim.anim_none);
    }

    protected void gotoColorConfig(int currentColor, int type) {
        viewPager.setCurrentItem(1);
        llColorConfig.setVisibility(View.VISIBLE);
        etHexValue.setVisibility(View.VISIBLE);
        colorPanel.setVisibility(View.VISIBLE);
        tvPathSure.setVisibility(View.GONE);
        llInputPath.setVisibility(View.GONE);
        llInputText.setVisibility(View.GONE);
//        llInputSvg.setVisibility(View.GONE);
        colorType = type;
        if (currentColor != -1)
            setSbCurrentColor(currentColor);
        else if (currentStyleId == BG_CHAT_DRAWER_SCRIM_RIGHT) {
            setSbCurrentColor(StyleConstants.DEF_DRAWER_SCRIM_COLOR);
        }
    }

    protected void gotoInternetInputPath() {
        viewPager.setCurrentItem(1);
        llColorConfig.setVisibility(View.GONE);
        etHexValue.setVisibility(View.GONE);
        colorPanel.setVisibility(View.GONE);
        llInputText.setVisibility(View.GONE);
//        llInputSvg.setVisibility(View.GONE);
        tvPathSure.setVisibility(View.VISIBLE);
        llInputPath.setVisibility(View.VISIBLE);
        currentPageType = 0;
    }

    private void gotoIconTextInput(String text) {
        viewPager.setCurrentItem(1);
        llColorConfig.setVisibility(View.GONE);
        etHexValue.setVisibility(View.GONE);
        colorPanel.setVisibility(View.GONE);
        tvPathSure.setVisibility(View.VISIBLE);
        llInputPath.setVisibility(View.GONE);
        llInputText.setVisibility(View.VISIBLE);
//        llInputSvg.setVisibility(View.GONE);
        etInputText.setText(text);
        currentPageType = 1;
    }


    protected float getAspectByCurrentId() {
        float aspect;
        if (currentStyleId == BG_CHAT_CONTENT) {
            aspect = screenWidth * 1.0f / screenHeight;
            if (aspect == 1) aspect = 1.001f;
        } else if (currentStyleId == BG_CHAT_CONTENT_BOTTOM) {
            aspect = screenWidth * 1.0f / chatBottomBg.getMeasuredHeight();
            if (aspect == 1) aspect = 1.001f;
        } else if (currentStyleId == BG_CHAT_BOTTOM_BAR) {
            aspect = screenWidth * 1.0f / DensityUtils.dip2px(this, 50);
            if (aspect == 1) aspect = 1.001f;
        } else if (currentStyleId == BG_CHAT_DRAWER_RIGHT) {
            aspect = clonedImageStyles.get(BG_CHAT_DRAWER_RIGHT).getWidth() * DensityUtils.getDisplayWidth(this) * 1.0f / DensityUtils.getDisplayHeight(this);
            if (aspect == 1) aspect = 1.001f;
        } else if (currentStyleId == BG_CHAT_DRAWER_BOTTOM_RIGHT) {
            aspect = clonedImageStyles.get(BG_CHAT_DRAWER_RIGHT).getWidth() * DensityUtils.getDisplayWidth(this) * 1.0f / llBottom.getMeasuredHeight();
            if (aspect == 1) aspect = 1.001f;
        } else if (currentStyleId == BG_CHAT_DRAWER_TOP_RIGHT) {
            aspect = clonedImageStyles.get(BG_CHAT_DRAWER_RIGHT).getWidth() * DensityUtils.getDisplayWidth(this) * 1.0f / llTop.getMeasuredHeight();
            if (aspect == 1) aspect = 1.001f;
        } else {
            aspect = 1;
        }
        return aspect;
    }

    public void copyImageFailed(int resId, int styleId) {
        clonedImageStyles.get(styleId).setImageType(IMAGE_TYPE_DEFAULT);
        changeUiByConfigStyleId(clonedImageStyles.get(styleId));
        ToastMaker.showShortToast(resId);
    }

    protected void copyImageSuccess(int styleId) {
        changeUiByConfigStyleId(clonedImageStyles.get(styleId));
        mainAdapter.notifyDataSetChanged();
    }

    protected void unableButtonClick() {
        ivDelete.setEnabled(false);
        ivPagerBack.setEnabled(false);
        tvPathSure.setEnabled(false);
    }

    protected void enableButtonClick() {
        ivDelete.setEnabled(true);
        ivPagerBack.setEnabled(true);
        tvPathSure.setEnabled(true);
    }

    private static class CloseDialogHandler extends Handler {
        private final WeakReference<StyleMainConfigActivity> mActivity;

        public CloseDialogHandler(StyleMainConfigActivity activity) {
            mActivity = new WeakReference<StyleMainConfigActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            if (mActivity.get() == null) {
                return;
            }
            switch (msg.what) {
                case 1:
                    mActivity.get().showLoadingDialog();
                    break;
                case 2:
                    mActivity.get().hideDialog();
                    break;
                case 3:
                    mActivity.get().hideDialog();
                    break;
                case 4:
                    removeMessages(1);
                    break;
                case 5:
                    mActivity.get().copyImageSuccess(msg.arg1);
                    break;
                case 6:
                    mActivity.get().copyImageFailed(R.string.dialog_fail, msg.arg1);
                    break;
                case 7:
                    mActivity.get().finish();
                    break;
                case 8:
                    mActivity.get().unableButtonClick();
                    break;
            }


        }
    }

//    protected String getCurrentSkinPath() {
//        return TimeUtils.milliseconds2String(System.currentTimeMillis(), TimeUtils.ORIGINAL_SDF) + "_" + screenWidth + "x" + screenHeight + ".jpeg";
//    }

//    private String getCurrentImgName() {
//        StringBuilder imgName = new StringBuilder();
//        imgName.append(identifier);
//        imgName.append(String.valueOf(System.currentTimeMillis()));
//        return "i" + EncryptUtils.encryptMD5(imgName.toString()) + ".jpeg";
//    }


    protected int getColorPosition(String mainColor) {
        if (TextUtils.isEmpty(mainColor)) return 0;
        for (int i = 0; i < COLOR_STRINGS.length - 2; i++) {
            String color = ConvertUtils.ColorToHex(getResources().getColor(COLOR_STRINGS[i]));
            if (color.equals(mainColor)) {
                return i;
            }
        }
        return COLOR_STRINGS.length - 2;
    }

    protected int getCheckIconColorPosition(String mainColor) {
        if (TextUtils.isEmpty(mainColor)) return 0;
//        if (mainColor.equals(DEF_SVG_IMAGE_CHECKED)) return 0;
        for (int i = 0; i < COLOR_STRINGS.length - 2; i++) {
            String color = ConvertUtils.ColorToHex(getResources().getColor(COLOR_STRINGS[i]));
            if (color.equals(mainColor)) {
                return i;
            }
        }
        return COLOR_STRINGS.length - 2;
    }

    protected void setCurrentStyleVisbleStatus() {
        boolean visible = clonedImageStyles.get(currentStyleId).getVisible() == 0;
        if (visible) tvMenu2.setText(R.string.style_visible_gone);
        else tvMenu2.setText(R.string.style_visible);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_pager_back2:
                backFirstPager();
                break;
            case R.id.iv_et_delete:
                etInputPath.setText("");
                break;
            case R.id.tv_input_sure:
                if (currentPageType == 0) {
                    loadImageFromAddress(etInputPath.getText().toString());
                } else if (currentPageType == 1) {
                    hideSoftInputKeyBoard(v);
                    if (getCurrentStyle() != null) {
                        if (!isDrawerConfig) getCurrentStyle().setImageType(IMAGE_TYPE_TEXT);

                        getCurrentStyle().setTextString(etInputText.getText().toString());
                        changeUiByConfigStyleId(getCurrentStyle());
                    }
                }
                break;
            case R.id.title_right_two:
                boolean visible = clonedImageStyles.get(currentStyleId).getVisible() == 0;
                if (visible) {
                    clonedImageStyles.get(currentStyleId).setVisible(1);
                    tvMenu2.setText(R.string.style_visible);
                } else {
                    clonedImageStyles.get(currentStyleId).setVisible(0);
                    tvMenu2.setText(R.string.style_visible_gone);
                }
                changeUiByConfigStyleId(clonedImageStyles.get(currentStyleId));
                break;
            case R.id.title_right_one:
                if (isChange()) {
                    skinData.setSkinStyle(clonedImageStylesList);
                    new SaveSkinAsyncTask(this, needDeleteImgUrlSet(true), skinData, originalSkin).execute();
                } else {
                    TLog.d(TAG, "nochange need delete extra ");
                }
                break;
//            case R.id.voice:
//                Log.d(TAG, "onClick() returned: voice clicked" );
//                svgVoice.toggle();
//                break;
//            case R.id.adapter_config_pager_main_tv:
//                if (!isDrawerConfig) break;
//                isDrawerConfig = false;
//                lastDrawerStyleid = currentStyleId;
//                tvMainUi.setChecked(true);
//                tvDrawerUi.setChecked(false);
//
//                setCurrentMainUiOrDrawerUi();
//                break;
//            case R.id.adapter_config_pager_drawer_tv:
//                if (isDrawerConfig) break;
//                isDrawerConfig = true;
//                lastMainStyleid = currentStyleId;
//                tvMainUi.setChecked(false);
//                tvDrawerUi.setChecked(true);
//                setCurrentMainUiOrDrawerUi();
//                break;
        }

    }

    private void setCurrentMainUiOrDrawerUi() {
        if (isDrawerConfig) {
//            llLeftPercent.setVisibility(View.VISIBLE);
//            llRightPercent.setVisibility(View.VISIBLE);
//            overScrollView.setVisibility(View.VISIBLE);
            if (lastDrawerStyleid != 0)
                currentStyleId = lastDrawerStyleid;
            else
                currentStyleId = BG_CHAT_DRAWER_RIGHT;
            initDrawerItemsData();
//            initDrawerStyleConfigure();
            runEnterAnimation(redrawerAll, llMainAll);
//            drawerAdapter.notifyDataSetChanged();
            rvPager.setAdapter(drawerAdapter);
            rvPager.setLayoutManager(drawerLayoutManager);

            if (getDrawerPositionFromCurrentStyleid(currentStyleId) < StyleConstants.MODULE_STRINGS.length - 1)
                tvTittle.setText(StyleConstants.MODULE_STRINGS[getDrawerPositionFromCurrentStyleid(currentStyleId)]);
//            drawerAdapter.notifyDataSetChanged();
        } else {
            if (lastMainStyleid != 0)
                currentStyleId = lastMainStyleid;
            else
                currentStyleId = BG_CHAT_CONTENT;
            initMainmainItemsData();
//            initMainStyleConfigure();
//            llLeftPercent.setVisibility(View.INVISIBLE);
//            llRightPercent.setVisibility(View.INVISIBLE);
//            mainAdapter.notifyDataSetChanged();
            runBackAnimation(redrawerAll, llMainAll);
            rvPager.setAdapter(mainAdapter);
            rvPager.setLayoutManager(mainLayoutManager);

            if (getPositionFromCurrentStyleid(currentStyleId) < StyleConstants.MODULE_STRINGS_MAIN.length - 1)
                tvTittle.setText(StyleConstants.MODULE_STRINGS_MAIN[getPositionFromCurrentStyleid(currentStyleId)]);
//            viewPager.getAdapter().notifyDataSetChanged();
        }
    }

    private void backFirstPager() {
        viewPager.setCurrentItem(0);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        mHandler.removeCallbacksAndMessages(null);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Log.d(TAG, "onActivityResult() returned: requestCode" + requestCode);
            if (requestCode == 1) {
                Uri outputUri = data.getParcelableExtra(UCrop.EXTRA_OUTPUT_URI);
                Uri inputUri = data.getParcelableExtra(UCrop.EXTRA_INPUT_URI);
                String outputPath = data.getStringExtra(UCrop.EXTRA_OUTPUT_PATH);
//                String outputUri = data.getStringExtra(UCrop.EXTRA_OUTPUT_URI);
//                Log.d(TAG, "onActivityResult() returned:uri " + uri.toString());
                Log.d(TAG, "onActivityResult() returned:inputUri " + inputUri.toString());
                Log.d(TAG, "onActivityResult() returned:outputName " + outputUri.toString());
                if (outputUri != null && inputUri != null && !TextUtils.isEmpty(outputPath)) {
                    Log.d(TAG, "onActivityResult() returned: currentStyleId " + currentStyleId);
                    ImageStyle changeStyle = clonedImageStyles.get(currentStyleId);
                    changeStyle.setImageType(IMAGE_TYPE_INTERNET);
                    if (!isCheckedImg) {
//                        changeStyle.setLocalUrl(uri.toString());
                        changeStyle.setImageUrl(outputPath);
//                        changeStyle.setImageName(outputName);
                        changeStyle.setImgStatus(STYLE_IMG_STATUS_CHANGE);
//                        if (!isDrawerConfig)
//                            configItem3.setTextString(FilePathManager.getSkinImgPath(this, outputName, getSkinDirName()));
//                        else {
//                            drawerConfigItem3.setTextString(FilePathManager.getSkinImgPath(this, outputName, getSkinDirName()));
//                        }
                    } else {
                        changeStyle.setCheckUrl(outputPath);
//                        changeStyle.setCheckImageName(outputName);
                        changeStyle.setCheckImgStatus(STYLE_IMG_STATUS_CHANGE);
//                        configItem5.setTextString(FilePathManager.getSkinImgPath(this, outputName, getSkinDirName()));
                    }
                    changeStyle.setMainColor("");
                    changeUiByConfigStyleId(changeStyle);
                    if (!isDrawerConfig) {
                        setConfigItemById(currentStyleId, getPositionFromCurrentStyleid(currentStyleId));
                        mainAdapter.notifyDataSetChanged();
                    } else {
                        setDrawerConfigItemById(currentStyleId, getDrawerPositionFromCurrentStyleid(currentStyleId));
                        drawerAdapter.notifyDataSetChanged();
                    }
//                    EventBus.getDefault().post(new Event.ImageChangeEvent(styleId, inputUri, uri, true, outputName));
//                    getActivity().finish();
                }
            }
        } else if (resultCode == UCrop.RESULT_ERROR) {

            if (currentStyleId == IMAGE_CONTENT_AVATAR_RIGHT || currentStyleId == BG_CHAT_CONTENT) {
                Uri inputUri = data.getParcelableExtra(UCrop.EXTRA_INPUT_URI);
                if (inputUri != null) {
                    showSnackBar(llContent, inputUri, currentStyleId, this);
                }
            } else {
                showToast(R.string.img_crop_wrong);
            }
        }
    }

    protected void showSnackBar(View view, final Uri inputUri, final int styleId, final Context context) {
        Snackbar.make(view, getResources().getText(R.string.img_crop_use_original), Snackbar.LENGTH_LONG)
                .setAction(getResources().getText(R.string.dialog_sure), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String outputPath = FilePathManager.getSkinImgPath(context, getCurrentImgName(), getSkinDirName(skinData));
                        File outputFile = new File(outputPath);
                        Uri outputUri = Uri.fromFile(outputFile);
                        EventBus.getDefault().post(new Event.ImageJustCopyEvent(styleId, inputUri, outputUri, outputPath));
                    }
                }).setActionTextColor(getResources().getColor(R.color.choice_color))
                .show();
    }

//    protected String getImgaePathByNameAndCurrentDir(String pathName) {
//        File imgFile = new File(FilePathManager.getSkinCurrentPath(this, pathName, currentStyleDirName));
//        Uri imgUri = Uri.fromFile(imgFile);
//        return imgUri.toString();
//    }

//    protected Uri getImgaeUriByNameAndCurrentDir(String pathName) {
//        File imgFile = new File(FilePathManager.getSkinCurrentPath(this, pathName, currentStyleDirName));
//        Uri imgUri = Uri.fromFile(imgFile);
//        return imgUri;
//    }


//    private void runAlphaInAnimation(View view) {
////        final int viewWidth = view.getMeasuredHeight();
//        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 0.0f, 1.0f);
//        animator.setInterpolator(new DecelerateInterpolator(3.f));
//        animator.setDuration(200);
//        animator.start();
//    }


    private boolean isChange() {
        for (int i = StyleConstants.IMAGE_STYLE_ID_BEGIN; i <= IMAGE_STYLE_ID_OVER; i++) {

            if (skinData != null) {
                ImageStyle lStyle = clonedImageStyles.get(i);
                ImageStyle rStyle = originalImageStyles.get(i);
                if (lStyle == null || rStyle == null) continue;
                if (!lStyle.equals(rStyle)) {
                    TLog.d("chage", lStyle.getId() + "");
                    return true;
                }
            }
        }
        return false;
    }

//    private void saveCurrentSkin() {
//        if (isChange()) {
//
//        }
//    }

//    private String getSkinDirName(SkinData skinData) {
//        if (TextUtils.isEmpty(skinData.getSkinDirName())) {
//            long currentTimeMillis = System.currentTimeMillis();
//            SimpleDateFormat dirSdf = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
//            TLog.d(TAG, "dirName" + TimeUtils.milliseconds2String(currentTimeMillis, dirSdf));
//            skinData.setSkinDirName(TimeUtils.milliseconds2String(currentTimeMillis, dirSdf));
//        }
//        return skinData.getSkinDirName();
//    }

//    private String getSkinFileName(SkinData skinData) {
//        if (TextUtils.isEmpty(skinData.getSkinFileName())) {
//            skinData.setSkinFileName(getCurrentFileName());
//        }
//        return skinData.getSkinFileName();
//    }

//    //Todo setAuthorname by personnalprofile
//    private String getSkinAutorName(SkinData skinData) {
//        if (TextUtils.isEmpty(skinData.getSkinAuthorName())) {
//            skinData.setSkinAuthorName(UserProfileManager.getInstance(this).getUserProfile().getNickName());
//        }
//        return skinData.getSkinAuthorName();
//    }
//
//    private String getSkinIdentifier(SkinData skinData) {
//        if (TextUtils.isEmpty(skinData.getSkinAuthorIdentifier())) {
//            skinData.setSkinAuthorIdentifier(UserProfileManager.getInstance(this).getUserProfile().getAccount());
//        }
//        return skinData.getSkinAuthorIdentifier();
//    }

    private HashSet<String> needDeleteImgUrlSet(boolean needSave) {
        if (needSave) {
            HashSet<String> extraImgNames = new HashSet<>();
            HashSet<String> allImgNames = AppStyleManager.getAllImgUrlByTheSkinDir(this, getSkinDirName(skinData));
            HashSet<String> referImgNames = AppStyleManager.getReferImgUrlFromSkin(clonedImageStyles, originalSkin);
            for (String s : allImgNames) {
                if (!referImgNames.contains(s)) {
                    extraImgNames.add(s);
                }
            }
            return extraImgNames;
        } else {
            return AppStyleManager.getChangeImgUrlFromSkin(clonedImageStyles, skinData);

        }
    }

//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onUpdateComplete(DatabaseEvent.PostIntEvent event) {
//        if (event.getToken() == AppSkinManager.TOKEN_UPDATE_SKIN) {
////            pbProgress.setProgress(100);
//            int skinLocalId = event.getResult();
//            Log.d(TAG, "onUpdateComplete() returned: skinLocalId" + skinLocalId);
////            tvSave.setText(R.string.saving_success);
//            AppSkinManager.getInstance(this).startQuerySkinByLocalId(skinLocalId);
////
//        }
//    }
//
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onQueryUpdatedSkinComplete(DatabaseEvent.SingleSkinEvent event) {
//        if (event.getToken() == AppSkinManager.TOKEN_QUERY_SKIN_BY_ID) {
//            pbProgress.setProgress(100);
//            Skin newSkin = event.getSkin();
//            tvSave.setText(R.string.saving_success);
//            EventBus.getDefault().post(new DatabaseEvent.PostSkinUpdateEvent(newSkin));
//
//            mHandler.sendEmptyMessageDelayed(7, 300);
//
////            mHandler.sendEmptyMessageDelayed(7,300);
//
//        }
//    }

    private class SaveSkinAsyncTask extends AsyncTask<Void, Integer, Skin> {
        private Bitmap thumbMainBitmap;
        private Bitmap thumbDrawerBitmap;
        private HashSet<String> needDeleteImgPaths;
        private SkinData needSaveSkinData;
        private Context context;
        private Skin orginalSkin;

        public SaveSkinAsyncTask(Context context, HashSet<String> needDeleteImgPaths, SkinData skinData, Skin orginalSkin) {
            this.needDeleteImgPaths = needDeleteImgPaths;
            this.context = context;
            needSaveSkinData = skinData;
            this.orginalSkin = orginalSkin;

        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            try {
                layoutSave.setVisibility(View.VISIBLE);
                getBitMapfromContent(thumbMainBitmap, thumbDrawerBitmap);
            } catch (Exception e) {
                onPostExecute(null);
            }

        }

        @Override
        protected Skin doInBackground(Void... params) {

            try {
                publishProgress(0);
                //第一步 删除文件夹无用图片
                if (needDeleteImgPaths != null && needDeleteImgPaths.size() > 0) {
                    for (String imgPath : needDeleteImgPaths) {
                        File imgFile = new File(imgPath);
                        if (imgFile.exists()) {
                            imgFile.delete();
                        }
                    }
                }
                publishProgress(20);
                Skin skin = new Skin();
                //第二步 保存main图片
                String thumbMainPath = getSkinThumbMainPath(orginalSkin);
                String thumbMainNewPath = FilePathManager.getSkinImgPath(context, getCurrentThumbMainName(), getSkinDirName(needSaveSkinData));
                if (!saveBitmapToloLcal(thumbMainPath, thumbMainNewPath, thumbMainBitmap, skin, false))
                    return null;
//                String thumbMainPath = FilePathManager.getSkinImgPath(context, StyleConstants.SKIN_THUMBMAINPATH, getSkinDirName(needSaveSkinData));
//                File imgFile = new File(thumbMainPath);
//                if (imgFile.exists()) {
//                    if (!imgFile.delete()) {
//                        return null;
//                    }
//                }
//
//                if (thumbMainBitmap != null && thumbMainBitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(thumbMainPath))) {
//                    skin.setThumbMainPath(thumbMainPath);
//                } else {
//                    return null;
//                }

                publishProgress(40);
                //第三步 保存drawer图片
                String thumbDrawerPath = getSkinThumbDrawerPath(orginalSkin);
                String thumbDrawerNewPath = FilePathManager.getSkinImgPath(context, getCurrentThumbDraweName(), getSkinDirName(needSaveSkinData));
                if (!saveBitmapToloLcal(thumbDrawerPath, thumbDrawerNewPath, thumbDrawerBitmap, skin, true))
                    return null;
//                String thumbDrawerPath = FilePathManager.getSkinImgPath(context, StyleConstants.SKIN_THUMBDRAWERPATH, getSkinDirName(needSaveSkinData));
//                File imgDrawerFile = new File(thumbDrawerPath);
//                if (imgDrawerFile.exists()) {
//                    if (!imgDrawerFile.delete()) {
//                        return null;
//                    }
//                }
//                if (thumbDrawerBitmap != null && thumbDrawerBitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(thumbDrawerPath))) {
//                    skin.setThumbDrawerPath(thumbDrawerPath);
//                } else {
//                    return null;
//                }

                publishProgress(60);
                //第四步 完善skindata和skin保存skindata到skin地址
                skin.setType(AppSkinManager.SKIN_TYPE_LOCAL);
                skin.setSyncStatus(AppSkinManager.SKIN_SYNC_NO);
                skin.setCreateDate(TextUtils.isEmpty(orginalSkin.getCreateDate()) ? String.valueOf(System.currentTimeMillis()) : orginalSkin.getCreateDate());
                skin.setUpdateDate(String.valueOf(System.currentTimeMillis()));
                skin.setAuthorName(getSkinAutorName(needSaveSkinData));
                skin.setAuthorIdentifier(getSkinIdentifier(needSaveSkinData));
                skin.setLocalId(originalSkin.getLocalId());
                skin.setStatus(orginalSkin.getStatus());
                skin.setSkinOs(orginalSkin.getSkinOs());
                skin.setSkinSum(orginalSkin.getSkinSum());
                skin.setSkinDirName(getSkinDirName(needSaveSkinData));
                skin.setSkinCoverPath(orginalSkin.getSkinCoverPath());


                if (!saveFileBySkinData(needSaveSkinData, skin)) return null;
//                String skinDataPath = FilePathManager.getSkinFilePath(context, getSkinFileName(needSaveSkinData), getSkinDirName(needSaveSkinData));
//                File dataFile = new File(skinDataPath);
//                if (dataFile.exists()) {
//                    if (!dataFile.delete()) {
//                        return null;
//                    }
//                }
//                String data = JSONParser.toString(needSaveSkinData);
//                if (data == null) return null;
//                Log.d(TAG, "data json content" + data);
//                if (OkioFileUtil.writeZipFile(skinDataPath, data)) {
//                    skin.setLocalPath(skinDataPath);
//                } else {
//                    return null;
//                }
                //第五步 更新数据库

                int rowid = AppSkinManager.getInstance(context).startUpdateSkinFormSkinSync(skin, identifier);
                if (rowid >= 0) {
                    skin = AppSkinManager.getInstance(context).startQuerySkinByLocalIdSync(rowid);
                    Log.d(TAG, "doInBackground() returned: " + skin.toString());
                }
                publishProgress(90);
                return skin;

            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }

//            return null;
        }

        @Override
        protected void onPostExecute(Skin skin) {
            super.onPostExecute(skin);
            if (skin == null) {
                showToast(R.string.saving_fail);
                finish();
            } else {
                pbProgress.setProgress(100);
                tvSave.setText(R.string.saving_success);
                EventBus.getDefault().post(new DatabaseEvent.PostSkinUpdateEvent(skin));
                mHandler.sendEmptyMessageDelayed(7, 300);
//                AppSkinManager.getInstance(context).startUpdateSkinFormSkin(skin, identifier);
            }
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            for (Integer i : values) {
                pbProgress.setProgress(i);
            }
        }
    }
}
